File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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?

Thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29248
    
139

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sagar Bilgi
Ranch Hand

Joined: Apr 26, 2001
Posts: 37
Hi,
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 X.java. X.java contains references to Y.java. 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

Hi,

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.
 
jQuery in Action, 2nd edition
 
subject: Querying class information
 
Similar Threads
FielNameFilter - how does accept () get called ???
Creating a new object from a text file - please help!!
Access database from a class file
FileNameFilter - how deos accept () get invoked ?
How cohesive is high cohesion?