How java compiler resolves non-imported name

I believe that I use the type Xin my java compilation module from the package foo.barand is Xnot defined in the compilation part itself and is not imported directly. How now does the java compiler resolve Xefficiently? There are several possibilities that Xmay include:

  • X can be imported through import stars a.b.*
  • X may be in the same package as the compilation unit
  • X can be a type of language, that is, be in java.lang

The problem I see is especially (2.). Since it Xcan be a private type of package, it is not even required to Xbe in a compilation unit with a name X.java. Thus, the compiler must examine all the class path entries and look for any classes in the package foo.bar, then it must read every class in the package foo.barto check if it is included X.

It sounds very expensive. Especially when I compile only one file, the compiler should read dozens of class files just for type search X. If I use many star names, this procedure needs to be repeated for many types (although class files will not be read twice, of course).

So is it also advisable to import types from one package to speed up the compilation process? Or is there a faster method for resolving an unimported type Xthat I could not find?

+4
source share
2 answers

It sounds very expensive.

If the compiler did this, it would be expensive.

But what actually happens is that it builds a data structure in memory containing all the class names in the class path, bootclasspath and sourcepath, and uses this for all class name resolutions in all classes that are compiled into javacrun.

? X, ?

, . . , , , .

, .

+1

, foo.bar

Class.forName().

, foo.bar, , X.

, .

.class Class.forName(),, , new File(...).exists() ..

, : . , , , .

0

Source: https://habr.com/ru/post/1543514/


All Articles