1 26 27 package net.sourceforge.groboutils.codecoverage.v2.module; 28 29 30 import java.util.HashSet ; 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 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 56 public String getMeasureName() 57 { 58 return "LineCount"; 59 } 60 61 64 public String getMeasureUnit() 65 { 66 return "lines"; 67 } 68 69 70 74 public String getMimeEncoding() 75 { 76 return "text/plain"; 77 } 78 79 80 83 public void analyze( IMethodCode method ) 84 { 85 LineNumberTable lnt = method.getLineNumberTable(); 87 if (lnt == null) 88 { 89 LOG.info( "Method "+method+" has no line numbers." ); 91 return; 92 } 93 94 HashSet linesFound = new HashSet (); 97 98 LineNumber[] lines = lnt.getLineNumberTable(); 99 int maxInstructionCount = method.getInstructionCount(); 100 int instructionPos[] = new int[ maxInstructionCount ]; 101 102 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 lineNo = new Integer ( lines[i].getLineNumber() ); 118 if (!linesFound.contains( lineNo ) ) 119 { 120 IAnalysisMetaData amd = createAnalysisMetaData( 123 lines[i] ); 124 125 markInstruction( method, j-1, amd, false ); 127 128 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 |