user219882
user219882

Reputation: 15834

Annotation which invokes 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 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

Answers (2)

javamonkey79
javamonkey79

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

AlexR
AlexR

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

Related Questions