简体   繁体   中英

How to find import associated with a class?

Within Eclipse if a class is not found and is available on classpath Eclipse can auto import the required package. What is the mechanism that allows this ?

I doubt there some repository of class files and their associated packages that Eclipse searches...

The reason I tagged this java and scala is I assume the mechanism is same for both languages.

It's as simple as searching through the whole classpath to see if it can match the class name. Then if it can, just adding the path it took to get there. There's nothing mysterious about this

The Eclipse feature simply searches the entire collection of folders in the classpath until it finds one or more matches, then it gives you those matches as options. In pseudocode:

Find Foo.class:
  For Each FOLDER in CLASSPATH
    For Each PATH...Foo.class Found There
      import PATH...Foo.class

You can count JAR files as folders. To see their content, try this on the command-line:

jar tvf MyJar.jar

The class path includes jars containing all JRE classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM