blackdrag commented on code in PR #3227:
URL: https://github.com/apache/groovy/pull/3227#discussion_r4648404270


##########
src/main/java/org/codehaus/groovy/control/ResolveVisitor.java:
##########
@@ -791,6 +807,187 @@ private void ambiguousClass(final ClassNode type, final 
ClassNode iType, final S
         }
     }
 
+    /**
+     * Binds a simple name from on-demand imports.
+     * <p>
+     * A source import shadows every default import, including implicit
+     * {@code java.lang.*} and star imports added by a compilation customizer.
+     * Source static-import-on-demand, source type-import-on-demand, and the
+     * packages expanded from one source {@code import module} are walked in
+     * source order. The packages of a single module import are one statement:
+     * the first accessible type among them is bound, and an inaccessible type
+     * in an earlier exported package does not hide a later package of that
+     * same statement.
+     * <p>
+     * Separate source statements are not a fallback for each other. The first
+     * statement that finds the type claims it. An accessible type is bound.
+     * That is the long-standing rule: {@code import java.util.*} before
+     * {@code import java.sql.*} selects {@code java.util.Date}, and the
+     * reverse selects {@code java.sql.Date}. Two accessible source stars are
+     * not an ambiguity. An inaccessible type also claims the name, so a later
+     * source statement does not bind a different type that would be displaced
+     * if the earlier type became visible. The name then falls through to
+     * customizer on-demand imports and the language defaults. A non-static
+     * member type is not claimed, because a type star does not import it.
+     * <p>
+     * Customizer on-demand imports have no source location. They are tried
+     * when no source statement bound the name, including when one claimed
+     * an inaccessible type, and before the language defaults, so a
+     * configured star can still supply {@code Date} or {@code String}.
+     * Within that tier an inaccessible type is skipped. Static stars
+     * precede package stars because the two are stored separately. An
+     * explicit single-type import is resolved earlier and therefore wins
+     * over every on-demand import.
+     *
+     * @return {@code true} if the name was bound
+     */
+    private boolean resolveFromOnDemandImports(final ClassNode type, final 
String name, final ModuleNode module) {
+        OnDemandImports order = onDemandImports(module);
+        if (bindOnDemandTier(type, name, order.declared, true)) return true;
+        return bindOnDemandTier(type, name, order.customizer, false);
+    }
+
+    /**
+     * @param claim when {@code true}, a group that finds the type but cannot
+     *              access it stops later groups in this tier. Customizer
+     *              imports pass {@code false} and skip that group.
+     * @return {@code true} if a type was bound
+     */
+    private boolean bindOnDemandTier(final ClassNode type, final String name, 
final List<List<ImportNode>> groups, final boolean claim) {
+        for (List<ImportNode> group : groups) {
+            OnDemandHit hit = bindOnDemandGroup(type, name, group);
+            if (hit == OnDemandHit.BOUND) return true;
+            if (claim && hit == OnDemandHit.BLOCKED) return false;
+        }
+        return false;
+    }
+
+    private OnDemandHit bindOnDemandGroup(final ClassNode type, final String 
name, final List<ImportNode> group) {
+        boolean blocked = false;
+        for (ImportNode importNode : group) {
+            OnDemandHit hit = bindOnDemand(type, name, importNode);
+            if (hit == OnDemandHit.BOUND) return OnDemandHit.BOUND;
+            if (hit == OnDemandHit.BLOCKED) blocked = true;
+        }
+        return blocked ? OnDemandHit.BLOCKED : OnDemandHit.MISS;
+    }
+
+    private OnDemandHit bindOnDemand(final ClassNode type, final String name, 
final ImportNode importNode) {
+        ClassNode outer = importNode.getType();
+        if (outer != null) {
+            // The star named a type: "import static Type.*" (the parser sets
+            // the type) or "import Type.*" (visitClass sets it when the name
+            // resolves to a class). Both import member types of that type,
+            // and only a static member type is in scope, the same rule as a
+            // single static import. A non-static inner class is not, and it
+            // does not claim the name. A member type of an interface is
+            // implicitly static, so Map.Entry still matches.
+            ClassNode tmp = new ConstructedNestedClass(outer, name);
+            if (!resolve(tmp, false, false, true) || 
!Modifier.isStatic(tmp.getModifiers())) {
+                return OnDemandHit.MISS;
+            }
+            if (!isStarImportAccessible(tmp, outer)) return 
OnDemandHit.BLOCKED;
+            type.setRedirect(tmp.redirect());
+            return OnDemandHit.BOUND;
+        }
+        String packageName = importNode.getPackageName();
+        if (packageName == null) return OnDemandHit.MISS;
+        ClassNode tmp = new ConstructedClassWithPackage(packageName, name);
+        if (!resolve(tmp, false, false, true)) return OnDemandHit.MISS;
+        if (!isStarImportAccessible(tmp, null)) return OnDemandHit.BLOCKED;
+        type.setRedirect(tmp.redirect());
+        return OnDemandHit.BOUND;
+    }
+
+    /**
+     * Source on-demand imports in source order, grouped by statement, then
+     * customizer on-demand imports. Recomputed when needed: the lists are
+     * small, and storing them as module metadata would keep them for the
+     * rest of compilation.
+     * <p>
+     * {@code import module} expands to one star per exported package, but only
+     * the last of those nodes carries the statement's line and column. The
+     * packages before it have no position of their own and must inherit that
+     * position, in export order, as one statement. Sorting separate nodes by
+     * the line numbers they actually store would place those packages, and
+     * every customizer import (line &lt;= 0), ahead of the source imports.
+     * {@code List.sort} is stable, so statements that share a position keep
+     * the order they were added.
+     */
+    private static OnDemandImports onDemandImports(final ModuleNode module) {
+        List<PositionedImport> positioned = new ArrayList<>();
+        for (ImportNode node : module.getStaticStarImports().values()) {
+            if (node.getLineNumber() > 0) positioned.add(single(node));
+        }
+        for (ImportNode node : module.getStarImports()) {
+            if (node.getLineNumber() > 0) positioned.add(single(node));
+        }
+        List<ImportNode> pendingModule = new ArrayList<>();
+        for (ImportNode node : module.getModuleStarImports()) {
+            pendingModule.add(node);
+            if (node.getLineNumber() > 0) {
+                positioned.add(new PositionedImport(node.getLineNumber(), 
node.getColumnNumber(), List.copyOf(pendingModule)));
+                pendingModule.clear();
+            }
+        }
+        positioned.sort((a, b) -> {
+            int byLine = Integer.compare(a.line, b.line);
+            if (byLine != 0) return byLine;
+            return Integer.compare(a.column, b.column);

Review Comment:
   This still gives me a headache, I think we should not depend on the line 
information for this. But I do not think it is worth blocking the issue. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to