Custom Annotation using JAVA Annotation

Annotations

–          To embed meta-data in source file. (data about data is called as meta-data)

User for:

  1. Code Analysis
  2. Compiler checking
  3. Documentation

 Program that demonstrates the custom annotation and how to find the annotation types associated with a method.

import java.lang.annotation.*;

import java.lang.reflect.*;

 

/*

 * My custom annotation ‘Complete’

 */

@Retention(RetentionPolicy.RUNTIME)

@Target (ElementType.METHOD)

@interface Complete {

      int percentage() default 100;

      boolean isComplete() default false;

}

 

/*

 * My custom annotation ‘Author’

 */

@Retention(RetentionPolicy.RUNTIME)

@Target (ElementType.METHOD)

@interface Author {

      String name() default “SARAVANAN SIVAJI”;

}

 

/*

 * My custom annotation ‘Dummy’. Discarded by Compiler.

 */

@Retention(RetentionPolicy.SOURCE)

@Target (ElementType.METHOD)

@interface Dummy {

      String name() default “SARAVANAN SIVAJI”;

}

 

/*

 * This class finds annotation type associated with a method display(), at runtime using Reflection.

 */

public class CustomAnnotation {

      //Method annotated with ‘Author’, ‘Dummy’ and ‘Complete’ annotation type     

      @Author

      @Dummy

      @Complete (percentage=200, isComplete=true)

      public void display() {      

      }

      public static void main(String [] args) {

            try {

                  // Use reflection to retrieve a reference to the specified methods

                  Method method = CustomAnnotation.class.getMethod(“display”,null);

                  // Check which methods, if any, are tagged by Complete

                  boolean yes = method.isAnnotationPresent(Complete.class);

                  if(yes){

                        Annotation ann = method.getAnnotation(Complete.class);

                        //Print the members of this annotation

                        System.out.println(“The percentage attribute of Complete annotation on ” + method.getName() + ” method is ” + ((Complete)ann).percentage());

                        System.out.println(“The isComplete attribute of Complete annotation on ” + method.getName() + ” method is ” + ((Complete)ann).isComplete());

                        // Print all annotations against this method

                        printAnnotations(method);

 

                  }

            }

            catch(NoSuchMethodException cnse) {

                  System.out.println(cnse);

            }

      }

      static public void printAnnotations(AnnotatedElement e) {

            //Following line prints method signature

            System.out.println(“Printing annotations for method “+ e.toString());

            //Fetch all the annotation type associated with this method

            Annotation[] annotations = e.getAnnotations();

            for (Annotation a : annotations) {

                  System.out.println(“Annotation found: ” + a.annotationType().getName());

            }

      }

 

 

}

 

Output:

The percentage attribute of Complete annotation on display method is 200

The isComplete attribute of Complete annotation on display method is true

Printing annotations for method public void CustomAnnotation.display()

Annotation found: Author

Annotation found:Complete

 

 

 

 

 

Advertisements

Leave a Reply

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

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

%d bloggers like this: