Last active
November 14, 2025 10:08
-
-
Save sormuras/05696a16b1950208de527ef4cd37a811 to your computer and use it in GitHub Desktop.
Show javac warnings for unknown enum constants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Path lib = Path.of("lib"); | |
| void main() throws Exception { | |
| step("Populate lib directory"); | |
| var guardian = get("org.apiguardian.api", "https://repo.maven.apache.org/maven2/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar"); | |
| var jspecify = get("org.jspecify", "https://repo.maven.apache.org/maven2/org/jspecify/jspecify/1.0.0/jspecify-1.0.0.jar"); | |
| var commons = get("org.junit.platform.commons", "https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-commons/6.0.1/junit-platform-commons-6.0.1.jar"); | |
| step("Describe module org.junit.platform.commons"); | |
| run("jar", "--describe-module", "--file", commons); | |
| var program = Path.of("HelloCommons.java"); | |
| if (!Files.exists(program)) { | |
| step("Create compact HelloCommons.java program"); | |
| Files.writeString(program, | |
| """ | |
| import module org.junit.platform.commons; | |
| void main() { | |
| IO.println(getClass().getModule()); | |
| } | |
| """); | |
| } | |
| var modular = Path.of("mod", "foo", "Modular.java"); | |
| if (!Files.exists(modular)) { | |
| step("Create modular Modular.java program and module descriptor"); | |
| Files.createDirectories(modular.getParent()); | |
| Files.writeString(Path.of("mod", "module-info.java"), | |
| """ | |
| module foo { | |
| requires org.junit.platform.commons; | |
| } | |
| """); | |
| Files.writeString(modular, | |
| """ | |
| package foo; | |
| import module org.junit.platform.commons; | |
| class Modular { | |
| void main() { | |
| IO.println(getClass().getModule()); | |
| } | |
| } | |
| """); | |
| } | |
| step("Run with all modules in play"); | |
| listModules(); | |
| run("java", "--module-path", "lib", "--add-modules", "ALL-MODULE-PATH", program); | |
| run("java", "--module-path", "lib", "--add-modules", "ALL-MODULE-PATH", modular); | |
| step("Run without JSpecify module"); | |
| Files.deleteIfExists(jspecify); | |
| listModules(); | |
| run("java", "--module-path", "lib", "--add-modules", "ALL-MODULE-PATH", program); | |
| run("java", "--module-path", "lib", "--add-modules", "ALL-MODULE-PATH", modular); | |
| step("Run without JSpecify and without API Guardian module"); | |
| Files.deleteIfExists(guardian); | |
| listModules(); | |
| run("java", "--module-path", "lib", "--add-modules", "ALL-MODULE-PATH", program); | |
| run("java", "--module-path", "lib", "--add-modules", "ALL-MODULE-PATH", modular); | |
| } | |
| void step(String caption) { | |
| IO.println(); | |
| IO.println("### " + caption); | |
| IO.println(); | |
| } | |
| Path get(String module, String uri) throws Exception { | |
| Files.createDirectories(lib); | |
| var target = lib.resolve(module + ".jar"); | |
| var source = URI.create(uri); | |
| try (var stream = source.toURL().openStream()) { | |
| IO.println(module + " <- " + source + "..."); | |
| Files.copy(stream, target, StandardCopyOption.REPLACE_EXISTING); | |
| } | |
| return target; | |
| } | |
| void listModules() { | |
| var finder = ModuleFinder.of(lib); | |
| var modules = finder.findAll(); | |
| modules.stream() | |
| .map(ModuleReference::descriptor) | |
| .map(ModuleDescriptor::toNameAndVersion) | |
| .sorted() | |
| .forEach(IO::println); | |
| var size = modules.size(); | |
| IO.println(" %d module%s".formatted(size, size == 1 ? "" : "s")); | |
| } | |
| void run(String tool, Object... args) throws Exception { | |
| var program = Path.of(System.getProperty("java.home", ""), "bin", tool); | |
| var builder = new ProcessBuilder(program.toString()).inheritIO(); | |
| builder.command().addAll(Stream.of(args).map(Object::toString).toList()); | |
| IO.println("| " + String.join(" ", builder.command())); | |
| var process = builder.start(); | |
| var code = process.waitFor(); | |
| if (code == 0) return; | |
| throw new Error(tool + " returned a non-zero exit code: " + code); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Run with all modules in play | |
| org.apiguardian.api@1.1.2 | |
| org.jspecify | |
| org.junit.platform.commons@6.0.1 | |
| 3 modules | |
| | J:\jdks\jdk-25\bin\java --module-path lib --add-modules ALL-MODULE-PATH HelloCommons.java | |
| unnamed module @10b48321 | |
| | J:\jdks\jdk-25\bin\java --module-path lib --add-modules ALL-MODULE-PATH mod\foo\Modular.java | |
| module foo | |
| ### Run without JSpecify module | |
| org.apiguardian.api@1.1.2 | |
| org.junit.platform.commons@6.0.1 | |
| 2 modules | |
| | J:\jdks\jdk-25\bin\java --module-path lib --add-modules ALL-MODULE-PATH HelloCommons.java | |
| unnamed module @7fad8c79 | |
| | J:\jdks\jdk-25\bin\java --module-path lib --add-modules ALL-MODULE-PATH mod\foo\Modular.java | |
| error: module not found: org.jspecify | |
| 1 error | |
| error: compilation failed | |
| Exception in thread "main" java.lang.Error: java returned a non-zero exit code: 1 | |
| at CompilerWarningsDemo.run(CompilerWarningsDemo.java:104) | |
| at CompilerWarningsDemo.main(CompilerWarningsDemo.java:58 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment