KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)CallPairMeasure.java
3  *
4  * Copyright (C) 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 net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData;
31 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
32 import net.sourceforge.groboutils.codecoverage.v2.IMethodCode;
33
34 import org.apache.bcel.generic.Instruction;
35 import org.apache.bcel.generic.InstructionHandle;
36 import org.apache.bcel.generic.InvokeInstruction;
37
38
39 /**
40  * Processes methods for call pair coverage analysis, where each function
41  * call instruction are marked.
42  * Currently, this does not support localization.
43  * <P>
44  * This is more accurately called "Object Code Branch Coverage", since
45  * true branch coverage requires the originating source code to correctly
46  * discover the branches.
47  * <P>
48  * This measure can be superior to line coverage due to the Java construct
49  * of the <tt>?:</tt> operation. This hides a branch inside a single
50  * statement. Also, some developers may put an <tt>if</tt> statement and
51  * its one-line branch all on the same line, which will hide the branch
52  * that was took.
53  *
54  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
55  * @version $Date: 2004/04/15 05:48:26 $
56  * @since February 17, 2003
57  * @see IAnalysisMetaData
58  */

59 public class CallPairMeasure implements IAnalysisModule
60 {
61     /**
62      * Returns the human-readable name of the measure.
63      */

64     public String JavaDoc getMeasureName()
65     {
66         return "Call Pair";
67     }
68     
69     /**
70      * Returns the unit name for this particular coverage measure.
71      */

72     public String JavaDoc getMeasureUnit()
73     {
74         return "call";
75     }
76     
77     
78     /**
79      * Returns the text format used in meta-data formatted text. This should
80      * be the mime encoding type, such as "text/plain" or "text/html".
81      */

82     public String JavaDoc getMimeEncoding()
83     {
84         return "text/plain";
85     }
86     
87     
88     /**
89      * Perform the analysis on the method.
90      */

91     public void analyze( IMethodCode method )
92     {
93         BytecodeLineUtil blu = new BytecodeLineUtil( method );
94         InstructionHandle handles[] = blu.getHandles();
95         
96         // find the positions of the instructions in the bytecode
97
for (int i = 0; i < handles.length; ++i)
98         {
99             InstructionHandle h = handles[i];
100             Instruction instr = h.getInstruction();
101             if (instr instanceof InvokeInstruction)
102             {
103                 // need to mark the target(s) and the next instruction, as it
104
// will be the "else" or "default" instruction.
105
int lineNo = blu.getLineNumber( h );
106                 method.markInstruction( i, createMetaData( i, lineNo ) );
107             }
108         }
109     }
110     
111     
112     private IAnalysisMetaData createMetaData( int bytecodePos, int lineNo )
113     {
114         return new DefaultAnalysisMetaData(
115             "Invoke for line "+lineNo,
116             "Didn't cover invocation for line "+lineNo,
117             (byte)0 );
118     }
119 }
120
121
Popular Tags