diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java index 7cf419f3ca8..73219ca38ae 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java @@ -128,6 +128,7 @@ public class JavacElements implements Elements { @Override @DefinedBy(Api.LANGUAGE_MODEL) public Set getAllModuleElements() { + ensureEntered("getAllModuleElements"); if (allowModules) return Collections.unmodifiableSet(modules.allModules()); else diff --git a/test/jdk/tools/sincechecker/SinceChecker.java b/test/jdk/tools/sincechecker/SinceChecker.java index a7ec90d53f8..f9a8e3d49dd 100644 --- a/test/jdk/tools/sincechecker/SinceChecker.java +++ b/test/jdk/tools/sincechecker/SinceChecker.java @@ -297,7 +297,6 @@ public class SinceChecker { Collections.singletonList(SimpleJavaFileObject.forSource(URI.create("myfo:/Test.java"), ""))); ct.analyze(); Elements elements = ct.getElements(); - elements.getModuleElement("java.base"); try (EffectiveSourceSinceHelper javadocHelper = EffectiveSourceSinceHelper.create(ct, List.of(root), this)) { processModuleCheck(elements.getModuleElement(moduleName), ct, moduleDirectory, javadocHelper); } catch (Exception e) { diff --git a/test/langtools/tools/javac/processing/model/util/elements/TestElementsProgrammatic.java b/test/langtools/tools/javac/processing/model/util/elements/TestElementsProgrammatic.java new file mode 100644 index 00000000000..dda3faac232 --- /dev/null +++ b/test/langtools/tools/javac/processing/model/util/elements/TestElementsProgrammatic.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8341342 + * @summary Test Elements methods programmatically + * @modules jdk.compiler + * @run junit TestElementsProgrammatic + */ + +import com.sun.source.util.JavacTask; +import java.net.URI; +import java.util.List; +import java.util.function.Consumer; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.SimpleJavaFileObject; +import javax.tools.ToolProvider; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Programmatically test workings of various methods of Elements. + */ +public class TestElementsProgrammatic { + + private final JavaCompiler systemCompiler = ToolProvider.getSystemJavaCompiler(); + + @ParameterizedTest + @MethodSource + public void automaticallyEnter(Consumer verify) { + //make sure the get{All,}{Module,Package,Type}Element{s,} methods will automatically enter: + JavaFileObject input = + SimpleJavaFileObject.forSource(URI.create("mem://Test.java"), ""); + List inputs = List.of(input); + JavacTask task = (JavacTask) systemCompiler.getTask(null, null, null, null, null, inputs); + verify.accept(task); + } + + private static List> automaticallyEnter() { + return List.of( + task -> assertFalse(task.getElements().getAllModuleElements().isEmpty()), + task -> assertNotNull(task.getElements().getModuleElement("java.base")), + task -> assertNotNull(task.getElements().getPackageElement("java.lang")), + task -> assertFalse(task.getElements().getAllPackageElements("java.lang").isEmpty()), + task -> assertNotNull(task.getElements().getTypeElement("java.lang.Object")), + task -> assertFalse(task.getElements().getAllTypeElements("java.lang.Object").isEmpty()) + ); + } +}