• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.core.internal.analysis;
13 
14 import java.util.ArrayList;
15 import java.util.Collection;
16 
17 import org.jacoco.core.analysis.IClassCoverage;
18 import org.jacoco.core.analysis.IMethodCoverage;
19 
20 /**
21  * Implementation of {@link IClassCoverage}.
22  */
23 public class ClassCoverageImpl extends SourceNodeImpl implements IClassCoverage {
24 
25 	private final long id;
26 	private final boolean noMatch;
27 	private final String signature;
28 	private final String superName;
29 	private final String[] interfaces;
30 	private final Collection<IMethodCoverage> methods;
31 	private String sourceFileName;
32 
33 	/**
34 	 * Creates a class coverage data object with the given parameters.
35 	 *
36 	 * @param name
37 	 *            vm name of the class
38 	 * @param id
39 	 *            class identifier
40 	 * @param noMatch
41 	 *            <code>true</code>, if class id does not match with execution
42 	 *            data
43 	 * @param signature
44 	 *            vm signature of the class
45 	 * @param superName
46 	 *            vm name of the superclass of this class
47 	 * @param interfaces
48 	 *            vm names of interfaces of this class
49 	 */
ClassCoverageImpl(final String name, final long id, final boolean noMatch, final String signature, final String superName, final String[] interfaces)50 	public ClassCoverageImpl(final String name, final long id,
51 			final boolean noMatch, final String signature,
52 			final String superName, final String[] interfaces) {
53 		super(ElementType.CLASS, name);
54 		this.id = id;
55 		this.noMatch = noMatch;
56 		this.signature = signature;
57 		this.superName = superName;
58 		this.interfaces = interfaces;
59 		this.methods = new ArrayList<IMethodCoverage>();
60 		this.classCounter = CounterImpl.COUNTER_1_0;
61 	}
62 
63 	/**
64 	 * Add a method to this class.
65 	 *
66 	 * @param method
67 	 *            method data to add
68 	 */
addMethod(final IMethodCoverage method)69 	public void addMethod(final IMethodCoverage method) {
70 		this.methods.add(method);
71 		increment(method);
72 		// As class is considered as covered when at least one method is
73 		// covered:
74 		if (methodCounter.getCoveredCount() > 0) {
75 			this.classCounter = CounterImpl.COUNTER_0_1;
76 		}
77 	}
78 
79 	/**
80 	 * Sets the name of the corresponding source file for this class.
81 	 *
82 	 * @param sourceFileName
83 	 *            name of the source file
84 	 */
setSourceFileName(final String sourceFileName)85 	public void setSourceFileName(final String sourceFileName) {
86 		this.sourceFileName = sourceFileName;
87 	}
88 
89 	// === IClassCoverage implementation ===
90 
getId()91 	public long getId() {
92 		return id;
93 	}
94 
isNoMatch()95 	public boolean isNoMatch() {
96 		return noMatch;
97 	}
98 
getSignature()99 	public String getSignature() {
100 		return signature;
101 	}
102 
getSuperName()103 	public String getSuperName() {
104 		return superName;
105 	}
106 
getInterfaceNames()107 	public String[] getInterfaceNames() {
108 		return interfaces;
109 	}
110 
getPackageName()111 	public String getPackageName() {
112 		final int pos = getName().lastIndexOf('/');
113 		return pos == -1 ? "" : getName().substring(0, pos);
114 	}
115 
getSourceFileName()116 	public String getSourceFileName() {
117 		return sourceFileName;
118 	}
119 
getMethods()120 	public Collection<IMethodCoverage> getMethods() {
121 		return methods;
122 	}
123 
124 }
125