Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class JavaBaseImport {

private final List<String> listFromUtilPackage = List.of("a", "b", "c");
private final File fileFromIoPackage = new File("file.txt");
final List<String> listFromUtilPackage = List.of("a", "b", "c");
final File fileFromIoPackage = new File("file.txt");

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pl.mperor.lab.java;

import module java.desktop;
import java.util.List; // single-type import wins over the module import below, resolving List as java.util.List

/// Demonstrates a caveat called out by JEP 511: `import module java.desktop` imports every package
/// exported by java.desktop (including `java.awt`, which defines `java.awt.List`) and, because
/// java.desktop is not java.base, also every package exported by java.base (including `java.util`,
/// which defines `java.util.List`). The bare name `List` is therefore ambiguous between the two
/// types; without the single-type import above, this file would not compile.
class JavaDesktopImportAmbiguity {

final List<String> names = List.of("John Doe", "Jane Doe");

}
16 changes: 16 additions & 0 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java25.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,20 @@ public void testScopedValueHasNoSetUnlikeThreadLocal() {
Assertions.assertFalse(USER.isBound());
}

@Test
public void testModuleImportBringsInAllExportedPackagesOfJavaBase() {
var demo = new JavaBaseImport();
// List.of(...) (java.util) and new File(...) (java.io) are two different java.base packages,
// both resolved inside JavaBaseImport.java by its single `import module java.base;` declaration
Assertions.assertEquals(java.util.List.of("a", "b", "c"), demo.listFromUtilPackage);
Assertions.assertEquals("file.txt", demo.fileFromIoPackage.getName());
}

@Test
public void testModuleImportAmbiguityResolvedBySingleTypeImport() {
var demo = new JavaDesktopImportAmbiguity();
// proof that `names` really is java.util.List, not java.awt.List, see JavaDesktopImportAmbiguity.java
Assertions.assertEquals(java.util.List.of("John Doe", "Jane Doe"), demo.names);
}

}
Loading