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

ASTParser should detect and report when no suitable system library has been configured #3047 #3063

Merged
merged 2 commits into from
Oct 15, 2024
Merged
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
35 changes: 35 additions & 0 deletions JCL/converterJclMin9/src/java/io/PrintStream.java
Original file line number Diff line number Diff line change
@@ -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) {
}
}
5 changes: 5 additions & 0 deletions JCL/converterJclMin9/src/java/lang/AutoCloseable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package java.lang;

public interface AutoCloseable {
void close() throws Exception;
}
20 changes: 20 additions & 0 deletions JCL/converterJclMin9/src/java/lang/Override.java
Original file line number Diff line number Diff line change
@@ -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 {
}
24 changes: 24 additions & 0 deletions JCL/converterJclMin9/src/java/lang/System.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Binary file modified org.eclipse.jdt.core.tests.model/JCL/converterJclMin9.jar
Binary file not shown.
Binary file modified org.eclipse.jdt.core.tests.model/JCL/converterJclMin9src.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -1324,4 +1330,119 @@ public void testBug381503() throws CoreException, IOException {
deleteProject("P");
}
}
public void testGH3047() throws Exception {
Hashtable<String, String> 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<String, String> 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<String> 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<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="var" path="CONVERTER_JCL18_LIB" sourcepath="CONVERTER_JCL18_SRC" rootpath="CONVERTER_JCL_SRCROOT"/>
<classpathentry kind="var" path="CONVERTER_JCL9_LIB" sourcepath="CONVERTER_JCL9_SRC" rootpath="CONVERTER_JCL_SRCROOT">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
Loading
Loading