learn java5 annotation

Java Annotation Facility:

    1.a syntax for declaring annotation types

    2.a syntax for annotating declarations

    3.APIs for reading annotations

    4.a class file representation for annotations

    5.an annotation processing tool

   

   

      @Retention(RetentionPolicy.RUNTIME)
	  @Target(ElementType.METHOD)
	  public @interface Test { }
	

   

Annotation Declaration

    1.Annotation type declarations are similar to normal interface declarations   

    2.An at-sign (@) precedes the interface keyword.

    3.Each method declaration defines an element of the annotation type.

    4.Method declarations must not have any parameters or a throws clause.

    5.Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types.

    6.Methods can have default values.

   

Annotating Declarations

	   @Test 
	   public static void m5() { }
	  

Annotation Processing Tool

    You can write code to process the declarations where declared.

   

    	 for (Method m : Class.forName(object).getMethods()) {
         if (m.isAnnotationPresent(Test.class)) {               
             Test a = f.getAnnotation(Test.class);
			 System.out.println(m.getName() +" is annotated with value of " +  a.value());
		 }
      }
    

   

  

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.