Reputation: 11974
Is it possible to have a configuration where all Annotations of a given name or type are automatically RetentionPolicy.RUNTIME
?
I came across a poblem where I need to use reflection at runtime to search for certain Annotations. But those are common Annotations scattered all over the app, such as @Service
or @Transactional
, and I can't go one by one and add @RetentionPolicy
to each one, it would be too long.
Upvotes: 1
Views: 483
Reputation: 159086
The Java Language Specification, section 9.6.4.2. @Retention states:
If T does not have a (meta-)annotation
m
that corresponds tojava.lang.annotation.Retention
, then a Java compiler must treat T as if it does have such a meta-annotationm
with an element whose value isjava.lang.annotation.RetentionPolicy.CLASS
.
As you can see, it is a strict requirement that missing annotation is same as @Retention(RetentionPolicy.CLASS)
.
If you need @Retention(RetentionPolicy.RUNTIME)
, then you must explicitly specify that.
Upvotes: 2