public InputStream openStream() { try { final IRuntime runtime = ArquillianRuntime.getInstance(); final Instrumenter instrumenter = new Instrumenter(runtime); final byte[] instrumented = instrumenter.instrument(asset.openStream(), EX_STRING); return new ByteArrayInputStream(instrumented); } catch (Exception e) { throw new RuntimeException("Could not instrument Asset " + asset, e); } } }
private int instrumentGzip(final InputStream input, final OutputStream output, final String name) throws IOException { final GZIPInputStream gzipInputStream; try { gzipInputStream = new GZIPInputStream(input); } catch (final IOException e) { throw instrumentError(name, e); } final GZIPOutputStream gzout = new GZIPOutputStream(output); final int count = instrumentAll(gzipInputStream, gzout, name); gzout.finish(); return count; }
/** * Creates a instrumented version of the given class if possible. * * @param buffer * definition of the class * @param name * a name used for exception messages * @return instrumented definition * @throws IOException * if the class can't be instrumented */ public byte[] instrument(final byte[] buffer, final String name) throws IOException { try { return instrument(buffer); } catch (final RuntimeException e) { throw instrumentError(name, e); } }
detector = new ContentTypeDetector(input); } catch (final IOException e) { throw instrumentError(name, e); instrument(detector.getInputStream(), output, name); return 1; case ContentTypeDetector.ZIPFILE: return instrumentZip(detector.getInputStream(), output, name); case ContentTypeDetector.GZFILE: return instrumentGzip(detector.getInputStream(), output, name); case ContentTypeDetector.PACK200FILE: return instrumentPack200(detector.getInputStream(), output, name); default: copy(detector.getInputStream(), output, name); return 0;
/** * Creates a instrumented version of the given class if possible. * * @param reader * definition of the class as ASM reader * @return instrumented definition * */ public byte[] instrument(final ClassReader reader) { return instrument(reader.b); }
/** * @param isJUnit5 tell if the given tests are JUnit5 or not * @param classesDirectory the path to the directory that contains the .class file of sources * @param testClassesDirectory the path to the directory that contains the .class file of test sources * @param blackList the names of the test methods to NOT be run. */ public JacocoRunner(boolean isJUnit5, String classesDirectory, String testClassesDirectory, List<String> blackList) { this.isJUnit5 = isJUnit5; try { this.instrumentedClassLoader = new MemoryClassLoader( new URL[]{ new File(classesDirectory).toURI().toURL(), new File(testClassesDirectory).toURI().toURL() } ); } catch (MalformedURLException e) { throw new RuntimeException(e); } this.blackList = blackList; this.runtime = new LoggerRuntime(); this.instrumenter = new Instrumenter(this.runtime); // instrument source code instrumentAll(classesDirectory); }
private int instrumentZip(final InputStream input, final OutputStream output, final String name) throws IOException { final ZipInputStream zipin = new ZipInputStream(input); final ZipOutputStream zipout = new ZipOutputStream(output); ZipEntry entry; int count = 0; while ((entry = nextEntry(zipin, name)) != null) { final String entryName = entry.getName(); if (signatureRemover.removeEntry(entryName)) { continue; } zipout.putNextEntry(new ZipEntry(entryName)); if (!signatureRemover.filterEntry(entryName, zipin, zipout)) { count += instrumentAll(zipin, zipout, name + "@" + entryName); } zipout.closeEntry(); } zipout.finish(); return count; }
private int instrument(final File src, final File dest) throws IOException { dest.getParentFile().mkdirs(); final InputStream input = new FileInputStream(src); try { final OutputStream output = new FileOutputStream(dest); try { return instrumenter.instrumentAll(input, output, src.getAbsolutePath()); } finally { output.close(); } } catch (final IOException e) { dest.delete(); throw e; } finally { input.close(); } }
private ZipEntry nextEntry(final ZipInputStream input, final String location) throws IOException { try { return input.getNextEntry(); } catch (final IOException e) { throw instrumentError(location, e); } }
/** * Creates a instrumented version of the given class file. The provided * {@link InputStream} and {@link OutputStream} instances are not closed by * this method. * * @param input * stream to read class definition from * @param output * stream to write the instrumented version of the class to * @param name * a name used for exception messages * @throws IOException * if reading data from the stream fails or the class can't be * instrumented */ public void instrument(final InputStream input, final OutputStream output, final String name) throws IOException { output.write(instrument(input, name)); }
/** * Creates a instrumented version of the given class if possible. The * provided {@link InputStream} is not closed by this method. * * @param input * stream to read class definition from * @param name * a name used for exception messages * @return instrumented definition * @throws IOException * if reading data from the stream fails or the class can't be * instrumented */ public byte[] instrument(final InputStream input, final String name) throws IOException { final byte[] bytes; try { bytes = InputStreams.readFully(input); } catch (final IOException e) { throw instrumentError(name, e); } return instrument(bytes, name); }
@Override public int execute(final PrintWriter out, final PrintWriter err) throws IOException { final File absoluteDest = dest.getAbsoluteFile(); instrumenter = new Instrumenter( new OfflineInstrumentationAccessGenerator()); int total = 0; for (final File s : source) { if (s.isFile()) { total += instrument(s, new File(absoluteDest, s.getName())); } else { total += instrumentRecursive(s, absoluteDest); } } out.printf("[INFO] %s classes instrumented to %s.%n", Integer.valueOf(total), absoluteDest); return 0; }
private int read(final InputStream input, final byte[] buffer, final String name) throws IOException { try { return input.read(buffer); } catch (final IOException e) { throw instrumentError(name, e); } }
public InputStream openStream() { try { final IRuntime runtime = ArquillianRuntime.getInstance(); final Instrumenter instrumenter = new Instrumenter(runtime); final byte[] instrumented = instrumenter.instrument(asset.openStream(), EX_STRING); return new ByteArrayInputStream(instrumented); } catch (Exception e) { throw new RuntimeException("Could not instrument Asset " + asset, e); } } }
private void instrumentAll(String classesDirectory) { final Iterator<File> iterator = FileUtils.iterateFiles(new File(classesDirectory), new String[]{"class"}, true); while (iterator.hasNext()) { final File next = iterator.next(); final String fileName = next.getPath().substring(classesDirectory.length() + (classesDirectory.endsWith(ConstantsHelper.FILE_SEPARATOR) ? 0 : 1)); final String fullQualifiedName = ConstantsHelper.pathToFullQualifiedName.apply(fileName).substring(0, fileName.length() - ".class".length()); try { instrumentedClassLoader.addDefinition(fullQualifiedName, instrumenter.instrument(instrumentedClassLoader.getResourceAsStream(fileName), fullQualifiedName)); } catch (IOException e) { throw new RuntimeException(fileName + "," + new File(fileName).getAbsolutePath() + "," + fullQualifiedName, e); } } clearCache(instrumentedClassLoader); }
@Override public void transform(@NonNull TransformInvocation invocation) throws IOException, TransformException, InterruptedException { checkNotNull(invocation.getOutputProvider(), "Missing output object for transform " + getName()); File outputDir = invocation.getOutputProvider().getContentLocation( "main", getOutputTypes(), getScopes(), Format.DIRECTORY); FileUtils.mkdirs(outputDir); TransformInput input = Iterables.getOnlyElement(invocation.getInputs()); // we don't want jar inputs. Preconditions.checkState(input.getJarInputs().isEmpty()); DirectoryInput directoryInput = Iterables.getOnlyElement(input.getDirectoryInputs()); File inputDir = directoryInput.getFile(); Instrumenter instrumenter = new Instrumenter(new OfflineInstrumentationAccessGenerator()); if (invocation.isIncremental()) { instrumentFilesIncremental(instrumenter, inputDir, outputDir, directoryInput.getChangedFiles()); } else { instrumentFilesFullRun(instrumenter, inputDir, outputDir); } }
private int instrumentPack200(final InputStream input, final OutputStream output, final String name) throws IOException { final InputStream unpackedInput; try { unpackedInput = Pack200Streams.unpack(input); } catch (final IOException e) { throw instrumentError(name, e); } final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final int count = instrumentAll(unpackedInput, buffer, name); Pack200Streams.pack(buffer.toByteArray(), output); return count; }
private static void instrumentFile( @NonNull Instrumenter instrumenter, @NonNull File inputFile, @NonNull File outputFile) throws IOException { InputStream inputStream = null; try { inputStream = Files.asByteSource(inputFile).openBufferedStream(); Files.createParentDirs(outputFile); byte[] instrumented = instrumenter.instrument( inputStream, inputFile.toString()); Files.write(instrumented, outputFile); } finally { Closeables.closeQuietly(inputStream); } }
final byte[] instrumented = instr.instrument(getTargetClass(targetName), ""); memoryClassLoader.addDefinition(junitName, instr.instrument(getTargetClass(junitName), "")); final Class<?> junitClass = memoryClassLoader.loadClass(junitName); JUnitCore junit = new JUnitCore();