1 22 23 package net.sourceforge.cobertura.instrument; 24 25 import java.util.Collection ; 26 27 import net.sourceforge.cobertura.coveragedata.ClassData; 28 import net.sourceforge.cobertura.util.RegexUtil; 29 30 import org.objectweb.asm.Label; 31 import org.objectweb.asm.MethodAdapter; 32 import org.objectweb.asm.MethodVisitor; 33 import org.objectweb.asm.Opcodes; 34 35 39 public class MethodInstrumenter extends MethodAdapter implements Opcodes 40 { 41 42 private final String ownerClass; 43 44 private String myName; 45 46 private String myDescriptor; 47 48 private Collection ignoreRegexs; 49 50 private ClassData classData; 51 52 private int currentLine = 0; 53 54 public MethodInstrumenter(ClassData classData, final MethodVisitor mv, 55 final String owner, final String myName, final String myDescriptor, 56 final Collection ignoreRegexs) 57 { 58 super(mv); 59 this.classData = classData; 60 this.ownerClass = owner; 61 this.myName = myName; 62 this.myDescriptor = myDescriptor; 63 this.ignoreRegexs = ignoreRegexs; 64 } 65 66 public void visitJumpInsn(int opcode, Label label) 67 { 68 super.visitJumpInsn(opcode, label); 69 70 if ((opcode != GOTO) && (currentLine != 0) 76 && (!this.myName.equals("<clinit>"))) 77 classData.markLineAsBranch(currentLine); 78 } 79 80 public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) 81 { 82 super.visitLookupSwitchInsn(dflt, keys, labels); 83 if (currentLine != 0) 84 classData.markLineAsBranch(currentLine); 85 } 86 87 public void visitLineNumber(int line, Label start) 88 { 89 currentLine = line; 91 classData.addLine(currentLine, myName, myDescriptor); 92 93 mv.visitMethodInsn(INVOKESTATIC, 96 "net/sourceforge/cobertura/coveragedata/ProjectData", 97 "getGlobalProjectData", 98 "()Lnet/sourceforge/cobertura/coveragedata/ProjectData;"); 99 100 mv.visitLdcInsn(ownerClass); 103 mv 104 .visitMethodInsn(INVOKEVIRTUAL, 105 "net/sourceforge/cobertura/coveragedata/ProjectData", 106 "getOrCreateClassData", 107 "(Ljava/lang/String;)Lnet/sourceforge/cobertura/coveragedata/ClassData;"); 108 109 mv.visitIntInsn(SIPUSH, line); 112 mv.visitMethodInsn(INVOKEVIRTUAL, 113 "net/sourceforge/cobertura/coveragedata/ClassData", "touch", 114 "(I)V"); 115 116 super.visitLineNumber(line, start); 117 } 118 119 public void visitMethodInsn(int opcode, String owner, String name, 120 String desc) 121 { 122 super.visitMethodInsn(opcode, owner, name, desc); 123 124 if (RegexUtil.matches(ignoreRegexs, owner)) 127 { 128 classData.removeLine(currentLine); 129 } 130 } 131 132 } 133 | Popular Tags |