Skip to content

Commit

Permalink
ASTParser should detect and report when no suitable system library has
Browse files Browse the repository at this point in the history
been configured #3047
  • Loading branch information
jarthana committed Oct 15, 2024
1 parent 4494017 commit ee344cd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
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
@@ -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,6 +14,7 @@
package org.eclipse.jdt.core.tests.dom;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
Expand All @@ -24,8 +25,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 +1327,62 @@ 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);
class MyFileASTRequestor extends FileASTRequestor {
@Override
public void acceptAST(String sourceFilePath, CompilationUnit cu) {
IProblem[] problems = cu.getProblems();
assertTrue("Number of problems should be at least 1", (problems.length >= 1));
IProblem problem = problems[0];
assertEquals("Unexpected problem in " + sourceFilePath,
"Pb(324) The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files", problem.toString());
}
}
parser.createASTs(paths, null, new String[] {}, new MyFileASTRequestor() {}, null);
}
}

0 comments on commit ee344cd

Please sign in to comment.