Skip to content

Commit

Permalink
Update BranchDetailAnalyzer.
Browse files Browse the repository at this point in the history
BranchDetailAnalyzer should have also been updated as part of ff1f745 when the jacoco version in bazel was updated.

Changes like this will be avoided after #8378 is merged, because it enables testing with the java coverage tools at head.

Closes #8417.

PiperOrigin-RevId: 249400130
  • Loading branch information
iirina authored and copybara-github committed May 22, 2019
1 parent 6fe6e98 commit 88a313d
Showing 1 changed file with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.testing.coverage;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -22,6 +24,7 @@
import org.jacoco.core.analysis.ICoverageVisitor;
import org.jacoco.core.data.ExecutionData;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.internal.InputStreams;
import org.jacoco.core.internal.data.CRC64;
import org.jacoco.core.internal.flow.ClassProbesAdapter;
import org.objectweb.asm.ClassReader;
Expand All @@ -38,16 +41,37 @@ public class BranchDetailAnalyzer extends Analyzer {
private final Map<String, BranchCoverageDetail> branchDetails;

public BranchDetailAnalyzer(final ExecutionDataStore executionData) {
super(executionData, new ICoverageVisitor() {
@Override
public void visitCoverage(IClassCoverage coverage) {
}
});

super(
executionData,
new ICoverageVisitor() {
@Override
public void visitCoverage(IClassCoverage coverage) {}
});
this.executionData = executionData;
this.branchDetails = new TreeMap<String, BranchCoverageDetail>();
}

// Override all analyzeClass methods.
@Override
public void analyzeClass(final InputStream input, final String location) throws IOException {
final byte[] buffer;
try {
buffer = InputStreams.readFully(input);
} catch (final IOException e) {
throw analyzerError(location, e);
}
analyzeClass(buffer, location);
}

@Override
public void analyzeClass(final byte[] buffer, final String location) throws IOException {
try {
analyzeClass(buffer);
} catch (final RuntimeException cause) {
throw analyzerError(location, cause);
}
}

@Override
public void analyzeClass(final ClassReader reader) {
final Map<Integer, BranchExp> lineToBranchExp = mapProbes(reader);
Expand Down Expand Up @@ -78,6 +102,17 @@ public void analyzeClass(final ClassReader reader) {
}
}

private void analyzeClass(final byte[] source) {
final ClassReader reader = new ClassReader(source);
analyzeClass(reader);
}

private IOException analyzerError(final String location, final Exception cause) {
final IOException ex = new IOException(String.format("Error while analyzing %s.", location));
ex.initCause(cause);
return ex;
}

// Generate the line to probeExp map so that we can evaluate the coverage.
private Map<Integer, BranchExp> mapProbes(final ClassReader reader) {
final ClassProbesMapper mapper = new ClassProbesMapper();
Expand Down

0 comments on commit 88a313d

Please sign in to comment.