aspose file tools*
The moose likes Java in General and the fly likes problem with custom annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem with custom annotations" Watch "problem with custom annotations" New topic
Author

problem with custom annotations

Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 46





now my problem is i don't want to get a particular annotation or all annotations against a particular class(like here we are providing class object) rather i want want to get all classes in the class path which are annotated with that particular annotation.Then from them based on the parameter value i will find a specific class and i will instantiate that class in runtime.

trying to get an idea how to move from configuration file to annotation where i have to get fully qualified class name or Class object at runtime based on annotation so that i can instantiate that bean and populate.
like JSF @ManagedBean().

please give me some idea on how i can proceed.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2434
    
  28

No there is no direct way to get all classes annottated with an annotation. However there are different strategies that you can use

A) if you already have a factory or a DI framework that instantiates the classes, you can have that framework scan the class that its instantiating.
B) you can scan all the classes loaded by class loader and look for a particular annotation. If you are using spring, spring provides a class for doing this
C) there is nothing stopping you from processing runtime annotations during compile time. At compile time you can generate a property file containing the list of classes to scan. You can use this during runtime
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

Luckily, there are libraries that can help you. There are Scannotations and the Reflections library. I only have experience with the latter, but it works fine for me.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 46
Thanks a lot to both.@Jan Hoppmann @Jayesh A Lalwani

I have found one way to do it using libraries
Here is my modified implementation.













But one confusion regarding this line Reflections reflections = new Reflections("org.example.annotation"); say if i have the frame work and someone else developing classes using that framework then how would i know
runtime the package prefix.Is there anyway to know the package structure runtime.Again looking for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with custom annotations