diff --git a/src/features/run/java.test.ts b/src/features/run/java.test.ts index 6a4e8764e..6476fbd5a 100644 --- a/src/features/run/java.test.ts +++ b/src/features/run/java.test.ts @@ -69,6 +69,16 @@ describe("scanJvmTargets (Java)", () => { expect(pick("class B {\n void main() {}\n}", "B.java")).toEqual([[1, "main", "B", null, false]]); }); + it("a Java 25 compact source file's top-level main runs the file's implicit class", () => { + const src = "// Run: java Hi.java\nenum Day { MONDAY }\n\nvoid main() {\n IO.println(Day.MONDAY);\n}\n"; + expect(pick(src, "examples/Hi.java")).toEqual([[3, "main", "Hi", null, false]]); + expect(scanJvmTargets(src, "examples/Hi.java", false)[0]).toMatchObject({ instance: true, compact: true }); + expect(pick("static void main(String[] args) {}\nvoid helper() {}", "Tool.java")).toEqual([ + [0, "main", "Tool", null, false], + ]); + expect(pick("void helper() {}\nint main(String[] a) { return 0; }", "Tool.java")).toEqual([]); + }); + it("finds JUnit tests, nested @Nested classes and multi-line annotations", () => { const src = [ "package a.b;", // 0 diff --git a/src/features/run/jvmTargets.ts b/src/features/run/jvmTargets.ts index 8eda9ad1c..fffb7e85e 100644 --- a/src/features/run/jvmTargets.ts +++ b/src/features/run/jvmTargets.ts @@ -19,6 +19,8 @@ export interface JvmTarget { spring?: boolean; /** Java 25 instance `main` (JEP 512): a subclass cannot inherit it on older JDKs. */ instance?: boolean; + /** Java 25 compact source file: a top-level `main` of the file's implicit class (JEP 512). */ + compact?: boolean; } const BOOT_ANNOTATIONS = ["SpringBootApplication", "SpringCloudApplication"]; @@ -310,6 +312,16 @@ function scan(src: string, relPath: string, kotlin: boolean) { instance: !isStatic || undefined, }); } + } else if (!kotlin && !owner && prev === "void" && stringArgs) { + // Compact source file: the implicit class is named after the file, in the unnamed package. + const stem = (relPath.split("/").pop() ?? relPath).replace(/\.java$/, ""); + out.push({ + line: tokens[i]!.line, + kind: "main", + className: stem, + instance: !modifiers.includes("static") || undefined, + compact: true, + }); } } annotations = []; diff --git a/src/features/run/producers.test.ts b/src/features/run/producers.test.ts index acbcd6c6c..1b20c56c2 100644 --- a/src/features/run/producers.test.ts +++ b/src/features/run/producers.test.ts @@ -655,6 +655,10 @@ describe("single-file producers (scratches)", () => { command: "/bin/java", args: ["Hi.java"], }); + // A type declared before the top-level main is a member of the implicit class, not the launch class. + const compact = scratch("Hi.java", "enum Day { MONDAY }\n\nvoid main() {\n IO.println(Day.MONDAY);\n}\n"); + expect(run(compact)).toMatchObject({ command: "/bin/java", args: ["Hi.java"] }); + expect(run({ ...compact, line: 2 })).toMatchObject({ command: "/bin/java", args: ["Hi.java"] }); }); it("Java: the gutter runs the main class it marks, nested ones and those of a JDK on Windows too", () => { diff --git a/src/features/run/producers.ts b/src/features/run/producers.ts index 70fb2e93b..ca3b595b1 100644 --- a/src/features/run/producers.ts +++ b/src/features/run/producers.ts @@ -747,11 +747,11 @@ function javaFile(input: ContextInput, env: ToolEnv, s: FileSite, mainClass?: st config: fileConfig(s, "java-file", java.command, [s.arg], "java-debug"), }; if (input.content === undefined) return source; - const first = firstTopLevelType(input.content); - const mains = scanJvmTargets(input.content, s.idPath, false) - .filter((t) => t.kind === "main") - .map((t) => t.className); + const targets = scanJvmTargets(input.content, s.idPath, false).filter((t) => t.kind === "main"); + const mains = targets.map((t) => t.className); const stem = stemOf(s.file); + // A compact source file's implicit class is the one the source launcher runs. + const first = targets.some((t) => t.compact) ? stem : firstTopLevelType(input.content); const fileMain = mains.find((c) => c === first) ?? mains.find((c) => c.slice(c.lastIndexOf(".") + 1) === stem) ?? mains[0]; const launch = mainClass ?? fileMain;