• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

how to search through all classes loaded?

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I am going to create an annotation and the developer can put it on a class. then I have a manager class in which I load those metadata for the first time to be used later.

earlier I used to have a XML file so the developer should specify the classes that are marked with the annotation and the manager class will read that file. Now I want to search through all the loaded classes and see if any of them are annotated with my annotation, so I can obtain the metadata.

My question is how to find the name or Class object of each classes that are already loaded by the class loader?

thanks in advance
[ October 09, 2008: Message edited by: Mohammad Norouzi ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammad:
My question is how to find the name or Class object of each classes that are already loaded by the class loader?


It is highly improbable that you will get a list of classes loaded by a classloader.
However, you can do one thing.
Write a custom classloader that inspects the class after loading for the required annotations. Use this classloader to load all the classes in your application.
You can get hold of annotations using reflection (Class.isAnnotationPresent())

BTW, which is the java version you are working on?
If it is java6 then you have a complete annotation processing infrastructure at your disposal! (look for javax.annotation.processing package)
You can do a lot of interesting things using this.

Jdk5 also has an annotation processing tool(apt) that will help you interpret and process build time annotations.
 
Sheriff
Posts: 22701
129
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

Originally posted by Nitesh Kant:

It is highly improbable that you will get a list of classes loaded by a classloader.


Through regular ways yes. Through reflection you can get access to the following field:

However, since it is private Sun can always change its signature. As long as you treat it as a Collection you're probably safe but even then the name could potentially change.
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
I reached this idea because I see that EJB containers can simply seek the session beans with no declaration but an annotation.

so I think this is a really complicated process

I am using JDK 5 and regarding APT tool you said, I had a look once but it seemed a little complicated and decided not to use that
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammad Norouzi:
I reached this idea because I see that EJB containers can simply seek the session beans with no declaration but an annotation.

so I think this is a really complicated process



They would use an annotation processor to generate code for descriptors,remote/home interfaces.
So, writing an ejb via annotations is just a facility to save you from writing the code that does not require any business logic.
At the end of it, the deployment unit for ejbs remain the same. Who writes what is changed when you write annotations or you dont.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difficulty is in "all loaded classes" (which is hard to do). But that's not really what you require, which -if I understand it correctly- is "all classes in the application's classpath". That's easier, since often you know exactly where those classes might be. For instance, for a web app, you need to look at the WEB-INF/classes and WEB-INF/lib directories, and inspect all classes and jar file you find. For JEE apps, it's wherever .ear files go. That's not fun code to write, but it's fairly straightforward. You wouldn't even need to use a special classloader for those classes.
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
You wouldn't even need to use a special classloader for those classes.



Yes Ulf, I need to search only through the classes in the scope of my application. As I understand from your reply, I have to search in file system under the classpath directories? is there any way to check all the classes in my application programmatically?

Nitesh Kant:
They would use an annotation processor to generate code for descriptors,remote/home interfaces.



May I ask you suggest if I am doing something wrong. I usually find those classes annotate with my already created annotation and in startup I have a Map<Class,ClassInfo> and ClassInfo is a structure in which I have stored those information provided by the annotation, it includes some Method objects of reflection to access the fields that is marked with another custom annotation. so each time I need to process that class I am using this cached information in a singleton class.

is there any other way with higher performance to do that? I ask this because you told that they generate some other codes in run time and this enlightened my mind if I can do the same
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As I understand from your reply, I have to search in file system under the classpath directories?


Yes, but you can leave out those parts of the classpath where no application classes should be, like the extension directory and the class libraries.

is there any way to check all the classes in my application programmatically?


That's what we're discussing, isn't it? Or do you mean "is there an API call for this?" I don't think there is.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammad Norouzi:
is there any other way with higher performance to do that? I ask this because you told that they generate some other codes in run time and this enlightened my mind if I can do the same



No, it does not generate code at runtime. It will be done at compile time.
If i would have to do it, i would process the annotated class with an annotation processor at compile time and generate a class that will have all the registration information of the annotated classes. (In your case the class having the map)
At runtime, i would just initialize this generated class and boom you have everything you want
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitesh and Ulf
Thanks you very much
So I have decided to have a deep look on APT
 
eat bricks! HA! And here's another one! And a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic