Re: Get imports for program
bH wrote:
Hi All,
My goal is know how are "imports"
at the top of a program discovered/obtained?
Are you referring to the process the compiler (and JVM as well) uses to
get references to other classes, or the IDE commands to figure out what
the correct import is?
If the former, this happens via classpath (or sourcepath in some cases
for the compiler), assuming you're using regular classloaders. More
advanced setups (e.g., dynamic runtime creation of classes) are beyond
the scope of this post.
If the latter, the IDE is most likely making a list of all classes in
the JVM distribution (mostly rt.jar, although I presume it would use
stuff in the extensions directory), the "imported" libraries (external
JARs your program will use), and the project source itself.
Note that in both cases, simple name conflicts can and do occur. For
example, there is a java.awt.List and a java.util.List. The compiler
will throw an error if it sees an ambiguous reference, the JVM always
uses fully-qualified class names, and IDEs may vary in how they deal
with such conflicts. Since I don't use this feature, nor use an IDE that
has feature, I don't know what an IDE would do.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth