Skip to content

Commit

Permalink
Backport bb93f67
Browse files Browse the repository at this point in the history
  • Loading branch information
lahodaj committed Jan 14, 2025
1 parent e76cc44 commit 05a0063
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 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
Expand Down Expand Up @@ -103,7 +103,6 @@
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
Expand Down Expand Up @@ -151,7 +150,6 @@ public class Modules extends JCTree.Visitor {
private final Target target;
private final boolean allowModules;
private final boolean allowAccessIntoSystem;
private final boolean allowRequiresTransitiveJavaBase;

public final boolean multiModuleMode;

Expand Down Expand Up @@ -207,11 +205,6 @@ protected Modules(Context context) {

allowAccessIntoSystem = options.isUnset(Option.RELEASE);

Preview preview = Preview.instance(context);

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

multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
Expand Down Expand Up @@ -822,12 +815,10 @@ public void visitRequires(JCRequires tree) {
Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class);
if (tree.isTransitive) {
if (msym == syms.java_base &&
!allowRequiresTransitiveJavaBase &&
!preview.participatesInPreview(syms, sym)) {
if (source.compareTo(Source.JDK10) >= 0) {
log.error(DiagnosticFlag.SOURCE_LEVEL,
tree.pos(),
Feature.JAVA_BASE_TRANSITIVE.error(source.name));
preview.checkSourceLevel(tree.pos(),
Feature.JAVA_BASE_TRANSITIVE);
}
}
flags.add(RequiresFlag.TRANSITIVE);
Expand Down
85 changes: 83 additions & 2 deletions test/langtools/tools/javac/ImportModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 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
Expand All @@ -23,7 +23,7 @@

/**
* @test
* @bug 8328481 8332236 8332890 8344647
* @bug 8328481 8332236 8332890 8344647 8347646
* @summary Check behavior of module imports.
* @library /tools/lib
* @modules java.logging
Expand All @@ -39,6 +39,7 @@
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskEvent.Kind;
import com.sun.source.util.TaskListener;
import java.lang.classfile.ClassFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -966,4 +967,84 @@ public class Object {}
.run(Task.Expect.SUCCESS)
.writeAll();
}

@Test //JDK-8347646
public void testRequiresTransitiveJavaBase(Path base) throws Exception {
Path current = base.resolve(".");
Path src = current.resolve("src");
Path classes = current.resolve("classes");
Path ma = src.resolve("ma");
Path maClasses = classes.resolve("ma");
tb.writeJavaFiles(ma,
"""
module ma {
requires transitive java.base;
}
""");
Path test = src.resolve("test");
tb.writeJavaFiles(test,
"""
module test {
requires ma;
}
""",
"""
package test;
import module ma;
public class Test {
public static void main(String... args) {
System.out.println(List.of("Hello"));
}
}
""");

Files.createDirectories(maClasses);

List<String> actualErrors = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(maClasses)
.files(tb.findJavaFiles(ma))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);

List<String> expectedErrors = List.of(
"module-info.java:2:4: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.java.base.transitive)",
"1 error"
);

if (!Objects.equals(expectedErrors, actualErrors)) {
throw new AssertionError("Incorrect Output, expected: " + expectedErrors +
", actual: " + actualErrors);

}

new JavacTask(tb)
.options("-XDrawDiagnostics",
"--source", "9")
.outdir(maClasses)
.files(tb.findJavaFiles(ma))
.run()
.writeAll();

Path maModuleInfo = maClasses.resolve("module-info.class");

if (ClassFile.of().parse(maModuleInfo).minorVersion() == ClassFile.PREVIEW_MINOR_VERSION) {
throw new AssertionError("wrong minor version");
}

new JavacTask(tb)
.options("-XDrawDiagnostics",
"--enable-preview", "--release", SOURCE_VERSION)
.outdir(maClasses)
.files(tb.findJavaFiles(ma))
.run()
.writeAll();

Path maModuleInfo2 = maClasses.resolve("module-info.class");

if (ClassFile.of().parse(maModuleInfo2).minorVersion() != ClassFile.PREVIEW_MINOR_VERSION) {
throw new AssertionError("wrong minor version");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 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
Expand All @@ -21,7 +21,7 @@
* questions.
*/

// key: compiler.err.feature.not.supported.in.source.plural
// key: compiler.err.preview.feature.disabled.plural
// key: compiler.misc.feature.java.base.transitive

module m {
Expand Down
4 changes: 2 additions & 2 deletions test/langtools/tools/javac/modules/JavaBaseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 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
Expand Down Expand Up @@ -166,7 +166,7 @@ void testSource(Path base, List<String> mods, String target) throws Exception {
for (String mod : mods) {
String key = mod.equals("static")
? "compiler.err.mod.not.allowed.here: " + mod
: "compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.java.base.transitive)";
: "compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.java.base.transitive)";
String message = "module-info.java:1:12: " + key;
if (log.contains(message)) {
foundErrorMessage = true;
Expand Down

0 comments on commit 05a0063

Please sign in to comment.