Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Class object from .java file

 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you are looking for is this:
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic