Java源码示例:org.jacoco.core.analysis.Analyzer
示例1
@VisibleForTesting
IBundleCoverage analyzeStructure() throws IOException {
final CoverageBuilder coverageBuilder = new CoverageBuilder();
final Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);
Set<String> alreadyInstrumentedClasses = new HashSet<>();
if (uninstrumentedClasses == null) {
for (File classesJar : classesJars) {
analyzeUninstrumentedClassesFromJar(analyzer, classesJar, alreadyInstrumentedClasses);
}
} else {
for (Map.Entry<String, byte[]> entry : uninstrumentedClasses.entrySet()) {
analyzer.analyzeClass(entry.getValue(), entry.getKey());
}
}
// TODO(bazel-team): Find out where the name of the bundle can pop out in the report.
return coverageBuilder.getBundle("isthisevenused");
}
示例2
public void processNBModule(String projectName, List<String> classDirectories, List<String> sourceDirectories) throws IOException {
CoverageBuilder coverageBuilder = new CoverageBuilder();
Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);
for (String classDirectory : classDirectories) {
analyzer.analyzeAll(new File(classDirectory));
}
IBundleCoverage bundleCoverage = coverageBuilder.getBundle(projectName);
MultiSourceFileLocator sourceLocator = new MultiSourceFileLocator(4);
for (String sourceDirectory : sourceDirectories) {
sourceLocator.add(new DirectorySourceFileLocator(new File(sourceDirectory), DEF_ENCODING, 4));
}
groupVisitor.visitBundle(bundleCoverage, sourceLocator);
}
示例3
private IBundleCoverage analyzeStructure() throws IOException {
CoverageBuilder coverageBuilder = new CoverageBuilder();
Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);
String[] classesDirs = classesPath.split(":");
for (String classesDir : classesDirs) {
File classesDirFile = new File(classesDir);
if (classesDirFile.exists()) {
for (File clazz : FileUtils.getFiles(classesDirFile, coverageIncludes, coverageExcludes)) {
analyzer.analyzeAll(clazz);
}
}
}
return coverageBuilder.getBundle(title);
}
示例4
public void jacocoCheckpoint(File classFile, File csvDir) {
int idx = nextFileIdx;
csvDir.mkdirs();
try {
// Get exec data by dynamically calling RT.getAgent().getExecutionData()
Class RT = Class.forName("org.jacoco.agent.rt.RT");
Method getAgent = RT.getMethod("getAgent");
Object agent = getAgent.invoke(null);
Method dump = agent.getClass().getMethod("getExecutionData", boolean.class);
byte[] execData = (byte[]) dump.invoke(agent, false);
// Analyze exec data
ExecFileLoader loader = new ExecFileLoader();
loader.load(new ByteArrayInputStream(execData));
final CoverageBuilder builder = new CoverageBuilder();
Analyzer analyzer = new Analyzer(loader.getExecutionDataStore(), builder);
analyzer.analyzeAll(classFile);
// Generate CSV
File csv = new File(csvDir, String.format("cov-%05d.csv", idx));
try (FileOutputStream out = new FileOutputStream(csv)) {
IReportVisitor coverageVisitor = new CSVFormatter().createVisitor(out);
coverageVisitor.visitBundle(builder.getBundle("JQF"), null);
coverageVisitor.visitEnd();
out.flush();
}
} catch (Exception e) {
System.err.println(e);
}
}
示例5
/**
* Analyzes all uninstrumented class files found in the given jar.
*
* <p>The uninstrumented classes are named using the .class.uninstrumented suffix.
*/
private void analyzeUninstrumentedClassesFromJar(
Analyzer analyzer, File jar, Set<String> alreadyInstrumentedClasses) throws IOException {
JarFile jarFile = new JarFile(jar);
Enumeration<JarEntry> jarFileEntries = jarFile.entries();
while (jarFileEntries.hasMoreElements()) {
JarEntry jarEntry = jarFileEntries.nextElement();
String jarEntryName = jarEntry.getName();
if (jarEntryName.endsWith(".class.uninstrumented")
&& !alreadyInstrumentedClasses.contains(jarEntryName)) {
analyzer.analyzeAll(jarFile.getInputStream(jarEntry), jarEntryName);
alreadyInstrumentedClasses.add(jarEntryName);
}
}
}
示例6
/**
* Load a JaCoCo binary report and convert it to HTML.
* <br/>See <a href="http://www.eclemma.org/jacoco/trunk/doc/examples/java/ReportGenerator.java">report generator example code</a>.
*
* @param jacocoexec the JaCoCo binary report.
* @param reportdir the folder to store HTML report.
* @param prjClassesDir the directory containing project's compiled classes.
* @param prjSourcesDir the directory containing project's Java source files.
* @param projectName the project's name.
* @return the absolute path of HTML report's {@code index.html} file.
* @throws FileNotFoundException if the JaCoCo binary report, compiled classes or Java sources files directory can't be found.
* @throws IOException if an I/O error occurs.
*/
public static String toHtmlReport(File jacocoexec, File reportdir, File prjClassesDir, File prjSourcesDir, String projectName)
throws FileNotFoundException,
IOException {
// Load the JaCoCo binary report.
FileInputStream fis = new FileInputStream(jacocoexec);
ExecutionDataStore executionDataStore = new ExecutionDataStore();
SessionInfoStore sessionInfoStore = new SessionInfoStore();
try {
ExecutionDataReader executionDataReader = new ExecutionDataReader(fis);
executionDataReader.setExecutionDataVisitor(executionDataStore);
executionDataReader.setSessionInfoVisitor(sessionInfoStore);
while (executionDataReader.read()) {
}
} finally {
fis.close();
}
// Convert the binary report to HTML.
CoverageBuilder coverageBuilder = new CoverageBuilder();
Analyzer analyzer = new Analyzer(executionDataStore, coverageBuilder);
analyzer.analyzeAll(prjClassesDir);
IBundleCoverage bundleCoverage = coverageBuilder.getBundle("JaCoCoverage analysis of project \"" + projectName
+ "\" (powered by JaCoCo from EclEmma)");
HTMLFormatter htmlformatter = new HTMLFormatter();
IReportVisitor visitor = htmlformatter.createVisitor(new FileMultiReportOutput(reportdir));
visitor.visitInfo(sessionInfoStore.getInfos(), executionDataStore.getContents());
visitor.visitBundle(bundleCoverage, new DirectorySourceFileLocator(prjSourcesDir, DEF_ENCODING, 4));
visitor.visitEnd();
return new File(reportdir, "index.html").getAbsolutePath();
}
示例7
/**
* Load a JaCoCo binary report and convert it to XML.
* <br/>See <a href="http://www.eclemma.org/jacoco/trunk/doc/examples/java/ReportGenerator.java">report generator example code</a>.
*
* @param jacocoexec the JaCoCo binary report.
* @param xmlreport the XML file to generate.
* @param prjClassesDir the directory containing project's compiled classes.
* @param prjSourcesDir the directory containing project's Java source files.
* @throws FileNotFoundException if the JaCoCo binary report, compiled classes or Java sources files directory can't be found.
* @throws IOException if an I/O error occurs.
*/
public static void toXmlReport(File jacocoexec, File xmlreport, File prjClassesDir, File prjSourcesDir)
throws FileNotFoundException,
IOException {
// Load the JaCoCo binary report.
FileInputStream fis = new FileInputStream(jacocoexec);
ExecutionDataStore executionDataStore = new ExecutionDataStore();
SessionInfoStore sessionInfoStore = new SessionInfoStore();
try {
ExecutionDataReader executionDataReader = new ExecutionDataReader(fis);
executionDataReader.setExecutionDataVisitor(executionDataStore);
executionDataReader.setSessionInfoVisitor(sessionInfoStore);
while (executionDataReader.read()) {
}
} finally {
fis.close();
}
// Convert the binary report to XML.
CoverageBuilder coverageBuilder = new CoverageBuilder();
Analyzer analyzer = new Analyzer(executionDataStore, coverageBuilder);
analyzer.analyzeAll(prjClassesDir);
IBundleCoverage bundleCoverage = coverageBuilder.getBundle("JaCoCoverage analysis (powered by JaCoCo from EclEmma)");
XMLFormatter xmlformatter = new XMLFormatter();
xmlformatter.setOutputEncoding(DEF_ENCODING);
IReportVisitor visitor = xmlformatter.createVisitor(new FileOutputStream(xmlreport));
visitor.visitInfo(sessionInfoStore.getInfos(), executionDataStore.getContents());
visitor.visitBundle(bundleCoverage, new DirectorySourceFileLocator(prjSourcesDir, DEF_ENCODING, 4));
visitor.visitEnd();
}