my dog learned polymorphism
The moose likes Java in General and the fly likes Querying class information Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Querying class information" Watch "Querying class information" New topic

Querying class information

Sagar Bilgi
Ranch Hand

Joined: Apr 26, 2001
Posts: 37
Hi All,
What I want to do is pretty simple. I have some java source files. I want to find the public methods in the Java files.

How do I achieve this? I do not want to parse the files and then go line by line and search for the public keyword.

I tried using reflection so that I could just query the .class file. Load it via Class.loadClass() and then get all the methods. However if the class references a file which is in another jar in the project then how do I get that information. If I try to query the class without having loaded the referenced class I get a ClassNotFoundException.

Any other ideas?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33111

Do these files compile? If so, the easiest way is to compile them and then use reflection.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Sagar Bilgi
Ranch Hand

Joined: Apr 26, 2001
Posts: 37
Yes the files do compile. The problem with using reflection as I have mentioned in my post is that the .class file expects any class referenced in the .class file to be also loaded before any methods are called on the loaded class.

for ex: I have a X.class file which is compiled from contains references to Now when we call Class.loadClass on X.class, if you want to do anything meaningful on the Class returned then you have to ensure that Y.class is also loaded else you will get a ClassNotFoundException.

SO -- Is there a different way to use reflection which will ensure that when I load Class X all of the classes that X references are also loaded?

Thanks in advance
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128


This happens when you are using one precompiled jar file ( containing class files only) which depends upon another set of jar files. for example if we use hibernate.jar then we have to also use some other jar files ( like dom4j CGLIB etc..unless they are not already in your classpath ).

The mandatory thing that we have to reckon with is to go through the 'readme' file to know all the dependencies of the jar file in question and add all those jar libraries also in your project / classpath. Typically the jar file providor also provides the dependent jar files in another folder like lib / ext.

well if all this is a head ache, then you can load the Class file through the util.jar apis and then use the defineClass() method to bring in all the classes that X depends upon. but the procedure defined above would avoid you to phisically load all the classes through the defineClass etc.
I agree. Here's the link:
subject: Querying class information
It's not a secret anymore!