Skip to content

Commit

Permalink
[23] JEP 476: Module Import Declarations (Preview)
Browse files Browse the repository at this point in the history
more tests regarding:
+ ambiguity from just one module import (incl. fix)
  • Loading branch information
stephan-herrmann committed May 28, 2024
1 parent ee2f38c commit 7531c5f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3671,19 +3671,34 @@ final Binding getTypeOrPackage(char[] name, int mask, boolean needResolve) {
}

private ReferenceBinding findTypeInModule(char[] name, ModuleBinding moduleBinding, PackageBinding currentPackage) {
ReferenceBinding type = null;
for (PackageBinding packageBinding : moduleBinding.getExports()) {
if (packageBinding.enclosingModule.isPackageExportedTo(packageBinding, module())) {
ReferenceBinding temp = findType(name, packageBinding, currentPackage);
if (temp != null && temp.canBeSeenBy(currentPackage)) // imported only if accessible
return temp;
if (temp != null && temp.canBeSeenBy(currentPackage)) {// imported only if accessible
if (type != null) {
// Answer error binding -- import on demand conflict; name found in two exported packages.
return new ProblemReferenceBinding(new char[][]{name}, temp, ProblemReasons.Ambiguous);
}
type = temp;
}
}
}
for (ModuleBinding required : moduleBinding.getRequiresTransitive()) {
ReferenceBinding temp = findTypeInModule(name, required, currentPackage);
if (temp != null)
return temp;
if (temp != null) {
if (temp.problemId() == ProblemReasons.Ambiguous)
return temp; // don't look further
if (temp.canBeSeenBy(currentPackage)) {
if (type != null) {
// Answer error binding -- import on demand conflict; name found in two modules.
return new ProblemReferenceBinding(new char[][]{name}, temp, ProblemReasons.Ambiguous);
}
type = temp;
}
}
}
return null;
return type;
}

private boolean isUnnecessarySamePackageImport(Binding resolvedImport, Scope unitScope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,82 @@ void m(Connection c) {
OUTPUT_DIR);
}

public void test009_notAccessible() {
public void test009_ambiguous_modules() {
String srcDir = OUTPUT_DIR + File.separator + "src";
List<String> files = new ArrayList<>();
String modOneDir = srcDir + File.separator + "mod.one";
writeFileCollecting(files, modOneDir, "module-info.java",
"""
module mod.one {
exports p1;
exports p2;
requires transitive java.sql;
}
""");
writeFileCollecting(files, modOneDir + File.separator + "p1", "Connection.java",
"""
package p1;
public class Connection {
}
""");
writeFileCollecting(files, modOneDir + File.separator + "p1", "Other.java",
"""
package p1;
public class Other {
}
""");
writeFileCollecting(files, modOneDir + File.separator + "p2", "Other.java",
"""
package p2;
public class Other{
}
""");

String modTwoDir = srcDir + File.separator + "mod.two";
writeFileCollecting(files, modTwoDir, "module-info.java",
"""
module mod.two {
requires mod.one;
}
""");
writeFileCollecting(files, modTwoDir + File.separator + "p3", "Client.java",
"""
package p3;
import module mod.one;
@SuppressWarnings("preview")
class Client {
Connection conn; // module conflict mod.one java.sql
Other other; // package conflict mod.one/p1 mod.one/p2
}
""");
StringBuilder commandLine = new StringBuilder();
commandLine.append(" -23 --enable-preview ");
commandLine.append(" --module-source-path \"").append(srcDir).append("\"");
commandLine.append(" -d \"").append(OUTPUT_DIR).append(File.separatorChar).append("bin").append("\"");

runNegativeModuleTest(
files,
commandLine,
"",
"""
----------
1. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/src/mod.two/p3/Client.java (at line 5)
Connection conn; // module conflict mod.one java.sql
^^^^^^^^^^
The type Connection is ambiguous
----------
2. ERROR in ---OUTPUT_DIR_PLACEHOLDER---/src/mod.two/p3/Client.java (at line 6)
Other other; // package conflict mod.one/p1 mod.one/p2
^^^^^
The type Other is ambiguous
----------
2 problems (2 errors)
""",
"reference to Connection is ambiguous",
OUTPUT_DIR);
}

public void test010_notAccessible() {
String srcDir = OUTPUT_DIR + File.separator + "src";
String modOneDir = srcDir + File.separator + "mod.one";
List<String> files = new ArrayList<>();
Expand Down Expand Up @@ -518,7 +593,7 @@ class Client {
OUTPUT_DIR);
}

public void test010_transitive() {
public void test011_transitive() {
String srcDir = OUTPUT_DIR + File.separator + "src";
String modOneDir = srcDir + File.separator + "mod.one";
List<String> files = new ArrayList<>();
Expand Down Expand Up @@ -570,7 +645,7 @@ class Client {
OUTPUT_DIR);
}

public void test011_redundant() {
public void test012_redundant() {
List<String> files = new ArrayList<>();
writeFileCollecting(files, OUTPUT_DIR + File.separator + "p", "X.java",
"""
Expand Down Expand Up @@ -618,7 +693,7 @@ public static void main(String[] args) {
OUTPUT_DIR);
}

public void test012_inUnnamedModule() {
public void test013_inUnnamedModule() {
runConformModuleTest(
new String[] {
"p/X.java",
Expand Down

0 comments on commit 7531c5f

Please sign in to comment.