Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 910
user219882 Avatar asked Feb 12 '12 16:02

user219882


People also ask

What is the @override annotation used for?

@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance. While it is not required to use this annotation when overriding a method, it helps to prevent errors.

How do you annotate a method in Java?

For an annotation to be repeatable it must be annotated with the @Repeatable annotation, which is defined in the java. lang. annotation package. Its value field specifies the container type for the repeatable annotation.

What is a method annotation?

An annotation is a construct associated with Java source code elements such as classes, methods, and variables. Annotations provide information to a program at compile time or at runtime based on which the program can take further action.

What is @retention in Java?

java.lang.annotation.Retention. Indicates how long annotations with the annotated type are to be retained. If no Retention annotation is present on an annotation type declaration, the retention policy defaults to RetentionPolicy.


1 Answers

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.

like image 109
javamonkey79 Avatar answered Sep 22 '22 12:09

javamonkey79