• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Querying class information

 
Sagar Bilgi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34686
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar,
Do these files compile? If so, the easiest way is to compile them and then use reflection.
 
Sagar Bilgi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic