• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Annotation Handler in Java

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic