January 21, 2021
Hot Topics:

Make Your Code Work for You with Java Annotations

  • By David Thurmond
  • Send Email »
  • More Articles »

Profiler.java accepts two parameters: a switch to indicate whether memory, execution time, or both are to be profiled; and the class name whose methods are to be profiled.

First, in the profiler's constructor, the profiler creates an instance of the class specified on the command line so that methods can be invoked on it. Next, the profiler obtains the class's methods through reflection:

Method [] methods = theInstance.getClass().getDeclaredMethods();

Next, the profiler cycles through all of the class's methods. For each method, the profiler checks to see if the @MemoryCheck or @ExecTime annotations were present, and then invokes the method:

if (theMemCheckAnnotation != null || theExecTimeAnnotation != null) {
   System.out.println("Profiling " + theMethod.getName());
   theMethod.invoke(theInstance, null);
}    // if

Note that the method's getAnnotation() method will return null if the specified annotation class was not present on the method declaration. If either the @ExecTime or @MemoryCheck annotation is not null, the method is invoked so that the before and after profile values can be computed.

  • If the @ExecTime annotation is present, the profiler notes the time in milliseconds before the method invocation, the time in milliseconds afterward, and calculates how many milliseconds execution took.
  • If the @MemoryCheck annotation is present, the profiler notes the free memory before the method invocation, the free memory afterwards, and calculates how much memory was used. Depending on the parameter value on the @MemoryCheck annotation, the memory used is shown in bytes, KB, or MB.

This profiler is only a very simple example of how to use annotations at run time, and it could certainly be improved. For one thing, the careful observer will note that it is impossible to profile a method that has any parameters at present. However, you can see that it is not difficult to access the annotations that were present in the source code by using reflection. The possibilities for the tools you can create are limited only by your imagination.


Annotations used at coding time can reduce repetitive coding tasks dramatically. When used at run time, they provide a way for meta-tools to access important information provided in your code. Hopefully, I have sparked your imagination and encouraged you to use this powerful Java language feature to make your code work hard for you.

Download the Code

The complete code from this article can be downloaded here.

About the Author

David Thurmond is a Sun Certified Developer with over fifteen years of software development experience. He has worked in the agriculture, construction equipment, financial, home improvement, and logistics industries.

Page 4 of 4

This article was originally published on December 15, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date