1 26 27 package net.sourceforge.groboutils.codecoverage.v2.compiler; 28 29 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData; 30 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule; 31 import net.sourceforge.groboutils.codecoverage.v2.IMethodCode; 32 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord; 33 import net.sourceforge.groboutils.codecoverage.v2.datastore.MarkRecord; 34 35 import org.apache.bcel.classfile.LineNumberTable; 36 import org.apache.bcel.classfile.Method; 37 import org.apache.bcel.generic.Instruction; 38 39 40 50 public class DefaultMethodCode implements IMethodCode 51 { 52 private static final org.apache.log4j.Logger LOG = 53 org.apache.log4j.Logger.getLogger( DefaultMethodCode.class ); 54 55 private final ModifiedMethod method; 56 private final ModifiedInstructionList list; 57 private final String className; 58 private final String methodName; 59 private final IAnalysisModule analysisModule; 60 private final short measureIndex; 61 private final ClassRecord cr; 62 private short markCount = 0; 63 64 65 DefaultMethodCode( short measureIndex, ModifiedMethod mm, ClassRecord cr ) 66 { 67 if (mm == null || cr == null) 68 { 69 throw new IllegalArgumentException ("no null args"); 70 } 71 72 this.method = mm; 73 this.list = mm.getInstructionList(); 74 if (this.list == null) 75 { 76 throw new IllegalStateException ("abstract method "+mm); 77 } 78 this.className = mm.getOriginalClass().getClassName(); 79 this.methodName = mm.getMethodName(); 80 this.measureIndex = measureIndex; 81 this.cr = cr; 82 this.analysisModule = this.cr.getAnalysisModuleSet(). 83 getAnalysisModuleAt( measureIndex ); 84 85 if (!this.className.equals( cr.getClassName() )) 87 { 88 throw new IllegalArgumentException ( 89 "modified method class ("+this.className+ 90 ") and class record class name ("+cr.getClassName()+ 91 ") do not match: " ); 92 } 93 if (cr.getMethodIndex( this.methodName ) < 0) 94 { 95 throw new IllegalArgumentException ( 96 "method name ("+this.methodName+ 97 ") and class record do not match" ); 98 } 99 } 100 101 102 107 public Method getOriginalMethod() 108 { 109 return this.method.getOriginalMethod(); 110 } 111 112 113 119 public LineNumberTable getLineNumberTable() 120 { 121 return getOriginalMethod().getLineNumberTable(); 122 } 123 124 125 130 public String getMethodName() 131 { 132 return this.methodName; 133 } 134 135 136 141 public String getClassName() 142 { 143 return this.className; 144 } 145 146 147 152 public int getInstructionCount() 153 { 154 return this.list.getInstructionCount(); 155 } 156 157 158 166 public Instruction getInstructionAt( int index ) 167 { 168 if (index == getInstructionCount()) 171 { 172 throw new IndexOutOfBoundsException ( 173 "Even though you can put a mark at one more than the last "+ 174 "instruction, you cannot retrieve any such instruction." ); 175 } 176 177 MarkedInstruction mi = this.list.getInstructionAt( index ); 178 return mi.getInstruction(); 179 } 180 181 182 194 public synchronized void markInstruction( int index, 195 IAnalysisMetaData meta ) 196 { 197 if (meta == null) 198 { 199 throw new IllegalArgumentException ("no null args"); 200 } 201 short count = this.markCount; 202 ++this.markCount; 203 204 LOG.debug( "Mark "+this+" at instruction "+index+"." ); 205 206 MarkedInstruction mi = this.list.getInstructionAt( index ); 208 mi.addMark( this.measureIndex, count ); 209 210 211 MarkRecord mr = new MarkRecord( meta, 212 this.analysisModule.getMeasureName(), getMethodName(), count, 213 getSourceLine( mi ) ); 214 cr.addMark( mr ); 215 } 216 217 218 221 public String toString() 222 { 223 return getClassName() + "#" + getMethodName(); 224 } 225 226 227 private int getSourceLine( MarkedInstruction mi ) 228 { 229 int ret = -1; 230 LineNumberTable lnt = getLineNumberTable(); 231 if (lnt != null) 232 { 233 try 236 { 237 ret = lnt.getSourceLine( mi.getInstructionPosition() ); 238 } 239 catch (ArrayIndexOutOfBoundsException e) 240 { 241 ret = -1; 242 } 243 } 244 return ret; 245 } 246 } 247 248 | Popular Tags |