gene b.
gene b.

Reputation: 11974

Configuring Annotations to be RUNTIME Retention By Default (without individual @Retention)

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

Answers (1)

Andreas
Andreas

Reputation: 159086

The Java Language Specification, section 9.6.4.2. @Retention states:

If T does not have a (meta-)annotation m that corresponds to java.lang.annotation.Retention, then a Java compiler must treat T as if it does have such a meta-annotation m with an element whose value is java.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

Related Questions