Blogs at Instant Kick
Imagine, need to capture the information at runtime and your is looking for best feature that serve your purpose. I'll show you what 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 is also available to being evaluated at runtime this ' s what is looking for.
Lets See how annotation can is used to capture runtime information. First step is to create the annotation type.
In Listing-1,
Line 1 @Target (elementtype.type) indicates this annotation can be used on TYPE such as class or interface.
Line 2 @Retention (retentionpolicy.runtime) suggests, annotation information should be available in JVM and can be acce Ssed via Reflection. Here, Runtimeinfo () method is declared.
Listing-1
12345 |
@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 is overriding the method Runtimeinfo () that would display the time taken by the instance to be constructed. Annotationtype () method returns the annotation class.
Listing-2
1234567891011121314151617181920212223 |
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 was a runtimeannotation class, running on" + System. GetProperty("Os.name") + ". Total time taken by constructor " + millis + "milliseconds." ; } @Override Public Class annotationtype() { return infoannotation. Class; }} |
You is 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's straight forward, you pass the annotation implementation class to utility constructor Infoconsolewriter () that impli citly first create the list of such objects (if you is capturing runtime information for multiple classess) and then call s The Printinformation () method, ultimately runtimeinfo () gets call.
Listing-3
12345678910111213141516171819202122232425262728 |
/ * Import java.lang.annotation.* or other classes * /public class infoconsolewriter { public infoconsolewriter (class.< Span class= "Crayon-sy". objinfoclassprinter) throws exception Span class= "Crayon-sy" >{ List listofinfoannotation = getdocumentannotations(objinfoclassprinter) ; printinformation(listofinfoannotation); } private list getdocumentannotations ( Class. objinfoclassprinter) throws exception Span class= "Crayon-sy" >{ List returnedlist = new ArrayList(); For (Class c : objinfoclassprinter) /c20> if (cisannotationpresent (InfoAnnotationclass) ) 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) /c19> System. Out. println(object. Runtimeinfo()); } public static void main (string[] args) throws exception Span class= "Crayon-sy" >{ New infoconsolewriter(runtimeannotationexample. Class); }} |
Output:
This was a runtimeannotation class, running on Windows 2008. Total time taken by constructor-milliseconds.
Similarly, you can retrieve different information at runtime.
How to use the Java annotation at runtime