Support logging interceptions

This commit is contained in:
Andrey Breslav
2013-05-12 10:47:58 +02:00
parent 6c60383978
commit c960dff166
4 changed files with 20 additions and 4 deletions
@@ -98,8 +98,8 @@ public class InterceptionInstrumenter implements Instrumenter {
compilePattern(annotation.erasedSignature()),
annotation.allowMultipleMatches(),
enterData,
exitData
);
exitData,
annotation.logInterceptions());
instrumenters.add(instrumenter);
neverMatchedInstrumenters.add(instrumenter);
}
@@ -205,6 +205,7 @@ public class InterceptionInstrumenter implements Instrumenter {
for (MethodInstrumenter instrumenter : instrumenters) {
if (instrumenter.isApplicable(name, desc)) {
applicableInstrumenters.add(instrumenter);
instrumenter.reportApplication(cr.getClassName(), name, desc);
checkMultipleMatches(instrumenter, name, desc);
neverMatchedInstrumenters.remove(instrumenter);
@@ -26,4 +26,6 @@ interface MethodInstrumenter {
List<MethodData> getEnterData();
boolean allowsMultipleMatches();
void reportApplication(String className, String methodName, String methodDesc);
}
@@ -25,19 +25,22 @@ class MethodInstrumenterImpl implements MethodInstrumenter {
private final boolean allowMultipleMatches;
private final List<MethodData> enterData;
private final List<MethodData> exitData;
private final boolean logApplications;
public MethodInstrumenterImpl(
Pattern namePattern,
Pattern descPattern,
boolean allowMultipleMatches,
List<MethodData> enterData,
List<MethodData> exitData
List<MethodData> exitData,
boolean logApplications
) {
this.namePattern = namePattern;
this.descPattern = descPattern;
this.allowMultipleMatches = allowMultipleMatches;
this.enterData = enterData;
this.exitData = exitData;
this.logApplications = logApplications;
}
@Override
@@ -45,6 +48,13 @@ class MethodInstrumenterImpl implements MethodInstrumenter {
return allowMultipleMatches;
}
@Override
public void reportApplication(String className, String methodName, String methodDesc) {
if (logApplications) {
System.out.println(toString() + " applied to " + className + ":" + methodName + methodDesc);
}
}
@Override
public boolean isApplicable(String name, String desc) {
return namePattern.matcher(name).matches() && descPattern.matcher(desc).matches();
@@ -62,6 +72,6 @@ class MethodInstrumenterImpl implements MethodInstrumenter {
@Override
public String toString() {
return namePattern + " " + descPattern + (allowMultipleMatches ? "[multiple]" : "");
return namePattern + " " + descPattern + (allowMultipleMatches ? " [multiple]" : "");
}
}
@@ -35,4 +35,7 @@ public @interface MethodInterceptor {
// if this is false, an exception is thrown when more than one method in the same class matches
boolean allowMultipleMatches() default false;
// if true, every method instrumented with this interceptor will be logged to stdout
boolean logInterceptions() default false;
}