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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With