001: /*
002: * @(#)LineCountMeasure.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.module;
028:
029: import java.util.HashSet;
030:
031: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData;
032: import net.sourceforge.groboutils.codecoverage.v2.IMethodCode;
033:
034: import org.apache.bcel.classfile.LineNumber;
035: import org.apache.bcel.classfile.LineNumberTable;
036:
037: /**
038: * Processes methods for line-count coverage analysis. Currently, this
039: * does not support localization.
040: *
041: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
042: * @version $Date: 2004/04/15 05:48:26 $
043: * @since December 17, 2002
044: * @see IAnalysisMetaData
045: */
046: public class LineCountMeasure extends AbstractMeasure {
047: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
048: .getLogger(LineCountMeasure.class);
049:
050: /**
051: * Returns the human-readable name of the measure.
052: */
053: public String getMeasureName() {
054: return "LineCount";
055: }
056:
057: /**
058: * Returns the unit name for this particular coverage measure.
059: */
060: public String getMeasureUnit() {
061: return "lines";
062: }
063:
064: /**
065: * Returns the text format used in meta-data formatted text. This should
066: * be the mime encoding type, such as "text/plain" or "text/html".
067: */
068: public String getMimeEncoding() {
069: return "text/plain";
070: }
071:
072: /**
073: * Perform the analysis on the method.
074: */
075: public void analyze(IMethodCode method) {
076: //Method m = method.getOriginalMethod();
077: LineNumberTable lnt = method.getLineNumberTable();
078: if (lnt == null) {
079: // nothing to do
080: LOG.info("Method " + method + " has no line numbers.");
081: return;
082: }
083:
084: // we must ensure that the same line number is not counted
085: // twice.
086: HashSet linesFound = new HashSet();
087:
088: LineNumber[] lines = lnt.getLineNumberTable();
089: int maxInstructionCount = method.getInstructionCount();
090: int instructionPos[] = new int[maxInstructionCount];
091:
092: // find the positions of the instructions in the bytecode
093: int methodSize = 0;
094: for (int i = 0; i < maxInstructionCount; ++i) {
095: instructionPos[i] = methodSize;
096: methodSize += method.getInstructionAt(i).getLength();
097: }
098:
099: for (int i = 0; i < lines.length; ++i) {
100: int bytecodeOffset = lines[i].getStartPC();
101: for (int j = 1; j < maxInstructionCount; ++j) {
102: if (bytecodeOffset < instructionPos[j]) {
103: Integer lineNo = new Integer(lines[i]
104: .getLineNumber());
105: if (!linesFound.contains(lineNo)) {
106: // the bytecode start was one up from the current
107: // instruction position.
108: IAnalysisMetaData amd = createAnalysisMetaData(lines[i]);
109:
110: // Bug 906207
111: markInstruction(method, j - 1, amd, false);
112:
113: // ensure we don't find this line number again
114: linesFound.add(lineNo);
115: }
116:
117: break;
118: }
119: }
120: }
121: }
122:
123: private IAnalysisMetaData createAnalysisMetaData(LineNumber ln) {
124: int lineNo = ln.getLineNumber();
125: return new DefaultAnalysisMetaData("Line " + lineNo,
126: "Didn't cover line " + lineNo, (byte) 0);
127: }
128: }
|