This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Creating Class object from .java file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating Class object from .java file" Watch "Creating Class object from .java file" New topic
Author

Creating Class object from .java file

Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Hello all.
Does any one know how can I create a Class instance from a java file I know that the obvious answer will be "compile it" and that will be may be what I'll but let me tell you what do I want this for.
I'm creating ( for learning purposes only ) a java browser like those in any respectable IDE, where I can browse a file and see in a panel it's methods, attributes and things like that.
I have an approach already for a loaded class
something like
public void display( Class clazz ){
Method[] methods = clazz.getDeclaredMethods();
for( int i = 0 ; i< methods.length; i++ ){
// you may guess the rest.
}
}
Now what Im looking for is a way I can show the java attributes of a .java file
public void display( File file ){
if( file.getName().endsWith(".java"){
display( getClassFrom( file ) );
}
}
This method public Class getClassFrom( File file ); will parse or compile or whatever it needs to make a Class instance from a java file, but googling throught the net I haven't found any API for this java parsing ( I might have not made the right questions though ).
Any way, In case I can't find an API for this what I'll do is Runtime.exec( "javac -d tmpDir file" ) etc and then load this class using a URLClassLoader, but somethig tells me there should be an easier way.
Looking forward for your comments.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
I think what you are looking for is this:
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Originally posted by Eugene Kononov:
I think what you are looking for is this:

Rigth, I knew that someone should know how to do this.
Thanks Eugene, I'll try it on the night and I'll tell you the results.
Byte!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I understand the tools package is deprecated now and going away in the future. I can't believe Sun doesn't provide some nice APIs for IDE authors. Do we have to shell the compiler in the future?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

It is my understanding that SUN is going to provide public APIs (i.e., java.something packages) for the now-deprecated tools API. It's jsut that they havn't gotten to that yet, even though they've deprecated the tools API.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating Class object from .java file