Java Annotation Default Properties

What are the exact default values ​​for the two meta annotations (Target and Retention) in a user annotation?

public @interface AnnotationWithDefaultProps { } 
+4
source share
2 answers

According to the source code, none of them have a default value, which means you have to provide it whenever you use annotation. The meaning of the missing annotation is defined in javadoc:

For Target this means

If the target meta annotation is not present in the annotation type declaration, the declared type can be used for any program element.

and for Retention it means

If the annotation type declaration does not have a Retention annotation, the retention policy defaults to RetentionPolicy.CLASS.

+7
source

Strictly speaking, default values ​​are not specified for annotations. There are simply no annotations.

But for these two, in particular,

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/Retention.html says

If the annotation type declaration does not have a Retention annotation, the retention policy defaults to RetentionPolicy.CLASS.

and equivalent for the purpose,

If the target meta annotation is not present in the annotation type declaration, the declared type can be used for any program element.

+2
source

Source: https://habr.com/ru/post/1339906/


All Articles