JEP 216: Process Import Statements Correctly
Summary
Fix javac
to properly accept and reject programs regardless of the order of import
statements and extends
and implements
clauses.
Motivation
In some cases javac
will accept source code with a certain order of imports and reject the same source code with just the imports reordered (e.g., JDK-7177813). That is wrong and confusing.
Description
javac
uses several stages when compiling classes. Considering import
handling, the two important stages are:
-
Type resolution, which looks through the provided AST looking for class and interface declaration, and
-
Member resolution, which includes:
- (1a) If
T
is toplevel,import
s in the source file definingT
are processed and imported members added inT
's scope - (1b) If
T
is nested, resolution of the class directly enclosingT
(if any) - (2)
extends
/implements
clauses ofT
are type-checked - (3) Type variables of
T
are type-checked
- (1a) If
The above stages are part of javac
's process of resolution of classes, which includes determining a class's supertypes, type variables, and members.
To see this in process in action, consider this code:
package P;
import static P.Outer.Nested.*;
import P.Q.*;
public class Outer {
public static class Nested implements I {
}
}
package P.Q;
public interface I {
}