• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Creating Class object from .java file

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic