Sunday 8 January 2012

How to define an annotation in java?


Just like creating a java interface, create f.e. a file ExampleAnnotation.java with the following code:

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface ExampleAnnotation
{
// Property Definition here.
}

You have to add the following annotations in the definition:
  • Target - define where annotation can be added (methods, class, fields, properties, constructor, local variable, package, annotation type)
  •  Retention - where annotation will be available (only in source, in class definition, also in runtime)

No comments: