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 *******************************************************************************/
12package org.jacoco.cli.internal.commands;
13
14import java.io.File;
15import java.io.IOException;
16import java.io.PrintWriter;
17import java.util.ArrayList;
18import java.util.List;
19
20import org.jacoco.cli.internal.Command;
21import org.jacoco.core.analysis.Analyzer;
22import org.jacoco.core.analysis.IClassCoverage;
23import org.jacoco.core.analysis.ICoverageVisitor;
24import org.jacoco.core.data.ExecutionDataStore;
25import org.kohsuke.args4j.Argument;
26
27/**
28 * The <code>classinfo</code> command.
29 */
30public class ClassInfo extends Command {
31
32	@Argument(usage = "location of Java class files", metaVar = "<classlocations>")
33	List<File> classfiles = new ArrayList<File>();
34
35	@Override
36	public String description() {
37		return "Print information about Java class files at the provided location.";
38	}
39
40	@Override
41	public int execute(final PrintWriter out, final PrintWriter err)
42			throws IOException {
43		final Analyzer analyzer = new Analyzer(new ExecutionDataStore(),
44				new ICoverageVisitor() {
45					public void visitCoverage(final IClassCoverage coverage) {
46						print(coverage, out);
47					}
48				});
49
50		for (final File file : classfiles) {
51			analyzer.analyzeAll(file);
52		}
53		return 0;
54	}
55
56	private void print(final IClassCoverage coverage, final PrintWriter out) {
57		out.printf("class name:   %s%n", coverage.getName());
58		out.printf("class id:     %016x%n", Long.valueOf(coverage.getId()));
59		out.printf("instructions: %s%n", Integer
60				.valueOf(coverage.getInstructionCounter().getTotalCount()));
61		out.printf("branches:     %s%n",
62				Integer.valueOf(coverage.getBranchCounter().getTotalCount()));
63		out.printf("lines:        %s%n",
64				Integer.valueOf(coverage.getLineCounter().getTotalCount()));
65		out.printf("methods:      %s%n",
66				Integer.valueOf(coverage.getMethodCounter().getTotalCount()));
67		out.printf("complexity:   %s%n%n", Integer
68				.valueOf(coverage.getComplexityCounter().getTotalCount()));
69	}
70
71}
72