Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to annotate a code block in Java

Is it possible to annotate a block of code? E.g. for cycle or simply curly brackets? If so, how?

First.java

package An;
import An.ForCycle;

class First {
    public static void main(String[] args) {
        First f = new First();
    }

    public First () {

        @ForCycle
        {   // error: illegal start of type {
            int k;
        }

        @ForCycle
        for (int i = 0; i < 5; i++) {   // similar error (illegal start...)
            System.out.println(i);
        }
    }
}

ForCycle.java

package An;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.SOURCE)
public @interface ForCycle {}

According to http://www.javacodegeeks.com/2012/11/java-annotations-tutorial-with-custom-annotation.html

@Target – indicates the kinds of program element to which an annotation type is applicable. Some possible values are TYPE, METHOD, CONSTRUCTOR, FIELD etc. If Target meta-annotation is not present, then annotation can be used on any program element.

Any program element (I guess) means also block, doesn't it? So why I can't annotate block or for? What am I missing?

Thanks for help

like image 941
petrbel Avatar asked Jun 15 '14 12:06

petrbel


People also ask

Can we annotate interface in Java?

Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.

What does annotate mean in Java?

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files.

Can we annotate array object Java?

Java developers typically need to store objects of a given type in a data structure such as an array. An efficient technique to achieve this goal is available in Java 8 by adding type and repeating annotations to your Java application.


1 Answers

No, you cannot do this. Actually you have already answered your question yourself. There is a close list of targets where annotation can be used: {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}. As you can see there is not such thing as "block". Why? Probably because annotation must be attached to something named, e.g. method, field, class etc.

Indeed think about how can use use the annotation? Annotations are accessed using reflection API. You can request annotations of specific method or field. How are you expecting to request annotation of block?

And the last notice. Annotations can be used at runtime or at compile time. Annotations available at compile time are used by compilers, IDEs and annotation processors to generate additional warnings, errors or code. IntelliJ as a well-known IDE with bunch of static code analysis features supports so called "block annotations" that syntactically are regular inline comments but are treated by IDE as annotations that can suppress some warnings.

like image 84
AlexR Avatar answered Sep 19 '22 19:09

AlexR