Only instrument .class files

+ Report exceptions thrown by the instrumenter with the info of the class name
This commit is contained in:
Andrey Breslav
2013-05-12 14:51:22 +02:00
parent 8dfb39ec19
commit d99ea2a783
@@ -175,25 +175,35 @@ public class InterceptionInstrumenter {
} }
public byte[] instrument(String resourceName, byte[] data) { public byte[] instrument(String resourceName, byte[] data) {
List<MethodInstrumenter> applicableInstrumenters = new ArrayList<MethodInstrumenter>(); try {
String className = stripClassSuffix(resourceName); String className = stripClassSuffix(resourceName);
for (Map.Entry<String, ClassMatcher> classPatternEntry : classPatterns.entrySet()) { if (className == null) {
String classPattern = classPatternEntry.getKey(); // Not a .class file
ClassMatcher classMatcher = classPatternEntry.getValue(); return data;
if (classMatcher.classPattern.matcher(className).matches()) {
neverMatchedClassPatterns.remove(classPattern);
applicableInstrumenters.addAll(classMatcher.instrumenters);
} }
List<MethodInstrumenter> applicableInstrumenters = new ArrayList<MethodInstrumenter>();
for (Map.Entry<String, ClassMatcher> classPatternEntry : classPatterns.entrySet()) {
String classPattern = classPatternEntry.getKey();
ClassMatcher classMatcher = classPatternEntry.getValue();
if (classMatcher.classPattern.matcher(className).matches()) {
neverMatchedClassPatterns.remove(classPattern);
applicableInstrumenters.addAll(classMatcher.instrumenters);
}
}
if (applicableInstrumenters.isEmpty()) return data;
return instrument(data, applicableInstrumenters);
}
catch (Throwable e) {
throw new IllegalStateException("Exception while instrumenting " + resourceName, e);
} }
if (applicableInstrumenters.isEmpty()) return data;
return instrument(data, applicableInstrumenters);
} }
private static String stripClassSuffix(String name) { private static String stripClassSuffix(String name) {
String suffix = ".class"; String suffix = ".class";
if (!name.endsWith(suffix)) return name; if (!name.endsWith(suffix)) return null;
return name.substring(0, name.length() - suffix.length()); return name.substring(0, name.length() - suffix.length());
} }