KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > module > LineCountMeasure


1 /*
2  * @(#)LineCountMeasure.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.module;
28
29
30 import java.util.HashSet JavaDoc;
31
32 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData;
33 import net.sourceforge.groboutils.codecoverage.v2.IMethodCode;
34
35 import org.apache.bcel.classfile.LineNumber;
36 import org.apache.bcel.classfile.LineNumberTable;
37
38
39 /**
40  * Processes methods for line-count coverage analysis. Currently, this
41  * does not support localization.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version $Date: 2004/04/15 05:48:26 $
45  * @since December 17, 2002
46  * @see IAnalysisMetaData
47  */

48 public class LineCountMeasure extends AbstractMeasure
49 {
50     private static final org.apache.log4j.Logger LOG =
51         org.apache.log4j.Logger.getLogger( LineCountMeasure.class );
52     
53     /**
54      * Returns the human-readable name of the measure.
55      */

56     public String JavaDoc getMeasureName()
57     {
58         return "LineCount";
59     }
60     
61     /**
62      * Returns the unit name for this particular coverage measure.
63      */

64     public String JavaDoc getMeasureUnit()
65     {
66         return "lines";
67     }
68     
69     
70     /**
71      * Returns the text format used in meta-data formatted text. This should
72      * be the mime encoding type, such as "text/plain" or "text/html".
73      */

74     public String JavaDoc getMimeEncoding()
75     {
76         return "text/plain";
77     }
78     
79     
80     /**
81      * Perform the analysis on the method.
82      */

83     public void analyze( IMethodCode method )
84     {
85         //Method m = method.getOriginalMethod();
86
LineNumberTable lnt = method.getLineNumberTable();
87         if (lnt == null)
88         {
89             // nothing to do
90
LOG.info( "Method "+method+" has no line numbers." );
91             return;
92         }
93         
94         // we must ensure that the same line number is not counted
95
// twice.
96
HashSet JavaDoc linesFound = new HashSet JavaDoc();
97         
98         LineNumber[] lines = lnt.getLineNumberTable();
99         int maxInstructionCount = method.getInstructionCount();
100         int instructionPos[] = new int[ maxInstructionCount ];
101         
102         // find the positions of the instructions in the bytecode
103
int methodSize = 0;
104         for (int i = 0; i < maxInstructionCount; ++i)
105         {
106             instructionPos[i] = methodSize;
107             methodSize += method.getInstructionAt( i ).getLength();
108         }
109         
110         for (int i = 0; i < lines.length; ++i)
111         {
112             int bytecodeOffset = lines[i].getStartPC();
113             for (int j = 1; j < maxInstructionCount; ++j)
114             {
115                 if (bytecodeOffset < instructionPos[j])
116                 {
117                     Integer JavaDoc lineNo = new Integer JavaDoc( lines[i].getLineNumber() );
118                     if (!linesFound.contains( lineNo ) )
119                     {
120                         // the bytecode start was one up from the current
121
// instruction position.
122
IAnalysisMetaData amd = createAnalysisMetaData(
123                             lines[i] );
124                         
125                         // Bug 906207
126
markInstruction( method, j-1, amd, false );
127                         
128                         // ensure we don't find this line number again
129
linesFound.add( lineNo );
130                     }
131                     
132                     break;
133                 }
134             }
135         }
136     }
137     
138     
139     private IAnalysisMetaData createAnalysisMetaData( LineNumber ln )
140     {
141         int lineNo = ln.getLineNumber();
142         return new DefaultAnalysisMetaData(
143             "Line "+lineNo,
144             "Didn't cover line "+lineNo,
145             (byte)0 );
146     }
147 }
148
149
Popular Tags