• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enumerate imports for a class

 
Enache Marius Cristian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to enumerate the imported classes for a given java class at run time. Does anybody know how to do this ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not aware of anything in standard reflection. Look in the JavaDoc for Class to be sure.

A rather nasty low-level approach is to read the class file and parse it. The internal structure is well documented and includes the classes on which this one depends. I think.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan's right. Pretty much the only thing you could do would be to root through the constant pool and find all the class references.

Note that this would get you the dependencies, but not what was imported in the source code -- the import statement has no runtime footprint. If a source file imported java.util.* but only used java.util.ArrayList, there'd be no way to determine the former, only the latter.
 
Enache Marius Cristian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, you`re right. From my researches I found a way to enumerate all constants for a class and from there determine the classes that I need (constants starting with com.myname).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic