开发者

How to do an inter-type injection on classes that have methods with a certain annotation?

To give you some context: I am successfully using aspectJ with android projects, and I've written a @Background annotation that I apply to methods that cannot block the main UI thread. This annotation is being intercepted by a pointcut and an around advice that takes care of running the code in a background thread and dealing with network errors开发者_JAVA百科 and user notification.

What I would like to do next is to automatically provide a reload button on the menu to all activities that have such background methods This has to be done by overriding a method with the following signature: public boolean onCreateOptionsMenu(Menu menu);

Activities that don't do background stuff don't need to have menu, and thus, don't override that method.

So, is there a way to make aspectJ override a method in just those classes that have (at least) a method with a particular annotation ?

Or is there a more elegant solution ?

Thanks, Carlos.


Take a look at hasMethod type pattern (you can see the bug report for usages). You should be able to add a marker interface to your types that have the method(s) with the required signature and then introduce onCreateOptionsMenu to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜