Reputation: 15834
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 annotate methods like @Checked
which would cause to call the check()
at the end? And if it is, can you provide some examples?
Upvotes: 6
Views: 14614
Reputation: 17755
Yeah - it is possible. You need to instrument your code, typically with aspects (AOP). Check out this example if you want to see what it looks like.
Upvotes: 6
Reputation: 115328
It can be done with AOP. Take a look on AspectJ and Dynamic Proxy. Using dynamic proxy you can wrap your class with yet another piece of code that perform some things before and after actual method call.
Upvotes: 3