diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ImportReference.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ImportReference.java index f1ccc4f240a..b6ce1f77124 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ImportReference.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ImportReference.java @@ -35,7 +35,6 @@ public class ImportReference extends ASTNode { public Annotation[] annotations; // star end position public int trailingStarPosition; - public boolean implicit; public ImportReference( char[][] tokens, @@ -60,9 +59,6 @@ public boolean isStatic() { public char[][] getImportName() { return this.tokens; } - public boolean isImplicit() { - return this.implicit; - } public char[] getSimpleName() { return this.tokens[this.tokens.length - 1]; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java index 1f646063e7f..a50361a11f0 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java @@ -241,14 +241,20 @@ void checkAndSetImports() { int numberOfImports = numberOfStatements + defaultImports.length; for (int i = 0; i < numberOfStatements; i++) { ImportReference importReference = this.referenceContext.imports[i]; - if ((importReference.bits & ASTNode.OnDemand) != 0 && !importReference.isStatic()) { - if (CharOperation.equals(TypeConstants.JAVA_LANG, importReference.tokens)) { - numberOfImports--; - } else if ((importReference.modifiers & ClassFileConstants.AccModule) != 0 && - this.referenceContext.isSimpleCompilationUnit() && - CharOperation.equals(TypeConstants.JAVA_BASE, importReference.tokens)) { - numberOfImports--; - } + if ((importReference.bits & ASTNode.OnDemand) != 0) { + if (importReference.isStatic()) { + if (CharOperation.equals(TypeConstants.JAVA_IO_IO, importReference.tokens)) { + numberOfImports--; + } + } else { + if (CharOperation.equals(TypeConstants.JAVA_LANG, importReference.tokens)) { + numberOfImports--; + } else if ((importReference.modifiers & ClassFileConstants.AccModule) != 0 && + this.referenceContext.isSimpleCompilationUnit() && + CharOperation.equals(TypeConstants.JAVA_BASE, importReference.tokens)) { + numberOfImports--; + } + } } } ImportBinding[] resolvedImports = new ImportBinding[numberOfImports]; @@ -262,6 +268,7 @@ void checkAndSetImports() { return false; } return (CharOperation.equals(TypeConstants.JAVA_LANG, imp.tokens) || + (CharOperation.equals(TypeConstants.JAVA_IO_IO, imp.tokens) && imp.isStatic()) || ((imp.modifiers & ClassFileConstants.AccModule) != 0 && CharOperation.equals(TypeConstants.JAVA_BASE, imp.tokens))); }; @@ -764,13 +771,24 @@ private MethodBinding findStaticMethod(ReferenceBinding currentType, char[] sele return null; } ImportBinding[] getDefaultImports() { + ImportBinding javaIOImport = null; 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, '.')); + Binding javaioIO = findSingleImport(TypeConstants.JAVA_IO_IO, Binding.TYPE, false); + if (javaioIO != null) { + javaIOImport = new ImportBinding(TypeConstants.JAVA_IO_IO, true, javaioIO, null) { + @Override + public boolean isStatic() { + return true; + } + }; + } 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}; + return javaIOImport != null ? new ImportBinding[] {javaBase, javaIOImport}: + new ImportBinding[] {javaBase}; // this module import is not cached, there shouldn't be many files needing it. } } @@ -780,7 +798,10 @@ ImportBinding[] getDefaultImports() { Binding importBinding = this.environment.getTopLevelPackage(TypeConstants.JAVA); if (importBinding != null) importBinding = ((PackageBinding) importBinding).getTypeOrPackage(TypeConstants.JAVA_LANG[1], module(), false); - + ImportBinding[] implicitImports = javaIOImport != null ? new ImportBinding[2] : new ImportBinding[1]; + if (javaIOImport != null) { + implicitImports[1] = javaIOImport; + } if (importBinding == null || !importBinding.isValidBinding()) { // create a proxy for the missing BinaryType problemReporter().isClassPathCorrect( @@ -789,9 +810,11 @@ ImportBinding[] getDefaultImports() { this.environment.missingClassFileLocation, false, null/*resolving j.l.O is not specific to any referencing type*/); BinaryTypeBinding missingObject = this.environment.createMissingType(null, TypeConstants.JAVA_LANG_OBJECT); importBinding = missingObject.fPackage; - return new ImportBinding[] {new ImportBinding(TypeConstants.JAVA_LANG, true, importBinding, null)}; + implicitImports[0] = new ImportBinding(TypeConstants.JAVA_LANG, true, importBinding, null); + return implicitImports; } - return this.environment.root.defaultImports = new ImportBinding[] {new ImportBinding(TypeConstants.JAVA_LANG, true, importBinding, null)}; + implicitImports[0] = new ImportBinding(TypeConstants.JAVA_LANG, true, importBinding, null); + return this.environment.root.defaultImports = implicitImports; } // NOT Public API public final Binding getImport(char[][] compoundName, boolean onDemand, int modifiers) { diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java index 2a72d85fa51..3897dc24425 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java @@ -225,6 +225,7 @@ public interface TypeConstants { char[][] JAVA_IO_IOEXCEPTION = new char[][] { JAVA, IO, "IOException".toCharArray()};//$NON-NLS-1$ char[][] JAVA_IO_OBJECTOUTPUTSTREAM = new char[][] { JAVA, IO, "ObjectOutputStream".toCharArray()}; //$NON-NLS-1$ char[][] JAVA_IO_OBJECTINPUTSTREAM = new char[][] { JAVA, IO, "ObjectInputStream".toCharArray()}; //$NON-NLS-1$ + char[][] JAVA_IO_IO = new char[][] {JAVA, IO, "IO".toCharArray()}; //$NON-NLS-1$ char[][] JAVA_NIO_FILE_FILES = new char[][] { JAVA, "nio".toCharArray(), "file".toCharArray(), "Files".toCharArray() }; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ // javax.rmi.CORBA.Stub char[][] JAVAX_RMI_CORBA_STUB = new char[][] { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/ImplicitlyDeclaredClassesTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/ImplicitlyDeclaredClassesTest.java index 44913db4ebd..b0dfecebfdb 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/ImplicitlyDeclaredClassesTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/ImplicitlyDeclaredClassesTest.java @@ -341,4 +341,27 @@ void main() { "Zork cannot be resolved to a type\n" + "----------\n"); } + public void testGH3137a() { + runConformTest(new String[] { + "X.java", + """ + public static void main(String[] args) { + println("Hello1"); + println("Hello2"); + }""" + }, + "Hello1\n" + + "Hello2"); + } + public void testGH3137b() { + runConformTest(new String[] { + "X.java", + """ + public static void main(String[] args) { + String str = readln("Enter:"); + println(str); + }""" + }, + "Enter:"); + } } diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java index f1959124dfb..3676a045096 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTConverter.java @@ -1542,8 +1542,6 @@ public CompilationUnit convert(org.eclipse.jdt.internal.compiler.ast.Compilation org.eclipse.jdt.internal.compiler.ast.ImportReference[] imports = unit.imports; if (imports != null) { for (ImportReference importReference : imports) { - if (importReference.isImplicit()) - continue; compilationUnit.imports().add(convertImport(importReference)); } }