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().
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
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.
Joined: Nov 17, 2012
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