Closed zcfrank1st closed 8 years ago
You specify:
.type(isAnnotatedWith(Log.class))
.transform((builder, typeDescription) -> builder.method(any())
.intercept(MethodDelegation.to(LogInterceptor.class)
.andThen(SuperMethodCall.INSTANCE)))
What you are saying is that you want to intercept any type that is annotated with @Log
. However, only your method is annotated with this annotation. Try declaresMethod(isAnnotatedWith(Log.class))
or any()
.
Also, make sure that this is what you want. Currently, you intercept any method if it is inherited or not. Consider adding isDeclaredBy(typeDescription)
to your method matching.
Byte Buddy builds a standard Java agent. The official documentation is rather short but contains all relevant information.
Thanks, but I tried
.type(declaresMethod(isAnnotatedWith(Log.class)))
.transform((builder, typeDescription) -> builder
.method(isDeclaredBy(any()))
.intercept(MethodDelegation.to(LogInterceptor.class).andThen(SuperMethodCall.INSTANCE)))
and run java -javaagent:logger-1.0-SNAPSHOT.jar -cp /Users/zcfrank1st/Desktop/git-svn/agent-demo/target/agent-demo-1.0-SNAPSHOT.jar Main
the terminal prints None of [] allows for delegation from void Main.hello() java.lang.IllegalArgumentException: None of [] allows for delegation from void Main.hello()
Your
static void interceptor(@Origin Method method)
method is package-private. Is this method visible to the Hello
class? If not, change the visibility of the static method to public
or change the package of the interceptor class and it should work.
:+1: it worked ! Thanks a lot ~
I try to use agent builder to write a java agent to log logs, but it does not work and there are no errors outputing, my main agent code is:
and my demo code is:
the intercepter is:
how to fix that ? BTW, are there any resources can help me learn bytebuddy deeply ? Thank you ! :laughing: