| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 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.coveragedata.ProjectData; |
| 29 | |
|
| 30 | |
import org.apache.log4j.Logger; |
| 31 | |
import org.objectweb.asm.ClassAdapter; |
| 32 | |
import org.objectweb.asm.ClassVisitor; |
| 33 | |
import org.objectweb.asm.MethodVisitor; |
| 34 | |
import org.objectweb.asm.Opcodes; |
| 35 | |
|
| 36 | |
class ClassInstrumenter extends ClassAdapter |
| 37 | |
{ |
| 38 | |
|
| 39 | 4 | private static final Logger logger = Logger |
| 40 | |
.getLogger(ClassInstrumenter.class); |
| 41 | |
|
| 42 | |
private final static String hasBeenInstrumented = "net/sourceforge/cobertura/coveragedata/HasBeenInstrumented"; |
| 43 | |
|
| 44 | |
private Collection ignoreRegexs; |
| 45 | |
|
| 46 | |
private Collection ignoreBranchesRegexs; |
| 47 | |
|
| 48 | |
private ProjectData projectData; |
| 49 | |
|
| 50 | |
private ClassData classData; |
| 51 | |
|
| 52 | |
private String myName; |
| 53 | |
|
| 54 | 14 | private boolean instrument = false; |
| 55 | |
|
| 56 | |
public String getClassName() |
| 57 | |
{ |
| 58 | 9 | return this.myName; |
| 59 | |
} |
| 60 | |
|
| 61 | |
public boolean isInstrumented() |
| 62 | |
{ |
| 63 | 14 | return instrument; |
| 64 | |
} |
| 65 | |
|
| 66 | |
public ClassInstrumenter(ProjectData projectData, final ClassVisitor cv, |
| 67 | |
final Collection ignoreRegexs, final Collection ignoreBranchesRegexes) |
| 68 | |
{ |
| 69 | 14 | super(cv); |
| 70 | 14 | this.projectData = projectData; |
| 71 | 14 | this.ignoreRegexs = ignoreRegexs; |
| 72 | 14 | this.ignoreBranchesRegexs = ignoreBranchesRegexs; |
| 73 | 14 | } |
| 74 | |
|
| 75 | |
private boolean arrayContains(Object[] array, Object key) |
| 76 | |
{ |
| 77 | 22 | for (int i = 0; i < array.length; i++) |
| 78 | |
{ |
| 79 | 9 | if (array[i].equals(key)) |
| 80 | 0 | return true; |
| 81 | |
} |
| 82 | |
|
| 83 | 13 | return false; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public void visit(int version, int access, String name, String signature, |
| 91 | |
String superName, String[] interfaces) |
| 92 | |
{ |
| 93 | 14 | this.myName = name.replace('/', '.'); |
| 94 | 14 | this.classData = this.projectData.getOrCreateClassData(this.myName); |
| 95 | 14 | this.classData.setContainsInstrumentationInfo(); |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | 14 | if (((access & Opcodes.ACC_INTERFACE) != 0) |
| 100 | |
|| arrayContains(interfaces, hasBeenInstrumented)) |
| 101 | |
{ |
| 102 | 1 | super.visit(version, access, name, signature, superName, |
| 103 | |
interfaces); |
| 104 | 1 | } |
| 105 | |
else |
| 106 | |
{ |
| 107 | 13 | instrument = true; |
| 108 | |
|
| 109 | |
|
| 110 | 13 | String[] newInterfaces = new String[interfaces.length + 1]; |
| 111 | 13 | System.arraycopy(interfaces, 0, newInterfaces, 0, |
| 112 | |
interfaces.length); |
| 113 | 13 | newInterfaces[newInterfaces.length - 1] = hasBeenInstrumented; |
| 114 | |
|
| 115 | 13 | super.visit(version, access, name, signature, superName, |
| 116 | |
newInterfaces); |
| 117 | |
} |
| 118 | 14 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public void visitSource(String source, String debug) |
| 124 | |
{ |
| 125 | 11 | super.visitSource(source, debug); |
| 126 | 11 | classData.setSourceFileName(source); |
| 127 | 11 | } |
| 128 | |
|
| 129 | |
public MethodVisitor visitMethod(final int access, final String name, |
| 130 | |
final String desc, final String signature, |
| 131 | |
final String[] exceptions) |
| 132 | |
{ |
| 133 | 38 | MethodVisitor mv = cv.visitMethod(access, name, desc, signature, |
| 134 | |
exceptions); |
| 135 | |
|
| 136 | 38 | if (!instrument) |
| 137 | 0 | return mv; |
| 138 | |
|
| 139 | 38 | return mv == null ? null : new FirstPassMethodInstrumenter(classData, mv, |
| 140 | |
this.myName, access, name, desc, signature, exceptions, ignoreRegexs, |
| 141 | |
ignoreBranchesRegexs); |
| 142 | |
} |
| 143 | |
|
| 144 | |
public void visitEnd() |
| 145 | |
{ |
| 146 | 14 | if (instrument && classData.getNumberOfValidLines() == 0) |
| 147 | 3 | logger.warn("No line number information found for class " |
| 148 | |
+ this.myName |
| 149 | |
+ ". Perhaps you need to compile with debug=true?"); |
| 150 | 14 | } |
| 151 | |
|
| 152 | |
} |