wood burning stoves
The moose likes Java in General and the fly likes Enumerate imports for a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enumerate imports for a class" Watch "Enumerate imports for a class" New topic

Enumerate imports for a class

Enache Marius Cristian

Joined: Nov 29, 2004
Posts: 5
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

Joined: Jan 29, 2003
Posts: 8791
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.

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
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Enache Marius Cristian

Joined: Nov 29, 2004
Posts: 5
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).
I agree. Here's the link: http://aspose.com/file-tools
subject: Enumerate imports for a class
It's not a secret anymore!