On-demand import declaration with subpackages only

Related: How can I compile "import pack. *" With ant / javac if there are no such classes?

Suppose we have a given packing structure

parent | ---a ---b 

where the parent package contains only two subpackages a and b (the class is not under the parent package).

The import parent.* Code, located in a package other than parent , compiled with Maven (i.e. javac ) throws a compile-time error. Error:

package parent does not exist

I studied the Java language specification in this case (on-demand import declaration, where types are not actually imported). Paragraph 7.5.2 of the JLS on declaring import on demand does not seem to indicate this behavior.

Is this a javac bug? I am not asking how to get around a compilation error. I would like to know why javac throws an error, because I cannot find the link in JLS, that it should; making me believe that this could be a mistake.

I tested with JDK 1.4, 1.6, 1.7 and 1.8, the error is the same.

As a side note, Eclipse lacks compilation with the same code (tested with Eclipse Indigo, Juno, Luna, and Mars).

NB: I encountered this behavior when “mavenizing” an (old) existing project that relied only on the Eclipse compiler. It took me a while to determine that this was the main cause of the compilation error I was getting.

+4
source share
1 answer

I think you are looking for section7.4.3

A package can be observed if and only if:

  • A compilation unit containing a package declaration, observable (§7.3).

  • The package subpackage can be observed.

The packages java, java.lang and java.io are always observable.

And in 7.5.2

This is a compile-time error if a named package or type is not available (§6.6).

So, if there is nothing in the package, it does not exist, and the error is actually on the Eclipse compiler side; it is described in a very old bug report , which remained in the “LATER” state until, in the end, this state was out of date, and instead it was transferred to WONT_FIX. Feel free to re-open it if you think this is worth the fix (now that JLS is clarifying in this area)

+2
source

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


All Articles