ClassCoverage.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 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 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.core.analysis;
   14
   15import java.util.Collection;
   16
   17/**
   18 * Coverage data of a single class.
   19 * 
   20 * @author Marc R. Hoffmann
   21 * @version $Revision: $
   22 */
   23public class ClassCoverage extends CoverageNodeImpl {
   24
   25    private final String signature;
   26    private final String superName;
   27    private final String[] interfaces;
   28    private final Collection<MethodCoverage> methods;
   29    private final String sourceFileName;
   30
   31    /**
   32     * Creates a class coverage data object with the given parameters.
   33     * 
   34     * @param name
   35     *            vm name of the class
   36     * @param signature
   37     *            vm signature of the class
   38     * @param superName
   39     *            vm name of the superclass of this class
   40     * @param interfaces
   41     *            vm names of interfaces of this class
   42     * @param sourceFileName
   43     *            optional name of the corresponding source file
   44     * @param methods
   45     *            contained methods
   46     */
   47    public ClassCoverage(final String name, final String signature,
   48            final String superName, final String[] interfaces,
   49            final String sourceFileName,
   50            final Collection<MethodCoverage> methods) {
   51        super(ElementType.CLASS, name, true);
   52        this.signature = signature;
   53        this.superName = superName;
   54        this.interfaces = interfaces;
   55        this.sourceFileName = sourceFileName;
   56        this.methods = methods;
   57        increment(methods);
   58        // As class is considered as covered when at least one method is
   59        // covered:
   60        final boolean covered = methodCounter.getCoveredCount() > 0;
   61        this.classCounter = CounterImpl.getInstance(covered);
   62    }
   63
   64    /**
   65     * Returns the VM signature of the class.
   66     * 
   67     * @return VM signature of the class (may be <code>null</code>)
   68     */
   69    public String getSignature() {
   70        return signature;
   71    }
   72
   73    /**
   74     * Returns the VM name of the superclass.
   75     * 
   76     * @return VM name of the super class (may be <code>null</code>, i.e.
   77     *         <code>java/lang/Object</code>)
   78     */
   79    public String getSuperName() {
   80        return superName;
   81    }
   82
   83    /**
   84     * Returns the VM names of implemented/extended interfaces
   85     * 
   86     * @return VM names of implemented/extended interfaces
   87     */
   88    public String[] getInterfaceNames() {
   89        return interfaces;
   90    }
   91
   92    /**
   93     * Returns the VM name of the package this class belongs to.
   94     * 
   95     * @return VM name of the package
   96     */
   97    public String getPackageName() {
   98        final int pos = getName().lastIndexOf('/');
   99        return pos == -1 ? "" : getName().substring(0, pos);
  100    }
  101
  102    /**
  103     * Returns the VM name of the class without the package prefix.
  104     * 
  105     * @return VM name of the class without the package
  106     */
  107    public String getSimpleName() {
  108        final int pos = getName().lastIndexOf('/');
  109        return pos == -1 ? getName() : getName().substring(pos + 1);
  110    }
  111
  112    /**
  113     * Returns the optional name of the corresponding source file.
  114     * 
  115     * @return name of the corresponding source file
  116     */
  117    public String getSourceFileName() {
  118        return sourceFileName;
  119    }
  120
  121    /**
  122     * Returns the methods included in this class.
  123     * 
  124     * @return methods of this class
  125     */
  126    public Collection<MethodCoverage> getMethods() {
  127        return methods;
  128    }
  129
  130}