Search in sources :

Example 16 with Instrumenter

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

the class StructuredLockingTest method assertStructuredLocking.

private void assertStructuredLocking(byte[] source) throws Exception {
    IRuntime runtime = new SystemPropertiesRuntime();
    Instrumenter instrumenter = new Instrumenter(runtime);
    byte[] instrumented = instrumenter.instrument(source, "TestTarget");
    final int version = BytecodeVersion.get(instrumented);
    instrumented = BytecodeVersion.downgradeIfNeeded(version, instrumented);
    ClassNode cn = new ClassNode();
    new ClassReader(instrumented).accept(cn, 0);
    for (MethodNode mn : cn.methods) {
        assertStructuredLocking(cn.name, mn);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) SystemPropertiesRuntime(org.jacoco.core.runtime.SystemPropertiesRuntime) ClassReader(org.objectweb.asm.ClassReader) Instrumenter(org.jacoco.core.instr.Instrumenter) IRuntime(org.jacoco.core.runtime.IRuntime)

Example 17 with Instrumenter

use of org.jacoco.core.instr.Instrumenter in project bazel-jdt-java-toolchain by salesforce.

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, JarCreator jar) throws IOException {
    // Use a directory for coverage metadata  that is unique to each built jar. Avoids
    // multiple threads performing read/write/delete actions on the instrumented classes directory.
    instrumentedClassesDirectory = getMetadataDirRelativeToJar(build.getOutputJar());
    Files.createDirectories(instrumentedClassesDirectory);
    if (jar == null) {
        jar = new JarCreator(coverageInformation);
    }
    jar.setNormalize(true);
    jar.setCompression(build.compressJar());
    Instrumenter instr = new Instrumenter(new OfflineInstrumentationAccessGenerator());
    instrumentRecursively(instr, build.getClassDir());
    jar.addDirectory(instrumentedClassesDirectory);
    if (isNewCoverageImplementation) {
        jar.addEntry(coverageInformation, coverageInformation);
    } else {
        jar.execute();
        cleanup();
    }
}
Also used : JarCreator(com.google.devtools.build.buildjar.jarhelper.JarCreator) OfflineInstrumentationAccessGenerator(org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator) Instrumenter(org.jacoco.core.instr.Instrumenter)

Example 18 with Instrumenter

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

the class JacocoProcessor method transformerBuildItem.

@BuildStep(onlyIf = IsTest.class)
void transformerBuildItem(BuildProducer<BytecodeTransformerBuildItem> transformers, OutputTargetBuildItem outputTargetBuildItem, ApplicationArchivesBuildItem applicationArchivesBuildItem, BuildSystemTargetBuildItem buildSystemTargetBuildItem, CurateOutcomeBuildItem curateOutcomeBuildItem, LaunchModeBuildItem launchModeBuildItem, JacocoConfig config) throws Exception {
    if (launchModeBuildItem.isAuxiliaryApplication()) {
        // no code coverage for continuous testing, it does not really make sense
        return;
    }
    String dataFile = outputTargetBuildItem.getOutputDirectory().toAbsolutePath().toString() + File.separator + config.dataFile;
    System.setProperty("jacoco-agent.destfile", dataFile);
    if (!config.reuseDataFile) {
        Files.deleteIfExists(Paths.get(dataFile));
    }
    Instrumenter instrumenter = new Instrumenter(new OfflineInstrumentationAccessGenerator());
    Set<String> seen = new HashSet<>();
    for (ApplicationArchive archive : applicationArchivesBuildItem.getAllApplicationArchives()) {
        for (ClassInfo i : archive.getIndex().getKnownClasses()) {
            String className = i.name().toString();
            if (seen.contains(className)) {
                continue;
            }
            seen.add(className);
            transformers.produce(new BytecodeTransformerBuildItem.Builder().setClassToTransform(className).setCacheable(true).setEager(true).setInputTransformer(new BiFunction<String, byte[], byte[]>() {

                @Override
                public byte[] apply(String className, byte[] bytes) {
                    try {
                        byte[] enhanced = instrumenter.instrument(bytes, className);
                        if (enhanced == null) {
                            return bytes;
                        }
                        return enhanced;
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).build());
        }
    }
    if (config.report) {
        ReportInfo info = new ReportInfo();
        info.dataFile = dataFile;
        File targetdir = new File(outputTargetBuildItem.getOutputDirectory().toAbsolutePath().toString() + File.separator + config.reportLocation);
        info.reportDir = targetdir.getAbsolutePath();
        String includes = StringUtils.join(config.includes.iterator(), ",");
        String excludes = StringUtils.join(config.excludes.orElse(Collections.emptyList()).iterator(), ",");
        Set<String> classes = new HashSet<>();
        info.classFiles = classes;
        Set<String> sources = new HashSet<>();
        ApplicationModel model;
        if (BuildToolHelper.isMavenProject(targetdir.toPath())) {
            model = curateOutcomeBuildItem.getApplicationModel();
        } else if (BuildToolHelper.isGradleProject(targetdir.toPath())) {
            // this seems counter productive, but we want the dev mode model and not the test model
            // as the test model will include the test classes that we don't want in the report
            model = BuildToolHelper.enableGradleAppModelForDevMode(targetdir.toPath());
        } else {
            throw new RuntimeException("Cannot determine project type generating Jacoco report");
        }
        if (model.getApplicationModule() != null) {
            addProjectModule(model.getAppArtifact(), config, info, includes, excludes, classes, sources);
        }
        for (ResolvedDependency d : model.getDependencies()) {
            if (d.isRuntimeCp() && d.isWorkspaceModule()) {
                addProjectModule(d, config, info, includes, excludes, classes, sources);
            }
        }
        info.sourceDirectories = sources;
        info.artifactId = buildSystemTargetBuildItem.getBaseName();
        Runtime.getRuntime().addShutdownHook(new Thread(new ReportCreator(info, config)));
    }
}
Also used : ReportCreator(io.quarkus.jacoco.runtime.ReportCreator) ResolvedDependency(io.quarkus.maven.dependency.ResolvedDependency) ReportInfo(io.quarkus.jacoco.runtime.ReportInfo) ApplicationModel(io.quarkus.bootstrap.model.ApplicationModel) OfflineInstrumentationAccessGenerator(org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator) IOException(java.io.IOException) ApplicationArchive(io.quarkus.deployment.ApplicationArchive) Instrumenter(org.jacoco.core.instr.Instrumenter) File(java.io.File) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

Instrumenter (org.jacoco.core.instr.Instrumenter)18 IRuntime (org.jacoco.core.runtime.IRuntime)8 OfflineInstrumentationAccessGenerator (org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator)6 SystemPropertiesRuntime (org.jacoco.core.runtime.SystemPropertiesRuntime)6 LoggerRuntime (org.jacoco.core.runtime.LoggerRuntime)4 File (java.io.File)3 RuntimeData (org.jacoco.core.runtime.RuntimeData)3 TargetLoader (org.jacoco.core.test.TargetLoader)3 JarCreator (com.google.devtools.build.buildjar.jarhelper.JarCreator)2 IOException (java.io.IOException)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 ApplicationModel (io.quarkus.bootstrap.model.ApplicationModel)1 ApplicationArchive (io.quarkus.deployment.ApplicationArchive)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 ReportCreator (io.quarkus.jacoco.runtime.ReportCreator)1 ReportInfo (io.quarkus.jacoco.runtime.ReportInfo)1 ResolvedDependency (io.quarkus.maven.dependency.ResolvedDependency)1