1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
*
* This program and the accompanying materials are made available under
* the terms of the Common Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/cpl-v10.html
*
* $Id: ClassPathProcessorST.java,v 1.1.1.1.2.1 2004/07/16 23:32:03 vlad_r Exp $
*/
package com.vladium.emma.rt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.vladium.jcd.cls.ClassDef;
import com.vladium.jcd.parser.ClassDefParser;
import com.vladium.logging.Logger;
import com.vladium.util.ByteArrayOStream;
import com.vladium.util.Files;
import com.vladium.util.IPathEnumerator;
import com.vladium.util.asserts.$assert;
import com.vladium.emma.IAppErrorCodes;
import com.vladium.emma.EMMARuntimeException;
import com.vladium.emma.data.IMetaData;
import com.vladium.emma.filter.IInclExclFilter;
import com.vladium.emma.instr.InstrVisitor;
// ----------------------------------------------------------------------------
/**
* @author Vlad Roubtsov, (C) 2003
*/
public
final class ClassPathProcessorST implements IPathEnumerator.IPathHandler, IAppErrorCodes
{
// public: ................................................................
public void run ()
{
long start = System.currentTimeMillis ();
// construct instr path enumerator [throws on illegal input only]:
final IPathEnumerator enumerator = IPathEnumerator.Factory.create (m_path, m_canonical, this);
// allocate I/O buffers:
m_readbuf = new byte [BUF_SIZE]; // don't reuse this across run() calls to reset it to the original size
m_readpos = 0;
m_baos = new ByteArrayOStream (BUF_SIZE); // don't reuse this across run() calls to reset it to the original size
if (m_log.atINFO ())
{
m_log.info ("processing classpath ...");
}
// actual work is driven by the path enumerator:
try
{
enumerator.enumerate ();
}
catch (IOException ioe)
{
throw new EMMARuntimeException (INSTR_IO_FAILURE, ioe);
}
if (m_log.atINFO ())
{
final long end = System.currentTimeMillis ();
m_log.info ("[" + m_classCount + " class(es) processed in " + (end - start) + " ms]");
}
}
// IPathEnumerator.IPathHandler:
public void handleArchiveStart (final File parentDir, final File archive, final Manifest manifest)
{
m_archiveFile = Files.newFile (parentDir, archive.getPath ());
}
public void handleArchiveEntry (final JarInputStream in, final ZipEntry entry)
{
if (m_log.atTRACE2 ()) m_log.trace2 ("handleArchiveEntry", "[" + entry.getName () + "]");
final String name = entry.getName ();
final String lcName = name.toLowerCase ();
if (lcName.endsWith (".class"))
{
final String className = name.substring (0, name.length () - 6).replace ('/', '.');
if ((m_coverageFilter == null) || m_coverageFilter.included (className))
{
String srcURL = null;
InputStream clsin = null;
try
{
readZipEntry (in, entry);
srcURL = "jar:".concat (m_archiveFile.toURL ().toExternalForm ()).concat ("!/").concat (name);
}
catch (FileNotFoundException fnfe)
{
// ignore: this should never happen
if ($assert.ENABLED)
{
fnfe.printStackTrace (System.out);
}
}
catch (IOException ioe)
{
// TODO: error code
throw new EMMARuntimeException (ioe);
}
finally
{
if (clsin != null)
try
{
clsin.close ();
clsin = null;
}
catch (Exception e)
{
// TODO: error code
throw new EMMARuntimeException (e);
}
}
// [original class def read into m_readbuf]
try
{
ClassDef clsDef = ClassDefParser.parseClass (m_readbuf, m_readpos);
if (! clsDef.isInterface ()) ++ m_classCount;
m_visitor.process (clsDef, false, false, true, m_instrResult); // get metadata only
clsDef = null;
boolean cacheClassDef = true;
if (m_instrResult.m_descriptor != null)
{
// do not overwrite existing descriptors to support "first
// in the classpath wins" semantics:
if (! m_mdata.add (m_instrResult.m_descriptor, false))
cacheClassDef = false;
}
if (cacheClassDef && (m_cache != null))
{
final byte [] bytes = new byte [m_readpos];
System.arraycopy (m_readbuf, 0, bytes, 0, m_readpos);
m_cache.put (className, new ClassPathCacheEntry (bytes, srcURL));
}
}
catch (IOException ioe)
{
// TODO: error code
throw new EMMARuntimeException (ioe);
}
}
}
}
public void handleArchiveEnd (final File parentDir, final File archive)
{
m_archiveFile = null;
}
public void handleDirStart (final File pathDir, final File dir)
{
// do nothing
}
public void handleFile (final File pathDir, final File file)
{
if (m_log.atTRACE2 ()) m_log.trace2 ("handleFile", "[" + pathDir + "] [" + file + "]");
final String name = file.getPath ();
final String lcName = name.toLowerCase ();
if (lcName.endsWith (".class"))
{
final String className = name.substring (0, name.length () - 6).replace (File.separatorChar, '.');
if ((m_coverageFilter == null) || m_coverageFilter.included (className))
{
String srcURL = null;
InputStream clsin = null;
try
{
final File inFile = Files.newFile (pathDir, file.getPath ());
readFile (inFile);
srcURL = inFile.toURL ().toExternalForm ();
}
catch (FileNotFoundException fnfe)
{
// ignore: this should never happen
if ($assert.ENABLED)
{
fnfe.printStackTrace (System.out);
}
}
catch (IOException ioe)
{
// TODO: error code
throw new EMMARuntimeException (ioe);
}
finally
{
if (clsin != null)
try
{
clsin.close ();
clsin = null;
}
catch (Exception e)
{
// TODO: error code
throw new EMMARuntimeException (e);
}
}
// [original class def read into m_readbuf]
try
{
ClassDef clsDef = ClassDefParser.parseClass (m_readbuf, m_readpos);
if (! clsDef.isInterface ()) ++ m_classCount;
m_visitor.process (clsDef, false, false, true, m_instrResult); // get metadata only
clsDef = null;
boolean cacheClassDef = true;
if (m_instrResult.m_descriptor != null)
{
// do not overwrite existing descriptors to support "first
// in the classpath wins" semantics:
if (! m_mdata.add (m_instrResult.m_descriptor, false))
cacheClassDef = false;
}
if (cacheClassDef && (m_cache != null))
{
final byte [] bytes = new byte [m_readpos];
System.arraycopy (m_readbuf, 0, bytes, 0, m_readpos);
m_cache.put (className, new ClassPathCacheEntry (bytes, srcURL));
}
}
catch (IOException ioe)
{
// TODO: error code
throw new EMMARuntimeException (ioe);
}
}
}
}
public void handleDirEnd (final File pathDir, final File dir)
{
// do nothing
}
// protected: .............................................................
// package: ...............................................................
/*
* null 'cache' indicates to only populate the metadata and not bother with
* caching instrumented class defs
*/
ClassPathProcessorST (final File [] path, final boolean canonical,
final IMetaData mdata, final IInclExclFilter filter,
final Map cache)
{
if (path == null) throw new IllegalArgumentException ("null input: path");
if (mdata == null) throw new IllegalArgumentException ("null input: mdata");
m_path = path;
m_canonical = canonical;
m_mdata = mdata;
m_coverageFilter = filter;
m_cache = cache; // can be null
m_visitor = new InstrVisitor (mdata.getOptions ());
m_instrResult = new InstrVisitor.InstrResult ();
m_log = Logger.getLogger ();
}
// private: ...............................................................
/*
* Reads into m_readbuf (m_readpos is updated correspondingly)
*/
private void readFile (final File file)
throws IOException
{
final int length = (int) file.length ();
ensureReadCapacity (length);
InputStream in = null;
try
{
in = new FileInputStream (file);
int totalread = 0;
for (int read;
(totalread < length) && (read = in.read (m_readbuf, totalread, length - totalread)) >= 0;
totalread += read);
m_readpos = totalread;
}
finally
{
if (in != null) try { in.close (); } catch (Exception ignore) {}
}
}
/*
* Reads into m_readbuf (m_readpos is updated correspondingly)
*/
private void readZipEntry (final ZipInputStream in, final ZipEntry entry)
throws IOException
{
final int length = (int) entry.getSize (); // can be -1 if unknown
if (length >= 0)
{
ensureReadCapacity (length);
int totalread = 0;
for (int read;
(totalread < length) && (read = in.read (m_readbuf, totalread, length - totalread)) >= 0;
totalread += read);
m_readpos = totalread;
}
else
{
ensureReadCapacity (BUF_SIZE);
m_baos.reset ();
for (int read; (read = in.read (m_readbuf)) >= 0; m_baos.write (m_readbuf, 0, read));
m_readbuf = m_baos.copyByteArray ();
m_readpos = m_readbuf.length;
}
}
private void ensureReadCapacity (final int capacity)
{
if (m_readbuf.length < capacity)
{
final int readbuflen = m_readbuf.length;
m_readbuf = null;
m_readbuf = new byte [Math.max (readbuflen << 1, capacity)];
}
}
private final File [] m_path; // never null
private final boolean m_canonical;
private final IMetaData m_mdata; // never null
private final IInclExclFilter m_coverageFilter; // can be null
private final InstrVisitor m_visitor;
private final InstrVisitor.InstrResult m_instrResult;
private final Map /* classJavaName:String -> ClassPathCacheEntry */ m_cache; // can be null
private final Logger m_log; // this class is instantiated and used on a single thread
private int m_classCount;
private byte [] m_readbuf;
private int m_readpos;
private ByteArrayOStream m_baos; // TODO: code to guard this from becoming too large
private File m_archiveFile;
private static final int BUF_SIZE = 32 * 1024;
} // end of class
// ----------------------------------------------------------------------------
|