Retention Policy in Java Annotations

Something I keep forgetting, which I’m recording here so I can remember in the future.

When creating a new annotation which is to be parsed during execution with Java Reflection, you must change the retention policy with the @Retention annotation.

So, for example:

@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation {
...
}

This will keep the annotation around when checking annotations at runtime.

For whatever reason I keep forgetting this.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s