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.