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