Skip to content

Commit

Permalink
Alternative approach to inject 'requires transitive java.base;'
Browse files Browse the repository at this point in the history
  • Loading branch information
lahodaj committed Oct 1, 2024
1 parent 52b755c commit 71f7957
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
8 changes: 1 addition & 7 deletions make/CompileJavaModules.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ CreateHkTargets = \

-include Java.gmk

ifeq ($(TARGET_RELEASE), )
# If unspecified, default to the new jdk we're building
TARGET_RELEASE := $(TARGET_RELEASE_NEWJDK)
endif

################################################################################
# Setup the main compilation

Expand All @@ -113,12 +108,11 @@ $(eval $(call SetupJavaCompilation, $(MODULE), \
EXCLUDE_FILES := $(EXCLUDE_FILES), \
KEEP_ALL_TRANSLATIONS := $(KEEP_ALL_TRANSLATIONS), \
JAVAC_FLAGS := \
$(DOCLINT) -doe \
$(DOCLINT) \
$(JAVAC_FLAGS) \
--module-source-path $(MODULESOURCEPATH) \
--module-path $(MODULEPATH) \
--system none, \
TARGET_RELEASE=$(TARGET_RELEASE) \
))

TARGETS += $($(MODULE))
Expand Down
1 change: 0 additions & 1 deletion make/Docs.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ JAVADOC_OPTIONS := -use -keywords -notimestamp \
-encoding ISO-8859-1 -docencoding UTF-8 -breakiterator \
-splitIndex --system none -javafx --expand-requires transitive \
--override-methods=summary \
--enable-preview --source $(JDK_SOURCE_TARGET_VERSION) \
--no-external-specs-page

# The reference options must stay stable to allow for comparisons across the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public static void main(String... args) throws IOException {
List<String> options = List.of("-source", Source.DEFAULT.name,
"-target", Target.DEFAULT.name,
"-proc:only",
"--enable-preview",
"--system", "none",
"--module-source-path", args[1],
"--add-modules", Arrays.stream(args)
Expand Down
1 change: 0 additions & 1 deletion src/java.se/share/classes/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
* @since 9
*/
module java.se {
requires transitive java.base;
requires transitive java.compiler;
requires transitive java.datatransfer;
requires transitive java.desktop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public class Modules extends JCTree.Visitor {
private final boolean allowModules;
private final boolean allowAccessIntoSystem;
private final boolean allowRequiresTransitiveJavaBase;
private final boolean previewEnabled;

public final boolean multiModuleMode;

Expand Down Expand Up @@ -209,6 +210,7 @@ protected Modules(Context context) {

allowRequiresTransitiveJavaBase =
(Feature.JAVA_BASE_TRANSITIVE.allowedInSource(source) && (!preview.isPreview(Feature.JAVA_BASE_TRANSITIVE) || preview.isEnabled()));
previewEnabled = preview.isEnabled();
lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option);

multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
Expand Down Expand Up @@ -1506,6 +1508,14 @@ private void completeModule(ModuleSymbol msym) {
initAddReads();

msym.requires = msym.requires.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));

if (previewEnabled && msym.name == names.java_se) {
msym.requires.forEach(rd -> {
if (rd.module == syms.java_base) {
rd.flags.add(RequiresFlag.TRANSITIVE);
}
});
}

List<RequiresDirective> requires = msym.requires;

Expand Down
90 changes: 90 additions & 0 deletions test/langtools/tools/javac/modules/EdgeCases.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Symtab;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import javax.lang.model.element.ModuleElement.DirectiveKind;

import toolbox.JarTask;
import toolbox.JavacTask;
Expand Down Expand Up @@ -1153,4 +1156,91 @@ private void record(TaskEvent e, String phase) {
}
}

@Test
public void testJavaSEHasRequiresTransitiveJavaBase(Path base) throws Exception {
Path src = base.resolve("src");
Path a = src.resolve("a");
tb.writeJavaFiles(a,
"module a { requires java.se; }",
"""
package test;
import module java.se;
public class Test {
ArrayList<String> l;
}
""");
Path classes = base.resolve("classes");
tb.createDirectories(classes);

AtomicBoolean seenJavaSEDependency = new AtomicBoolean();

List<String> log;

log = new JavacTask(tb)
.outdir(classes)
.options("-XDrawDiagnostics", "-XDshould-stop.at=FLOW")
.callback(verifyJavaSEDependency(false, seenJavaSEDependency))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);

List<String> expected = List.of(
"Test.java:2:8: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.module.imports)",
"Test.java:4:5: compiler.err.cant.resolve.location: kindname.class, ArrayList, , , (compiler.misc.location: kindname.class, test.Test, null)",
"2 errors");

if (!expected.equals(log))
throw new Exception("expected output not found: " + log);

if (!seenJavaSEDependency.get()) {
throw new AssertionError("Didn't find the java.se dependency!");
}

seenJavaSEDependency.set(false);

new JavacTask(tb)
.outdir(classes)
.options("--enable-preview",
"--source", System.getProperty("java.specification.version"))
.callback(verifyJavaSEDependency(true, seenJavaSEDependency))
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);

if (!seenJavaSEDependency.get()) {
throw new AssertionError("Didn't find the java.se dependency!");
}
}
private Consumer<com.sun.source.util.JavacTask> verifyJavaSEDependency(
boolean expectedTransitive,
AtomicBoolean seenJavaSEDependency) {
return t -> {
t.addTaskListener(new TaskListener() {
@Override
public void finished(TaskEvent e) {
if (e.getKind() == TaskEvent.Kind.ANALYZE) {
ModuleElement javaBase =
t.getElements().getModuleElement("java.base");
ModuleElement javaSE =
t.getElements().getModuleElement("java.se");
RequiresDirective requiresJavaSE =
javaSE.getDirectives()
.stream()
.filter(d -> d.getKind() == DirectiveKind.REQUIRES)
.map(d -> (RequiresDirective) d)
.filter(d -> d.getDependency() == javaBase)
.findAny()
.orElseThrow();
if (requiresJavaSE.isTransitive() != expectedTransitive) {
throw new AssertionError("Expected: " + expectedTransitive + ", " +
"but got: " + requiresJavaSE.isTransitive());
}
seenJavaSEDependency.set(true);
}
}
});
};
}
}

0 comments on commit 71f7957

Please sign in to comment.