aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar elenairina <elenairina@google.com>2017-07-11 10:22:52 +0200
committerGravatar László Csomor <laszlocsomor@google.com>2017-07-11 10:56:53 +0200
commite9e02123ba84bf05eca9c047278895b9158ea4cd (patch)
treeed25095609a295dd6c18fe9746e8fe2550750837 /src
parent76a251b8405cc49750d73561ce19902bda7f269e (diff)
Change JavaBuilder to work with the new Bazel Java coverage.
The new implementation doesn't use the metadata jar anymore, but wraps all the uninstrumented classes in the build jar among with a txt file that contains the paths of the files to be instrumented. PiperOrigin-RevId: 161499019
Diffstat (limited to 'src')
-rw-r--r--src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java8
-rw-r--r--src/java_tools/buildjar/java/com/google/devtools/build/buildjar/instrumentation/JacocoInstrumentationProcessor.java38
2 files changed, 33 insertions, 13 deletions
diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java
index fc1b6e4aa8..2948a5cb58 100644
--- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java
+++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/SimpleJavaLibraryBuilder.java
@@ -107,10 +107,6 @@ public class SimpleJavaLibraryBuilder implements Closeable {
}
};
BlazeJavacResult result = compileSources(build, javacRunner);
- JacocoInstrumentationProcessor processor = build.getJacocoInstrumentationProcessor();
- if (processor != null) {
- processor.processRequest(build);
- }
return result;
}
@@ -140,6 +136,10 @@ public class SimpleJavaLibraryBuilder implements Closeable {
jar.setNormalize(true);
jar.setCompression(build.compressJar());
jar.addDirectory(build.getClassDir());
+ JacocoInstrumentationProcessor processor = build.getJacocoInstrumentationProcessor();
+ if (processor != null) {
+ processor.processRequest(build, processor.isNewCoverageImplementation() ? jar : null);
+ }
} finally {
jar.execute();
}
diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/instrumentation/JacocoInstrumentationProcessor.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/instrumentation/JacocoInstrumentationProcessor.java
index df1f0f0fce..d31c00a9c9 100644
--- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/instrumentation/JacocoInstrumentationProcessor.java
+++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/instrumentation/JacocoInstrumentationProcessor.java
@@ -50,18 +50,27 @@ public final class JacocoInstrumentationProcessor {
}
private final String metadataDir;
- private final String metadataOutput;
+ private final String coverageInformation;
+ private final boolean isNewCoverageImplementation;
- private JacocoInstrumentationProcessor(String metadataDir, String metadataOutput) {
+ private JacocoInstrumentationProcessor(String metadataDir, String coverageInfo) {
this.metadataDir = metadataDir;
- this.metadataOutput = metadataOutput;
+ this.coverageInformation = coverageInfo;
+ // This is part of the new Java coverage implementation where JacocoInstrumentationProcessor
+ // receives a file that includes the relative paths of the uninstrumented Java files, instead
+ // of the metadata jar.
+ this.isNewCoverageImplementation = coverageInfo.endsWith(".txt");
+ }
+
+ public boolean isNewCoverageImplementation() {
+ return isNewCoverageImplementation;
}
/**
* 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 {
+ public void processRequest(JavaLibraryBuildRequest build, JarCreator jar) throws IOException {
// Clean up jacocoMetadataDir to be used by postprocessing steps. This is important when
// running JavaBuilder locally, to remove stale entries from previous builds.
if (metadataDir != null) {
@@ -71,15 +80,19 @@ public final class JacocoInstrumentationProcessor {
}
Files.createDirectories(workDir);
}
-
- JarCreator jar = new JarCreator(metadataOutput);
+ if (jar == null) {
+ jar = new JarCreator(coverageInformation);
+ }
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();
+ if (isNewCoverageImplementation) {
+ jar.addEntry(coverageInformation, coverageInformation);
+ } else {
+ jar.execute();
+ }
}
/**
@@ -102,7 +115,14 @@ public final class JacocoInstrumentationProcessor {
// We first move the original .class file to our metadata directory, then instrument it
// and output the instrumented version in the regular classes output directory.
Path instrumentedCopy = file;
- Path uninstrumentedCopy = Paths.get(metadataDir).resolve(root.relativize(file));
+ Path uninstrumentedCopy;
+ if (isNewCoverageImplementation) {
+ Path absoluteUninstrumentedCopy = Paths.get(file + ".uninstrumented");
+ uninstrumentedCopy =
+ Paths.get(metadataDir).resolve(root.relativize(absoluteUninstrumentedCopy));
+ } else {
+ uninstrumentedCopy = Paths.get(metadataDir).resolve(root.relativize(file));
+ }
Files.createDirectories(uninstrumentedCopy.getParent());
Files.move(file, uninstrumentedCopy);
try (InputStream input =