File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Annotation Handler in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Annotation Handler in Java" Watch "Annotation Handler in Java" New topic

Annotation Handler in Java

PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
Hi Everyone,

I have written a custom annotation which i have included below

I wrote a separate class while uses the above annotation. This class uses two more annotations defined by me, but i have not included them since they have the same structure as the above one except that they are for methods and instance variables.

I have also written a main Class described below

What happens is that my Main class calls the Handler class which handles the annotation in the AnnotationUser class. I have included the handler class below

My concern is this, I have called the Handler class to handle the annotations used by the AnnotationUser from my Main. The point to note here is this: My handler class has been hardcoded with the Class name that i must check for annotations. I may look it up from a xml property file but i believe it negates the entire use of annotations.What i mean is, i am working on a big project and i have defined custom annotations to be used by my team members. How to automate the handling of those custom annotations that are in my source code(without the program calling the handler and the handler knowing beforehand the classes which uses the annotations)?Is it possible to add these Custom Annotations and their Handlers(defined by me) to the Compiler so that when i compile i dont have to specifically call the handler , rather the compiler will check the files for annotations and call the appropriate annotationhandler(Ex:suppresswarning annotation).

Again sorry for the big thread, just wanted to make myself clear.

To err is human,
To forgive is not company policy
Lester Burnham

Joined: Oct 14, 2008
Posts: 1337
One technique I've seen is that at application startup time the entire contents of the classpath are scanned -every single class- and checked whether they do or do not have one of the relevant annotations. That has some obvious overhead, but for a long-running server application an additional 10 seconds at startup (or whatever it turns out to be) may be negligible.

The process can be short-circuited by checking *where* a class lives; for example, if it's part of the JRE installation, then there's no need to check it for your annotations.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

PrasannaKumar Sathiyanantham wrote:

I really hope you never use annotations like @Override, @Deprecated, etc, because those will lead to nasty ClassCastExceptions.

Instead of getAnnotations() you want to call getAnnotation(ClassAnnotation.class), getAnnotation(MethodAnnotation.class) and getAnnotation(FieldAnnotation.class).

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Annotation Handler in Java
It's not a secret anymore!