Skip to content

Commit

Permalink
[23] default imports between explicit/implicit classes are in conflict (
Browse files Browse the repository at this point in the history
#2953)

don't use the cached default import java.lang.* in implicitly declared classes.

fixes #2952
  • Loading branch information
stephan-herrmann authored Sep 13, 2024
1 parent 4e315ae commit e3ca2f3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -764,18 +764,19 @@ private MethodBinding findStaticMethod(ReferenceBinding currentType, char[] sele
return null;
}
ImportBinding[] getDefaultImports() {
// initialize the default imports if necessary... share the default java.lang.* import
if (this.environment.root.defaultImports != null) return this.environment.root.defaultImports;

if (JavaFeature.IMPLICIT_CLASSES_AND_INSTANCE_MAIN_METHODS.isSupported(this.environment.globalOptions) &&
this.referenceContext.isSimpleCompilationUnit()) {
ModuleBinding module = this.environment.getModule(CharOperation.concatWith(TypeConstants.JAVA_BASE, '.'));
if (module != null) {
ImportBinding javaBase = new ImportBinding(TypeConstants.JAVA_BASE, true, module, null);
// No need for the java.lang.* as module java.base covers it
return new ImportBinding[] {javaBase};
// this module import is not cached, there shouldn't be many files needing it.
}
}
// initialize the default imports if necessary... share the default java.lang.* import
if (this.environment.root.defaultImports != null) return this.environment.root.defaultImports;

Binding importBinding = this.environment.getTopLevelPackage(TypeConstants.JAVA);
if (importBinding != null)
importBinding = ((PackageBinding) importBinding).getTypeOrPackage(TypeConstants.JAVA_LANG[1], module(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,35 @@ public static void main(String ... args) {
}"""},
"true");
}
@Test
public void testImplicitImport() {
// the explicit class must be given first to trigger https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2952
// the test is made as negative because we can't execute the second class
// and we are not interested in executing the first, which cannot see the second
runNegativeTest(
new String[] {
"b/B.java",
"""
package b;
import java.util.Collection;
public class B {
public static void print(Collection<?> col) {
System.out.print(col.size());
}
Zork zork;
}
""",
"X.java",
"""
void main() {
b.B.print(Collections.emptySet());
}"""
},
"----------\n" +
"1. ERROR in b\\B.java (at line 7)\n" +
" Zork zork;\n" +
" ^^^^\n" +
"Zork cannot be resolved to a type\n" +
"----------\n");
}
}

0 comments on commit e3ca2f3

Please sign in to comment.