• Post Reply Bookmark Topic Watch Topic
  • New Topic

Load all classes in a package  RSS feed

 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

While working on a framework, I have to build an annotation processor that basically traverses through any package passed in, to load all the classes within the package that carry annotation say @X, I am looking for suggestions on how to do this ... any ideas? I will be sure to add your name, the link on this thread on the code that I check in ...

I just need some basic ideas ... i can work on developing my own code alright ...

Regards
 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh and actually the way i do it right now is use the current directory to traverse through the package names, and build a string (using stringbuilder) ... with this order:

x
x.y
x.y.z
x.y.z.u
x.y.z.u.* // this is where i go through the entire directory and then finally use the file name to load the class ... something like:

Thread.currentThread().getContextClassLoader().loadClass("x.y.z.u." + currentFileName);

is this the only way?, it sure is ugly! =( I wish there was a runtime construct in java for reflecting on packages ...


Regards
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem you are going to encounter is that when talking about 'classes in a package', the visible classes in that package may be in several locations.
For example you could have a runtime classpath that contains two directories and two JAR files, and each of these could define some but not all of the classes in the package, so you would need to be able to find all of the files in all of these locations before being able to preload them. Your next problem is that if one code exists remotely (eg using a URLClassLoader) you may be able to find the package not be able to view the files located there.

Having said all of that, you may be able to convert the package to a folder path and use ClassLoader.findResources as a starting point.
 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good points.

I'll try things out this weekend and let ya know (Gosh its been such a crazy week ... deadlines and all); My framework's lagging behind! ... =(


Regards
 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I think thats a basic flaw that no one sees in dependency injection, A chief premise of dependency injection is that there has to be a programmatic or xml config way to hook your classes to be managed; So essentially Convention over configuration principle is defied in that sense.


Re-question:

There's a class called Package in java API (It has some methods like Package.getPackages(), getAnnotation( ... ), isAnnotationPresent( ... ) etc, can i get all packages and then somehow reflectively get all classes annotated with @Component?

 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i take my stmt about DI back, I think thats a flaw in java language, because technically they should have package information available at hand through which i can traverse through all classes and check for things i want to for processing.

 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those interested, after doing some digging in JBoss EJB container implementation and some other sources such as these:
Looking for annotations, I was able to finally get around those issues!

I thought it'd be of help to people, thanks for helping me out anyways, appreciate it!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!