Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[23] static methods from java.io.IO not imported in implicitly declared classes #3137 #3140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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)));
};
Expand Down Expand Up @@ -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.
}
}
Expand All @@ -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(
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[][] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Loading