Search in sources :

Example 1 with Instrumenter

use of org.jacoco.core.instr.Instrumenter in project bazel by bazelbuild.

the class JacocoInstrumentationProcessor method processRequest.

/**
   * Instruments classes using Jacoco and keeps copies of uninstrumented class files in
   * jacocoMetadataDir, to be zipped up in the output file jacocoMetadataOutput.
   */
public void processRequest(JavaLibraryBuildRequest build) throws IOException {
    // running JavaBuilder locally, to remove stale entries from previous builds.
    if (metadataDir != null) {
        Path workDir = Paths.get(metadataDir);
        if (Files.exists(workDir)) {
            recursiveRemove(workDir);
        }
        Files.createDirectories(workDir);
    }
    JarCreator jar = new JarCreator(metadataOutput);
    jar.setNormalize(true);
    jar.setCompression(build.compressJar());
    Instrumenter instr = new Instrumenter(new OfflineInstrumentationAccessGenerator());
    // TODO(bazel-team): not sure whether Emma did anything fancier than this (multithreaded?)
    instrumentRecursively(instr, Paths.get(build.getClassDir()));
    jar.addDirectory(metadataDir);
    jar.execute();
}
Also used : Path(java.nio.file.Path) JarCreator(com.google.devtools.build.buildjar.jarhelper.JarCreator) OfflineInstrumentationAccessGenerator(org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator) Instrumenter(org.jacoco.core.instr.Instrumenter)

Example 2 with Instrumenter

use of org.jacoco.core.instr.Instrumenter in project jacoco by jacoco.

the class ClassFileVersionsTest method testVersion.

private void testVersion(int version, boolean frames) throws IOException {
    final byte[] original = createClass(version);
    IRuntime runtime = new SystemPropertiesRuntime();
    Instrumenter instrumenter = new Instrumenter(runtime);
    byte[] instrumented = instrumenter.instrument(original, "TestTarget");
    assertFrames(instrumented, frames);
}
Also used : SystemPropertiesRuntime(org.jacoco.core.runtime.SystemPropertiesRuntime) Instrumenter(org.jacoco.core.instr.Instrumenter) IRuntime(org.jacoco.core.runtime.IRuntime)

Example 3 with Instrumenter

use of org.jacoco.core.instr.Instrumenter in project jacoco by jacoco.

the class CyclomaticComplexityTest method instrument.

private void instrument(final Class<? extends Target> clazz) throws Exception {
    bytes = TargetLoader.getClassDataAsBytes(clazz);
    final byte[] instrumented = new Instrumenter(runtime).instrument(bytes, "TestTarget");
    final TargetLoader loader = new TargetLoader();
    target = (Target) loader.add(clazz, instrumented).newInstance();
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) Instrumenter(org.jacoco.core.instr.Instrumenter)

Example 4 with Instrumenter

use of org.jacoco.core.instr.Instrumenter in project jacoco by jacoco.

the class InstrumentMojo method executeMojo.

@Override
public void executeMojo() throws MojoExecutionException, MojoFailureException {
    final File originalClassesDir = new File(getProject().getBuild().getDirectory(), "generated-classes/jacoco");
    originalClassesDir.mkdirs();
    final File classesDir = new File(getProject().getBuild().getOutputDirectory());
    if (!classesDir.exists()) {
        getLog().info("Skipping JaCoCo execution due to missing classes directory:" + classesDir);
        return;
    }
    final List<String> fileNames;
    try {
        fileNames = new FileFilter(includes, excludes).getFileNames(classesDir);
    } catch (final IOException e1) {
        throw new MojoExecutionException("Unable to get list of files to instrument.", e1);
    }
    final Instrumenter instrumenter = new Instrumenter(new OfflineInstrumentationAccessGenerator());
    for (final String fileName : fileNames) {
        if (fileName.endsWith(".class")) {
            final File source = new File(classesDir, fileName);
            final File backup = new File(originalClassesDir, fileName);
            InputStream input = null;
            OutputStream output = null;
            try {
                FileUtils.copyFile(source, backup);
                input = new FileInputStream(backup);
                output = new FileOutputStream(source);
                instrumenter.instrument(input, output, source.getPath());
            } catch (final IOException e2) {
                throw new MojoExecutionException("Unable to instrument file.", e2);
            } finally {
                IOUtil.close(input);
                IOUtil.close(output);
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) OfflineInstrumentationAccessGenerator(org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator) IOException(java.io.IOException) Instrumenter(org.jacoco.core.instr.Instrumenter) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with Instrumenter

use of org.jacoco.core.instr.Instrumenter in project jacoco by jacoco.

the class CoreTutorial method execute.

/**
 * Run this example.
 *
 * @throws Exception
 *             in case of errors
 */
public void execute() throws Exception {
    final String targetName = TestTarget.class.getName();
    // For instrumentation and runtime we need a IRuntime instance
    // to collect execution data:
    final IRuntime runtime = new LoggerRuntime();
    // The Instrumenter creates a modified version of our test target class
    // that contains additional probes for execution data recording:
    final Instrumenter instr = new Instrumenter(runtime);
    InputStream original = getTargetClass(targetName);
    final byte[] instrumented = instr.instrument(original, targetName);
    original.close();
    // Now we're ready to run our instrumented class and need to startup the
    // runtime first:
    final RuntimeData data = new RuntimeData();
    runtime.startup(data);
    // In this tutorial we use a special class loader to directly load the
    // instrumented class definition from a byte[] instances.
    final MemoryClassLoader memoryClassLoader = new MemoryClassLoader();
    memoryClassLoader.addDefinition(targetName, instrumented);
    final Class<?> targetClass = memoryClassLoader.loadClass(targetName);
    // Here we execute our test target class through its Runnable interface:
    final Runnable targetInstance = (Runnable) targetClass.newInstance();
    targetInstance.run();
    // At the end of test execution we collect execution data and shutdown
    // the runtime:
    final ExecutionDataStore executionData = new ExecutionDataStore();
    final SessionInfoStore sessionInfos = new SessionInfoStore();
    data.collect(executionData, sessionInfos, false);
    runtime.shutdown();
    // Together with the original class definition we can calculate coverage
    // information:
    final CoverageBuilder coverageBuilder = new CoverageBuilder();
    final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
    original = getTargetClass(targetName);
    analyzer.analyzeClass(original, targetName);
    original.close();
    // Let's dump some metrics and line coverage information:
    for (final IClassCoverage cc : coverageBuilder.getClasses()) {
        out.printf("Coverage of class %s%n", cc.getName());
        printCounter("instructions", cc.getInstructionCounter());
        printCounter("branches", cc.getBranchCounter());
        printCounter("lines", cc.getLineCounter());
        printCounter("methods", cc.getMethodCounter());
        printCounter("complexity", cc.getComplexityCounter());
        for (int i = cc.getFirstLine(); i <= cc.getLastLine(); i++) {
            out.printf("Line %s: %s%n", Integer.valueOf(i), getColor(cc.getLine(i).getStatus()));
        }
    }
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) LoggerRuntime(org.jacoco.core.runtime.LoggerRuntime) InputStream(java.io.InputStream) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) Analyzer(org.jacoco.core.analysis.Analyzer) IRuntime(org.jacoco.core.runtime.IRuntime) ExecutionDataStore(org.jacoco.core.data.ExecutionDataStore) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) Instrumenter(org.jacoco.core.instr.Instrumenter) SessionInfoStore(org.jacoco.core.data.SessionInfoStore)

Aggregations

Instrumenter (org.jacoco.core.instr.Instrumenter)16 IRuntime (org.jacoco.core.runtime.IRuntime)8 SystemPropertiesRuntime (org.jacoco.core.runtime.SystemPropertiesRuntime)6 LoggerRuntime (org.jacoco.core.runtime.LoggerRuntime)4 OfflineInstrumentationAccessGenerator (org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator)4 RuntimeData (org.jacoco.core.runtime.RuntimeData)3 TargetLoader (org.jacoco.core.test.TargetLoader)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Callable (java.util.concurrent.Callable)2 ClassNode (org.objectweb.asm.tree.ClassNode)2 MethodNode (org.objectweb.asm.tree.MethodNode)2 JarCreator (com.google.devtools.build.buildjar.jarhelper.JarCreator)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 BuildException (org.apache.tools.ant.BuildException)1