Java Annotations in Short

A short explanation and usage of Annotations

General rules:

  • @ sign at the front of interface keyword
  • Method declarations defines annotation elements
  • Return types are: String, Enum, Class, Annotation, primitive and Arrays of these types
  • Methods can have default falues

 

 

public @interface myAnnotation {
String name();
int number();
String description() default "default description";
}

usage :

@myAnnotation(name="test", number=1)
class myClass {}

Annotation sorts:

1. Marker annotation : these type annotations have no elements.

Example:
declaration:

public @interface Simple{}

usage:

@Simple
public class myClass{}

2. Single member annotation: these type annotations have only one element.

The element name should be “value”. An other name can be used but if “value” is used as
element name, the name can be omit when using the annotation.
Example:
declaration:

public @interface Single{ String value();}

usage:

@Single("mySingleAnnotation");
/* also possible: "@Single(value="mySingleAnnotation")" but using "value=" not needed. */
public class myClass{} public @interface Single{ String name();}

usage:

@Single(name="mySingleAnnotation")
/* "name=" is mandatory because the element name is not "value". */
public class myClass{}

3. Multimember annotation: these type annotations have more than 1 element.

Example:
declaration:

public @interface Multi{ String value(); String description();}

usage:

@Multi(value="mySingleAnnotation", description"myMultiAnnotation description") // using "value=" is mandatory in multi valued annotations.
public class myClass{}

4. Nested annotation: These are annotations that are used in de declaration of other annotations.

Example:

public @interface Trace(String value();)
public @interface Maintenance(Trace trace();)

Usage:

@Maintenance(trace=@Trace("tracevalue"))
public class myClass

Special Annotations:

1. @Target:

– Restrichts the usage of the annotation on which it is used
– Possible Enum types: TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE

2. @Retention:

– Declares how long the annotation will be kept in the code.
– Possible Enum types:

  • SOURCE: the annotation will be available only in the compile phase and won’t be placed in the generated byte code.
  • CLASS: Default. The annotation will be put in the generated .class file by the compiler.
  • RUNTIME: Annotation will be available in the runtime. These annotations can be queried using Reflection API.

Example:
declaration:

public @interface Simple{}
public @interface Multi{ String value(); String description();}

usage:

@Simple
public class myClass{
@Multi(value="mySingleAnnotation", description"myMultiAnnotation description")
public myMethod(){}
}
String value = MyClass.class.getMethod("myMethod").getAnnotation(Multi.class).value();

3. @Nonbinding:

РExcludes a member of an annotation type from consideration when the container compares two annotation instances.

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