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);
}
}
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();
}
}
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)));
}
}
Aggregations