#Java @nnotations

“Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

Java Tutorial – Oracle (Sun)


public @interface <name>{

<type> <method_name>();

<type> <method_name>() default <value>;


  • @Retention annotation specifies how the marked annotation is stored:
    • RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.
    • RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
    • RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the runtime environment.
  • @Target annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to. A target annotation specifies one of the following element types as its value:
    • ElementType.ANNOTATION_TYPE can be applied to an annotation type.
    • ElementType.CONSTRUCTOR can be applied to a constructor.
    • ElementType.FIELD can be applied to a field or property.
    • ElementType.LOCAL_VARIABLE can be applied to a local variable.
    • ElementType.METHOD can be applied to a method-level annotation.
    • ElementType.PACKAGE can be applied to a package declaration.
    • ElementType.PARAMETER can be applied to the parameters of a method.
    • ElementType.TYPE can be applied to any element of a class.
  • @Inherited annotation indicates that the annotation type can be inherited from the super class. (This is not true by default.) When the user queries the annotation type and the class has no annotation for this type, the class’ superclass is queried for the annotation type. This annotation applies only to class declarations.


We want to create an annotation that allows to mark the fields of a class granting the possibility to tag them with different names (e.g.: we have got the class car with n fields but their names in a JSON that has to be exchanged with the server should be different).

  • Step 1: Creating the Annotation that will be used to mark class fields.
* Annotation used to mark fields that will be evaluated at Runtime.
public @interface Prop {

* Used to change to set up a different name in the result.
String name();


  • Step 2: Mark all the necessary class fields with the just created annotation
* For example we want to map the wheels and engineType fields to n_wheels and engine_type.
public class Car {

@Prop(name = “n_wheels”)
private int wheels;
private String color;
@Prop(name = “engine_type”)
private String engineType;


  • Step 3: Given a car we want to access to its fields and map them into a JSON.
// NB: NPE checks skipped for space reasons
public static String toJSON(final Car car){
final JSONObject res = new JSONObject();
final Field[] fields = obj.getClass().getDeclaredFields();
for (final Field field : fields) {
final Prop prop = field.getAnnotation(Prop.class);
// Explained after the toJSON(Car car) method
final Object value = ReflectionUtil.get(obj, field); 
// Checking if the field has a Prop
if (prop != null) {
res.put(prop.name(), value);
} else {
res.put(field.getName(),  value);
return res.toString();

  • Reflection utility:
public final class ReflectionUtil {

public static <T> T get(Object parent, Field field) {
if (field == null) {
return null;
Object value = null;
try {
// Checking the accessibility of the field
boolean accessible = field.isAccessible();
if (!accessible) {
value = field.get(parent);
} else {
value = field.get(parent);
} catch (Exception e) {
return (T) value;


About alchemiasoft

Android Developer at Bloomberg LP. Algorithms and performance passionate. Curious. Eager to learn.

Posted on June 23, 2013, in Development, Java and tagged , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

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 )

Google+ photo

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


Connecting to %s

%d bloggers like this: