Annotations that call a method

I'll start with a piece of code

class Clazz { public void doSomething() { ... check(); } public void doSomethingElse() { ... check(); } ... // etc., these methods look basically the same - they all call check() at the end } 

Is it possible to comment on methods like @Checked that will invoke check() at the end? And if so, can you give some examples?

+6
source share
2 answers

Yes it is possible. You need to customize the code, usually with aspects (AOP). Check out this example if you want to see what it looks like.

+6
source

This can be done using AOP. Take a look at AspectJ and Dynamic Proxy. Using a dynamic proxy, you can wrap your class with another piece of code that performs some actions before and after calling the actual method.

+3
source

Source: https://habr.com/ru/post/908264/


All Articles