How to use java annotation at runtime

by Shell RS, blogs at Instant Kick

Imagine, you need to capture the information at runtime and you are looking for best feature that serve your purpose. I’ll show you how it can be achieved.

Java Annotation is very useful because it can perform many useful tasks such as

1) It can be used by the compiler to detect errors or suppress warnings,

2) Annotation information can be used to generate code, XML files etc.

3) Some annotations are also available to be evaluated at runtime that’s what you are looking for.

Lets see how annotation can be used to capture runtime information. First step is to create the annotation type.

In Listing-1,

Line 1 @Target(ElementType.TYPE) indicates that annotation can be used on type such as class or interface .

line 2 @Retention(RetentionPolicy.RUNTIME) suggests that annotation information should be available in JVM and can be accessed via reflection. Here, runTimeInfo() method is declared.

Listing-1

@Target( ElementType.TYPE )
@Retention( RetentionPolicy.RUNTIME )
@interface InfoAnnotation {
    String runTimeInfo();
}

Listing-2 displays the code for the class we would like to monitor on runtime. This class implements the annotation created above. Here, we are overriding the method runTimeInfo() that will display the time taken by the instance to be constructed. annotationType() method returns the annotation class.

Listing-2

class RunTimeAnnotationExample implements InfoAnnotation {

    public RunTimeAnnotationExample() {
			//some code to process info
    }

    @Overrides
    public String runTimeInfo() {
        long millis = System.currentTimeMillis();
        new RunTimeAnnotationExample();
        millis = System.currentTimeMillis() - millis;
        return "This is a RunTimeAnnotation class, running on "
                + System.getProperty("os.name")
                + ". Total time taken by constructor "
                + millis + " milliseconds.";
    }

    @Override
    public Class annotationType() {
        return InfoAnnotation.class;
    }

}

You are ready with your annotation code and annotation implementation. Now, you need to utility to extract the runtime information out of these.

Listing-3, displays utility code. It is straight forward, you pass the annotation implementation class to utility constructor InfoConsoleWriter() that implicitly first create the list of such objects (if you are capturing runtime information for multiple classess) and then calls the printInformation() method, ultimately runTimeInfo() gets call.

Listing-3

/* import java.lang.annotation.* or other classes */
public class InfoConsoleWriter {

    public InfoConsoleWriter(Class... objInfoClassPrinter) throws Exception {
        List listOfInfoAnnotation = getDocumentAnnotations(objInfoClassPrinter);
        printInformation(listOfInfoAnnotation);
    }

    private List getDocumentAnnotations(Class... objInfoClassPrinter) throws Exception {
        List returnedList = new ArrayList();
        for (Class c : objInfoClassPrinter)
            if (c.isAnnotationPresent(InfoAnnotation.class))
                returnedList.add(c.getAnnotation(InfoAnnotation.class));
            else if (Arrays.asList(c.getInterfaces()).contains(InfoAnnotation.class))
                returnedList.add((InfoAnnotation) c.newInstance());
        return returnedList;
    }

    private void printInformation(List objectList) {
        for (InfoAnnotation object : objectList)
            System.out.println(object.runTimeInfo());
    }

	public static void main(String[] args) throws Exception {
        new InfoConsoleWriter(RunTimeAnnotationExample.class);
    }

}

Output:
This is a RunTimeAnnotation class, running on Windows 2008. Total time taken by constructor 26 milliseconds.

Similarly, you can retrieve different information at runtime.

Leave A Comment

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

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