8341342: Elements.getAllModuleElements() does not work properly before JavacTask.analyze()

Reviewed-by: vromero, liach
This commit is contained in:
Jan Lahoda 2025-08-14 07:02:08 +00:00
parent 9dcc502cc8
commit c22e01d776
3 changed files with 75 additions and 1 deletions

View File

@ -128,6 +128,7 @@ public class JavacElements implements Elements {
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Set<? extends ModuleElement> getAllModuleElements() {
ensureEntered("getAllModuleElements");
if (allowModules)
return Collections.unmodifiableSet(modules.allModules());
else

View File

@ -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) {

View File

@ -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<JavacTask> 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<JavaFileObject> inputs = List.of(input);
JavacTask task = (JavacTask) systemCompiler.getTask(null, null, null, null, null, inputs);
verify.accept(task);
}
private static List<Consumer<JavacTask>> 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())
);
}
}