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 51716a9a870..fb54d6f1b46 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, 2024 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -21,6 +21,7 @@ import java.util.Map; import java.util.Set; import junit.framework.Test; +import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.ICompilationUnit; @@ -1445,4 +1446,43 @@ public void acceptAST(String sourceFilePath, CompilationUnit cu) { parser.createASTs(paths, null, new String[] {}, new MyFileASTRequestor() {}, null); assertEquals(expectedProblems, actualProblems); } +public void testGH3298() 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); + + createProject("GH3298"); + IFile javaFile = createFile("GH3298/Test.java", "public class Test{}"); + ICompilationUnit javaElement = JavaCore.createCompilationUnitFrom(javaFile); + + try { + ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); + parser.setSource(javaElement); + parser.setCompilerOptions(options); + parser.setResolveBindings(true); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + assertNotNull(parser.createAST(null)); + + addJavaNature("GH3298"); + parser.setSource(javaElement); + parser.setCompilerOptions(options); + parser.setResolveBindings(true); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + try { + parser.createAST(null); + fail("Expected exception was not thrown"); + } catch (IllegalStateException ise) { + assertEquals("Missing system library", ise.getMessage()); + } + + parser.setSource(javaElement); + parser.setCompilerOptions(options); + parser.setResolveBindings(false); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + assertNotNull(parser.createAST(null)); + } finally { + deleteProject("GH3298"); + } +} } 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 75190f4430c..85533a1b6af 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 @@ -272,6 +272,9 @@ private List getClasspath() throws IllegalStateException { } private void checkForSystemLibrary(List allClasspaths) { + if (!hasJavaNature()) { + return; + } boolean hasSystemLibrary = true; // default for 1.8 setting without a valid project boolean hasModule = false; Throwable exception = null; @@ -1656,4 +1659,8 @@ private void rootNodeToCompilationUnit(AST ast, CompilationUnit compilationUnit, } } } + + private boolean hasJavaNature() { + return this.project == null || JavaProject.hasJavaNature(this.project.getProject()); + } }