• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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 Collection<IMethodCoverage> methods;
28 	private String signature;
29 	private String superName;
30 	private String[] interfaces;
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 	 */
ClassCoverageImpl(final String name, final long id, final boolean noMatch)44 	public ClassCoverageImpl(final String name, final long id,
45 			final boolean noMatch) {
46 		super(ElementType.CLASS, name);
47 		this.id = id;
48 		this.noMatch = noMatch;
49 		this.methods = new ArrayList<IMethodCoverage>();
50 		this.classCounter = CounterImpl.COUNTER_1_0;
51 	}
52 
53 	/**
54 	 * Add a method to this class.
55 	 *
56 	 * @param method
57 	 *            method data to add
58 	 */
addMethod(final IMethodCoverage method)59 	public void addMethod(final IMethodCoverage method) {
60 		this.methods.add(method);
61 		increment(method);
62 		// As class is considered as covered when at least one method is
63 		// covered:
64 		if (methodCounter.getCoveredCount() > 0) {
65 			this.classCounter = CounterImpl.COUNTER_0_1;
66 		}
67 	}
68 
69 	/**
70 	 * Sets the VM signature of the class.
71 	 *
72 	 * @param signature
73 	 *            VM signature of the class (may be <code>null</code>)
74 	 */
setSignature(final String signature)75 	public void setSignature(final String signature) {
76 		this.signature = signature;
77 	}
78 
79 	/**
80 	 * Sets the VM name of the superclass.
81 	 *
82 	 * @param superName
83 	 *            VM name of the super class (may be <code>null</code>, i.e.
84 	 *            <code>java/lang/Object</code>)
85 	 */
setSuperName(final String superName)86 	public void setSuperName(final String superName) {
87 		this.superName = superName;
88 	}
89 
90 	/**
91 	 * Sets the VM names of implemented/extended interfaces.
92 	 *
93 	 * @param interfaces
94 	 *            VM names of implemented/extended interfaces
95 	 */
setInterfaces(final String[] interfaces)96 	public void setInterfaces(final String[] interfaces) {
97 		this.interfaces = interfaces;
98 	}
99 
100 	/**
101 	 * Sets the name of the corresponding source file for this class.
102 	 *
103 	 * @param sourceFileName
104 	 *            name of the source file
105 	 */
setSourceFileName(final String sourceFileName)106 	public void setSourceFileName(final String sourceFileName) {
107 		this.sourceFileName = sourceFileName;
108 	}
109 
110 	// === IClassCoverage implementation ===
111 
getId()112 	public long getId() {
113 		return id;
114 	}
115 
isNoMatch()116 	public boolean isNoMatch() {
117 		return noMatch;
118 	}
119 
getSignature()120 	public String getSignature() {
121 		return signature;
122 	}
123 
getSuperName()124 	public String getSuperName() {
125 		return superName;
126 	}
127 
getInterfaceNames()128 	public String[] getInterfaceNames() {
129 		return interfaces;
130 	}
131 
getPackageName()132 	public String getPackageName() {
133 		final int pos = getName().lastIndexOf('/');
134 		return pos == -1 ? "" : getName().substring(0, pos);
135 	}
136 
getSourceFileName()137 	public String getSourceFileName() {
138 		return sourceFileName;
139 	}
140 
getMethods()141 	public Collection<IMethodCoverage> getMethods() {
142 		return methods;
143 	}
144 
145 }
146