diff --git a/JCL/converterJclMin9/src/java/io/PrintStream.java b/JCL/converterJclMin9/src/java/io/PrintStream.java new file mode 100644 index 00000000000..503f080425d --- /dev/null +++ b/JCL/converterJclMin9/src/java/io/PrintStream.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package java.io; + +public class PrintStream { + + public void println() { + } + + public void println(String s) { + } + + public void println(int i) { + } + + public void println(Object o) { + } + + public void print(String s) { + } + + public void print(Object o) { + } + + public void print(int i) { + } +} diff --git a/JCL/converterJclMin9/src/java/lang/AutoCloseable.java b/JCL/converterJclMin9/src/java/lang/AutoCloseable.java new file mode 100644 index 00000000000..b6fbe467732 --- /dev/null +++ b/JCL/converterJclMin9/src/java/lang/AutoCloseable.java @@ -0,0 +1,5 @@ +package java.lang; + +public interface AutoCloseable { + void close() throws Exception; +} diff --git a/JCL/converterJclMin9/src/java/lang/Override.java b/JCL/converterJclMin9/src/java/lang/Override.java new file mode 100644 index 00000000000..e30eba7b6df --- /dev/null +++ b/JCL/converterJclMin9/src/java/lang/Override.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package java.lang; + +import java.lang.annotation.ElementType; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.Retention; + +@Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) +public @interface Override { +} \ No newline at end of file diff --git a/JCL/converterJclMin9/src/java/lang/System.java b/JCL/converterJclMin9/src/java/lang/System.java new file mode 100644 index 00000000000..a13acd2404d --- /dev/null +++ b/JCL/converterJclMin9/src/java/lang/System.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package java.lang; + +import java.io.PrintStream; + +public class System { + + public static PrintStream err; + public static PrintStream out; + + public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); + public static String getProperty(String s) { + return null; + } +} 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 1809520b367..efb8efd3bc1 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 @@ -791,6 +791,7 @@ 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)}; } return this.environment.root.defaultImports = new ImportBinding[] {new ImportBinding(TypeConstants.JAVA_LANG, true, importBinding, null)}; } diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java index 812d8e49655..90f7870a193 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java @@ -10328,4 +10328,106 @@ F test() { """; runner.runNegativeTest(); } +public void testGH3047() throws Exception { + Runner runner = new Runner(); + runner.testFiles = new String[] { + "resources/examples/mockito/MockingFromFinder.java", + """ + package examples.mockito; + public class MockingFromFinder{} + """, + "resources/examples/mockito/MockingWhileAdding.java", + """ + package examples.mockito; + public class MockingWhileAdding { + public static void calculateWithAdder(int x, int y) { + IOperation adder = new Adder()::execute; + } + public interface IOperation { + int execute(int x, int y); + } + public static class Adder implements IOperation { + public int execute(int x, int y) { + return x+y; + } + } + } + """ + }; + runner.classLibraries = new String[0]; + if (this.complianceLevel <= ClassFileConstants.JDK13) { + runner.expectedCompilerLog = + """ + ---------- + 1. ERROR in resources\\examples\\mockito\\MockingFromFinder.java (at line 1) + package examples.mockito; + ^ + The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files + ---------- + 2. ERROR in resources\\examples\\mockito\\MockingFromFinder.java (at line 2) + public class MockingFromFinder{} + ^^^^^^^^^^^^^^^^^ + Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor + ---------- + ---------- + 1. ERROR in resources\\examples\\mockito\\MockingWhileAdding.java (at line 1) + package examples.mockito; + ^ + The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files + ---------- + 2. ERROR in resources\\examples\\mockito\\MockingWhileAdding.java (at line 2) + public class MockingWhileAdding { + ^^^^^^^^^^^^^^^^^^ + Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor + ---------- + 3. ERROR in resources\\examples\\mockito\\MockingWhileAdding.java (at line 9) + public static class Adder implements IOperation { + ^^^^^ + Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor + ---------- + """; + } else { + runner.expectedCompilerLog = + """ + ---------- + 1. ERROR in resources\\examples\\mockito\\MockingFromFinder.java (at line 1) + package examples.mockito; + ^ + The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files + ---------- + 2. ERROR in resources\\examples\\mockito\\MockingFromFinder.java (at line 1) + package examples.mockito; + ^ + The type java.lang.Error cannot be resolved. It is indirectly referenced from required .class files + ---------- + 3. ERROR in resources\\examples\\mockito\\MockingFromFinder.java (at line 1) + package examples.mockito; + ^ + The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files + ---------- + 4. ERROR in resources\\examples\\mockito\\MockingFromFinder.java (at line 2) + public class MockingFromFinder{} + ^^^^^^^^^^^^^^^^^ + Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor + ---------- + ---------- + 1. ERROR in resources\\examples\\mockito\\MockingWhileAdding.java (at line 1) + package examples.mockito; + ^ + The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files + ---------- + 2. ERROR in resources\\examples\\mockito\\MockingWhileAdding.java (at line 2) + public class MockingWhileAdding { + ^^^^^^^^^^^^^^^^^^ + Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor + ---------- + 3. ERROR in resources\\examples\\mockito\\MockingWhileAdding.java (at line 9) + public static class Adder implements IOperation { + ^^^^^ + Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor + ---------- + """; + } + runner.runNegativeTest(); +} } diff --git a/org.eclipse.jdt.core.tests.model/JCL/converterJclMin9.jar b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin9.jar index ffedd500d72..0fe91046f6f 100644 Binary files a/org.eclipse.jdt.core.tests.model/JCL/converterJclMin9.jar and b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin9.jar differ diff --git a/org.eclipse.jdt.core.tests.model/JCL/converterJclMin9src.zip b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin9src.zip index d04c399924b..636debe16aa 100644 Binary files a/org.eclipse.jdt.core.tests.model/JCL/converterJclMin9src.zip and b/org.eclipse.jdt.core.tests.model/JCL/converterJclMin9src.zip differ diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTest.java index 3220c21f3f9..ebcef50186b 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2017 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -14,8 +14,12 @@ package org.eclipse.jdt.core.tests.dom; import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Hashtable; import java.util.List; import java.util.Map; +import java.util.Set; import junit.framework.Test; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.IClassFile; @@ -24,8 +28,10 @@ import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.WorkingCopyOwner; +import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.dom.*; @SuppressWarnings("rawtypes") @@ -1324,4 +1330,119 @@ public void testBug381503() throws CoreException, IOException { deleteProject("P"); } } +public void testGH3047() throws Exception { + Hashtable options = JavaCore.getDefaultOptions(); + options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_9); + options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_9); + options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_9); + + createJava9Project("P"); + createFolder("P/src"); + + String srcFolderInWS = "/P/src"; + createFolder(srcFolderInWS + "/resources/examples/mockito"); + String srcFilePathInWS = srcFolderInWS + "/resources/examples/mockito/MockingFromFinder.java"; + createFile(srcFilePathInWS, + """ + package examples.mockito; + public class MockingFromFinder{}""" + ); + srcFilePathInWS = srcFolderInWS + "/resources/examples/mockito/MockingWhileAdding.java"; + createFile(srcFilePathInWS, + """ + package examples.mockito; + public class MockingWhileAdding { + public static void calculateWithAdder(int x, int y) { + IOperation adder = new Adder()::execute; + } + public interface IOperation { + int execute(int x, int y); + } + public static class Adder implements IOperation { + public int execute(int x, int y) { + return x+y; + } + } + }""" + ); + String[] paths = new String[2]; + paths[0] = getWorkspacePath() + "P/src/resources/examples/mockito/MockingFromFinder.java"; + paths[1] = getWorkspacePath() + "P/src/resources/examples/mockito/MockingWhileAdding.java"; + @SuppressWarnings("deprecation") + ASTParser parser = ASTParser.newParser(AST_INTERNAL_JLS9); + parser.setCompilerOptions(options); + parser.setResolveBindings(true); + parser.setStatementsRecovery(true); + parser.setBindingsRecovery(true); + parser.setEnvironment(null, new String[] {getWorkspacePath() + "P/src/resources"}, null, false); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + try { + parser.createASTs(paths, null, new String[] {}, null, null); + fail("Expected exception was not thrown"); + } catch (IllegalStateException ise) { + assertEquals("Missing system library", ise.getMessage()); + } +} +public void testGH3047_2() throws Exception { + Hashtable options = JavaCore.getDefaultOptions(); + options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_9); + options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_9); + options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_9); + + IJavaProject javaProject = createJavaProject("P", new String[] {""}, new String[] {"CONVERTER_JCL18_LIB"}, "1.8"); + createFolder("P/src"); + + String srcFolderInWS = "/P/src"; + createFolder(srcFolderInWS + "/resources/examples/mockito"); + String srcFilePathInWS = srcFolderInWS + "/resources/examples/mockito/MockingFromFinder.java"; + createFile(srcFilePathInWS, + """ + package examples.mockito; + public class MockingFromFinder{}""" + ); + srcFilePathInWS = srcFolderInWS + "/resources/examples/mockito/MockingWhileAdding.java"; + createFile(srcFilePathInWS, + """ + package examples.mockito; + public class MockingWhileAdding { + public static void calculateWithAdder(int x, int y) { + IOperation adder = new Adder()::execute; + } + public interface IOperation { + int execute(int x, int y); + } + public static class Adder implements IOperation { + public int execute(int x, int y) { + return x+y; + } + } + }""" + ); + String[] paths = new String[2]; + paths[0] = getWorkspacePath() + "P/src/resources/examples/mockito/MockingFromFinder.java"; + paths[1] = getWorkspacePath() + "P/src/resources/examples/mockito/MockingWhileAdding.java"; + @SuppressWarnings("deprecation") + ASTParser parser = ASTParser.newParser(AST_INTERNAL_JLS9); + parser.setProject(javaProject); + parser.setCompilerOptions(options); + parser.setResolveBindings(true); + parser.setStatementsRecovery(true); + parser.setBindingsRecovery(true); + parser.setEnvironment(null, new String[] {getWorkspacePath() + "P/src/resources"}, null, false); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + Set expectedProblems = new HashSet<>(Arrays.asList( + "Pb(324) The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files", + "Pb(140) Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor" + )); + Set actualProblems = new HashSet<>(); + class MyFileASTRequestor extends FileASTRequestor { + @Override + public void acceptAST(String sourceFilePath, CompilationUnit cu) { + for (IProblem prob : cu.getProblems()) + actualProblems.add(prob.toString()); + } + } + parser.createASTs(paths, null, new String[] {}, new MyFileASTRequestor() {}, null); + assertEquals(expectedProblems, actualProblems); +} } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.java index 6bd0a2a5c2b..772b0037780 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ConverterTestSetup.java @@ -207,6 +207,14 @@ public void setUpJCLClasspathVariables(String compliance, boolean useFullJCL) th new IPath[] {getConverterJCLPath("14"), getConverterJCLSourcePath("14"), getConverterJCLRootSourcePath()}, null); } + } else if ("15".equals(compliance)) { + if (JavaCore.getClasspathVariable("CONVERTER_JCL15_LIB") == null) { + setupExternalJCL("converterJclMin15"); + JavaCore.setClasspathVariables( + new String[] {"CONVERTER_JCL15_LIB", "CONVERTER_JCL15_SRC", "CONVERTER_JCL15_SRCROOT"}, + new IPath[] {getConverterJCLPath("15"), getConverterJCLSourcePath("15"), getConverterJCLRootSourcePath()}, + null); + } } else if ("17".equals(compliance)) { if (JavaCore.getClasspathVariable("CONVERTER_JCL_17_LIB") == null) { setupExternalJCL("converterJclMin17"); diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewrite_RecordTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewrite_RecordTest.java index 3db4f9604fe..71248344102 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewrite_RecordTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewrite_RecordTest.java @@ -63,7 +63,7 @@ public static Test suite() { protected void setUp() throws Exception { super.setUp(); - IJavaProject proj= createJavaProject(PROJECT, new String[] {"src"}, new String[] {"JCL18_LIB"}, "bin", "15"); + IJavaProject proj= createJavaProject(PROJECT, new String[] {"src"}, new String[] {"JCL14_LIB"}, "bin", "15"); proj.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); proj.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4"); proj.setOption(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_16); diff --git a/org.eclipse.jdt.core.tests.model/workspace/Converter9/.classpath b/org.eclipse.jdt.core.tests.model/workspace/Converter9/.classpath index 3522bc0c3f5..6da169624e4 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Converter9/.classpath +++ b/org.eclipse.jdt.core.tests.model/workspace/Converter9/.classpath @@ -1,6 +1,10 @@ - + + + + + diff --git a/org.eclipse.jdt.core.tests.model/workspace/Converter_15_1/.classpath b/org.eclipse.jdt.core.tests.model/workspace/Converter_15_1/.classpath index 3522bc0c3f5..49c53688466 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Converter_15_1/.classpath +++ b/org.eclipse.jdt.core.tests.model/workspace/Converter_15_1/.classpath @@ -1,6 +1,10 @@ - + + + + + diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java index 9c0024e1469..a156cd00975 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java @@ -24,21 +24,17 @@ import java.util.Map; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.jdt.core.IClassFile; -import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.ITypeRoot; -import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.WorkingCopyOwner; +import org.eclipse.jdt.core.*; import org.eclipse.jdt.core.compiler.CategorizedProblem; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall; import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath; import org.eclipse.jdt.internal.compiler.batch.Main; +import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.compiler.env.IBinaryType; +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; +import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner; import org.eclipse.jdt.internal.compiler.parser.RecoveryScannerData; import org.eclipse.jdt.internal.compiler.parser.Scanner; @@ -272,8 +268,49 @@ private List getClasspath() throws IllegalStateException { } catch (IllegalArgumentException e) { throw new IllegalStateException("invalid environment settings", e); //$NON-NLS-1$ } + if ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0) { + checkForSystemLibrary(allClasspaths); + } return allClasspaths; } + + private void checkForSystemLibrary(List allClasspaths) { + boolean hasSystemLibrary = true; // default for 1.8 setting without a valid project + boolean hasModule = false; + Throwable exception = null; + String compliance = this.compilerOptions.get(JavaCore.COMPILER_COMPLIANCE); + if (CompilerOptions.versionToJdkLevel(compliance) >= ClassFileConstants.JDK9) { + hasSystemLibrary = allClasspaths.stream().anyMatch(cp -> cp.getModule(TypeConstants.JAVA_DOT_BASE) != null); + if (!hasSystemLibrary && this.project != null) { + // not found in allClasspaths, try this.project instead: + try { + // try module java.base: + for (IPackageFragmentRoot root : this.project.getAllPackageFragmentRoots()) { + IModuleDescription moduleDescription = root.getModuleDescription(); + if (moduleDescription != null) { + hasModule = true; + if (moduleDescription.getElementName().equals(String.valueOf(TypeConstants.JAVA_DOT_BASE))) { + hasSystemLibrary = true; + break; + } + } + } + } catch (JavaModelException e) { + exception = e; + } + if (!hasModule) { + try { + // if no modules try class java.lang.Object: + hasSystemLibrary = this.project.findType(String.valueOf(TypeConstants.CharArray_JAVA_LANG_OBJECT)) != null; + } catch (JavaModelException e) { + exception = e; + } + } + } + if (!hasSystemLibrary) + throw new IllegalStateException("Missing system library", exception); //$NON-NLS-1$ + } + } /** * Sets all the setting to their default values. */