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