I want run some code before a method is executed at run-time and I want to achieve it using annotation. I have an annotation @SetValues
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface SetValues
{}
I am new to annotations and have couple of questions
There are several ways this can be done:
But I'm not quite sure as to what you exactly it is you want. The name "SetValues" seems to imply that you want to change the value of certain fields (and maybe restore them after the execution of the method). You have to realise that the InvocationHandler can only do so much. It cannot and should not break encapsulation for example (at least not any more so than any other piece of code can do with the help of reflection). Another point to consider with such a feature is that it may interact very badly with concurrent execution of such methods when the InvocationHandler attempts to change values that both methods need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.