1 /******************************************************************************* 2 * Copyright (c) 2009, 2017 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.FileInputStream; 15 import java.io.IOException; 16 import java.io.PrintStream; 17 import java.util.Date; 18 19 import org.jacoco.core.data.ExecutionData; 20 import org.jacoco.core.data.ExecutionDataReader; 21 import org.jacoco.core.data.IExecutionDataVisitor; 22 import org.jacoco.core.data.ISessionInfoVisitor; 23 import org.jacoco.core.data.SessionInfo; 24 25 /** 26 * This example reads execution data files given as program arguments and dumps 27 * their content. 28 */ 29 public final class ExecDump { 30 31 private final PrintStream out; 32 33 /** 34 * Creates a new example instance printing to the given stream. 35 * 36 * @param out 37 * stream for outputs 38 */ ExecDump(final PrintStream out)39 public ExecDump(final PrintStream out) { 40 this.out = out; 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 dump(file); 54 } 55 } 56 dump(final String file)57 private void dump(final String file) throws IOException { 58 out.printf("exec file: %s%n", file); 59 out.println("CLASS ID HITS/PROBES CLASS NAME"); 60 61 final FileInputStream in = new FileInputStream(file); 62 final ExecutionDataReader reader = new ExecutionDataReader(in); 63 reader.setSessionInfoVisitor(new ISessionInfoVisitor() { 64 public void visitSessionInfo(final SessionInfo info) { 65 out.printf("Session \"%s\": %s - %s%n", info.getId(), new Date( 66 info.getStartTimeStamp()), 67 new Date(info.getDumpTimeStamp())); 68 } 69 }); 70 reader.setExecutionDataVisitor(new IExecutionDataVisitor() { 71 public void visitClassExecution(final ExecutionData data) { 72 out.printf("%016x %3d of %3d %s%n", 73 Long.valueOf(data.getId()), 74 Integer.valueOf(getHitCount(data.getProbes())), 75 Integer.valueOf(data.getProbes().length), 76 data.getName()); 77 } 78 }); 79 reader.read(); 80 in.close(); 81 out.println(); 82 } 83 getHitCount(final boolean[] data)84 private int getHitCount(final boolean[] data) { 85 int count = 0; 86 for (final boolean hit : data) { 87 if (hit) { 88 count++; 89 } 90 } 91 return count; 92 } 93 94 /** 95 * Entry point to run this examples as a Java application. 96 * 97 * @param args 98 * list of program arguments 99 * @throws IOException 100 * in case of errors executing the example 101 */ main(final String[] args)102 public static void main(final String[] args) throws IOException { 103 new ExecDump(System.out).execute(args); 104 } 105 } 106