Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with custom annotations

 
Ayan mallick
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ayan mallick
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic