Skip to content

Custom annotation

April 15, 2010

Declarate,decorate and use them.

public @interface Helper{
String job();


The annotation created has a location limitation that can be set using the @Target [ElementType] and also the lifetime usage period that can be set using @Retention [RetentionPolicy] . The annotation can exist at the time of source, until compilation or at runtime.can mention the default value

Return types of the method should be one of the following:
– primitives,- String,- Class,enum,array of the above types

@Helper(“veg”) – if only 1 member variable, otherwise simple @Helper if marker interface.

Processing annotation: Runtime: – Use the new AnnotatedElement and Annotation interfaces added to the java reflection API to detect the presence of your annotation at run-time
-Every annotation type automatically implements the java.lang.annotation.Annotation interface which happens in the background when the @interface keyword is used to declare a new annotation type

CompileTime: Use the Annotation Processing Tool (apt) to make your own AnnotationProcessor/AbstractProcessor? and print the message with javax.annotation.processing.Messager

Custom Annotation

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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

%d bloggers like this: