Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

java - Is it possible with ByteBuddy to intercept a method that is declared by an interface with an annotation?

I need to instrument all methods, which implement an interface that itself is annotated. Consider such an example:

@WebService
interface HelloWorld {
  void hello();
}
class HelloWorldImpl implements HelloWorld {
  void hello() {...}
  void goodBye() {...}
}

Here I need to have an advice around hello, because it is declared by an interface with @WebService annotation. Is it possible to build such a ButeBuddy matcher?

question from:https://stackoverflow.com/questions/65905221/is-it-possible-with-bytebuddy-to-intercept-a-method-that-is-declared-by-an-inter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, in the ElementMatchers class, you find a long row of predefined matchers, also such that deal with annotations. Do however know that annotations on interfaces are not inherited. You'd need to wrap the matcher in a ElementMatchers.hasSuperType to check the entire hierarchy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...