• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Marc R. Hoffmann - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.examples;
14 
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.PrintStream;
18 
19 import org.jacoco.core.analysis.Analyzer;
20 import org.jacoco.core.analysis.IClassCoverage;
21 import org.jacoco.core.analysis.ICoverageVisitor;
22 import org.jacoco.core.data.ExecutionDataStore;
23 
24 /**
25  * This example reads Java class files, directories or JARs given as program
26  * arguments and dumps information about the classes.
27  */
28 public final class ClassInfo implements ICoverageVisitor {
29 
30 	private final PrintStream out;
31 	private final Analyzer analyzer;
32 
33 	/**
34 	 * Creates a new example instance printing to the given stream.
35 	 *
36 	 * @param out
37 	 *            stream for outputs
38 	 */
ClassInfo(final PrintStream out)39 	public ClassInfo(final PrintStream out) {
40 		this.out = out;
41 		analyzer = new Analyzer(new ExecutionDataStore(), this);
42 	}
43 
44 	/**
45 	 * Run this example with the given parameters.
46 	 *
47 	 * @param args
48 	 *            command line parameters
49 	 * @throws IOException
50 	 *             in case of error reading a input file
51 	 */
execute(final String[] args)52 	public void execute(final String[] args) throws IOException {
53 		for (final String file : args) {
54 			analyzer.analyzeAll(new File(file));
55 		}
56 	}
57 
visitCoverage(final IClassCoverage coverage)58 	public void visitCoverage(final IClassCoverage coverage) {
59 		out.printf("class name:   %s%n", coverage.getName());
60 		out.printf("class id:     %016x%n", Long.valueOf(coverage.getId()));
61 		out.printf("instructions: %s%n", Integer
62 				.valueOf(coverage.getInstructionCounter().getTotalCount()));
63 		out.printf("branches:     %s%n",
64 				Integer.valueOf(coverage.getBranchCounter().getTotalCount()));
65 		out.printf("lines:        %s%n",
66 				Integer.valueOf(coverage.getLineCounter().getTotalCount()));
67 		out.printf("methods:      %s%n",
68 				Integer.valueOf(coverage.getMethodCounter().getTotalCount()));
69 		out.printf("complexity:   %s%n%n", Integer
70 				.valueOf(coverage.getComplexityCounter().getTotalCount()));
71 	}
72 
73 	/**
74 	 * Entry point to run this examples as a Java application.
75 	 *
76 	 * @param args
77 	 *            list of program arguments
78 	 * @throws IOException
79 	 *             in case of errors executing the example
80 	 */
main(final String[] args)81 	public static void main(final String[] args) throws IOException {
82 		new ClassInfo(System.out).execute(args);
83 	}
84 
85 }
86