Skip to content

Commit c22e01d

Browse files
committed
8341342: Elements.getAllModuleElements() does not work properly before JavacTask.analyze()
Reviewed-by: vromero, liach
1 parent 9dcc502 commit c22e01d

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ protected JavacElements(Context context) {
128128

129129
@Override @DefinedBy(Api.LANGUAGE_MODEL)
130130
public Set<? extends ModuleElement> getAllModuleElements() {
131+
ensureEntered("getAllModuleElements");
131132
if (allowModules)
132133
return Collections.unmodifiableSet(modules.allModules());
133134
else

test/jdk/tools/sincechecker/SinceChecker.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ private void checkModule(String moduleName) throws Exception {
297297
Collections.singletonList(SimpleJavaFileObject.forSource(URI.create("myfo:/Test.java"), "")));
298298
ct.analyze();
299299
Elements elements = ct.getElements();
300-
elements.getModuleElement("java.base");
301300
try (EffectiveSourceSinceHelper javadocHelper = EffectiveSourceSinceHelper.create(ct, List.of(root), this)) {
302301
processModuleCheck(elements.getModuleElement(moduleName), ct, moduleDirectory, javadocHelper);
303302
} catch (Exception e) {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8341342
27+
* @summary Test Elements methods programmatically
28+
* @modules jdk.compiler
29+
* @run junit TestElementsProgrammatic
30+
*/
31+
32+
import com.sun.source.util.JavacTask;
33+
import java.net.URI;
34+
import java.util.List;
35+
import java.util.function.Consumer;
36+
import javax.tools.JavaCompiler;
37+
import javax.tools.JavaFileObject;
38+
import javax.tools.SimpleJavaFileObject;
39+
import javax.tools.ToolProvider;
40+
41+
import org.junit.jupiter.params.ParameterizedTest;
42+
import org.junit.jupiter.params.provider.MethodSource;
43+
44+
import static org.junit.jupiter.api.Assertions.*;
45+
46+
/**
47+
* Programmatically test workings of various methods of Elements.
48+
*/
49+
public class TestElementsProgrammatic {
50+
51+
private final JavaCompiler systemCompiler = ToolProvider.getSystemJavaCompiler();
52+
53+
@ParameterizedTest
54+
@MethodSource
55+
public void automaticallyEnter(Consumer<JavacTask> verify) {
56+
//make sure the get{All,}{Module,Package,Type}Element{s,} methods will automatically enter:
57+
JavaFileObject input =
58+
SimpleJavaFileObject.forSource(URI.create("mem://Test.java"), "");
59+
List<JavaFileObject> inputs = List.of(input);
60+
JavacTask task = (JavacTask) systemCompiler.getTask(null, null, null, null, null, inputs);
61+
verify.accept(task);
62+
}
63+
64+
private static List<Consumer<JavacTask>> automaticallyEnter() {
65+
return List.of(
66+
task -> assertFalse(task.getElements().getAllModuleElements().isEmpty()),
67+
task -> assertNotNull(task.getElements().getModuleElement("java.base")),
68+
task -> assertNotNull(task.getElements().getPackageElement("java.lang")),
69+
task -> assertFalse(task.getElements().getAllPackageElements("java.lang").isEmpty()),
70+
task -> assertNotNull(task.getElements().getTypeElement("java.lang.Object")),
71+
task -> assertFalse(task.getElements().getAllTypeElements("java.lang.Object").isEmpty())
72+
);
73+
}
74+
}

0 commit comments

Comments
 (0)