Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Create your own Java customized annotations

Posted on 29/12/2009

Annotations is a relatively new technology, it was added to the language in Java5.

An annotation is a meta data that can be attached to a java class, member, function or a parameter.

It gives information about the element that is annotated.

An example is the Deprecated annotation that specifies this function is old and better not used.

@Deprecated
public void oldFunction(){

}

Java5 comes with a set of annotations but you can also can create your own.

In this post I will show you how to create your own annotations, use them and also refer to them during runtime.

Steps for creating customized annotations:

1. Creating the annotation interface

First you create the annotation interface which has specific structure.

public @interface InvokeMultiple {
	int numberOfTimesToInvoke();
}

In this example the name of the annotation is ‘InvokeMultiple’ and it has 1 field ‘numberOfTimesToInvoke’.

Meta-Annotations
Meta annotations are actually annotations being used by annotations.

  • @Target – specifies the type pf element this annotation is attached to.
    • ElementType.TYPE-can be applied to any element of a class
    • ElementType.FIELD-can be applied to a field or property
    • ElementType.METHOD-can be applied to a method level annotation
    • ElementType.PARAMETER-can be applied to the parameters of a method
    • ElementType.CONSTRUCTOR-can be applied to constructors
    • ElementType.LOCAL_VARIABLE-can be applied to local variables
    • ElementType.ANNOTATION_TYPE-indicates that the declared type itself is an annotation type
  • @Retention – specifies the retention level of this annotation.
    • RetentionPolicy.SOURCE—Retained only at the source level and will be ignored by the compiler
    • RetentionPolicy.CLASS—Retained by the compiler at compile time, but will be ignored by the VM
    • RetentionPolicy.RUNTIME—Retained by the VM so they can be read only at run-time
  • @Documented – by default annotations are mentioned in java doc, this meta-annotation will make this annotation to be mentioned.
  • @Inherited – Indicates that the annotation will automatically be inherited (take a look at the example attached to this post).

Example:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InvokeMultiple {
	int numberOfTimesToInvoke();
}

default values
It is also possible to give annotation fields default value.

Example:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InvokeMultiple {
	int numberOfTimesToInvoke() default 1;
}

2. Using the annotation

Using the annotation is done by attaching the annotation name to the element(class, method, parameter…).
A field value is given in this format (fieldName1=FieldValue1, fieldName2=FieldValue2 ).

Example:

public class TestObject {

	private String firstName;
	private String lastName;
	
	public TestObject(String firstName,
				String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	
	@InvokeMultiple(numberOfTimesToInvoke=3)
	public void printFirstName(){
		System.out.println(firstName);
	}
	
	@InvokeMultiple(numberOfTimesToInvoke=6)
	public void printLastName(){
		System.out.println(lastName);
	}

	@InvokeMultiple
	public void printMessage(){
		System.out.println("printed only once");
	}
	
	public void printSecret(){
		System.out.println("this will not be printed");
	}	
}

Notice that the function ‘printMessage()’ is using the default value of the annotation field.

3. Referring to the annotation during runtime

Reading the annotation is done by reflection, but only if your annotation has a retention of RUNTIME.
You can check which annotation an element (class, method, parameter…) is using. And you can also find out the value of the fields of the annotations.

Example:

	public static void invokeThis(Object theObject){
		try {
			Method [] methods = Class.forName(theObject.getClass().getName()).getMethods();
			
			for (int i = 0; i < methods.length; i++) {
				InvokeMultiple invokeMultiple = methods[i].getAnnotation(InvokeMultiple.class);
				if(invokeMultiple != null){
					int numberOfTimesToInvoke = invokeMultiple.numberOfTimesToInvoke();
					for (int j = 0; j < numberOfTimesToInvoke; j++) {
						methods[i].invoke(theObject, null);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

This example above is of a method that receives an object.
By using reflection it goes over all object’s methods and checks if it’s annotated with ‘InvokeMultiple’.
If so, it will invoke that method a number of times according to the value of the annotation field ‘numberOfTimesToInvoke’.

This will not work if your annotation had a Retention other than RUNTIME.

download download example

7 thoughts on “Create your own Java customized annotations”

  1. jags says:
    29/11/2012 at 10:22

    nice post!! thanks

    Reply
  2. kumar says:
    29/01/2013 at 14:58

    Neatly explained !! Thanks

    Reply
  3. Venkat says:
    17/03/2013 at 16:36

    Good example

    Reply
  4. Jonhy Pacheco says:
    08/05/2013 at 22:31

    Short and clear.

    Reply
  5. Nagi says:
    26/12/2013 at 13:13

    Nice and clear

    Reply
  6. Hidayath says:
    18/03/2014 at 15:49

    Very good example…….. Thank you.

    Reply
  7. Dom says:
    23/07/2014 at 03:42

    Again to reiterate with everyone else, very short and clear example. Thanks.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme