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