From 019eba058356539d23d4212a9387c5821d4a3f47 Mon Sep 17 00:00:00 2001 From: Shawn Date: Mon, 4 May 2026 15:51:34 +0800 Subject: [PATCH 1/3] fix: extract JVM symbol names from declarations --- src/services/graph-symbols.ts | 38 +++++++++++++++++++++++++------- tests/unit/graph-symbols.test.ts | 35 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/services/graph-symbols.ts b/src/services/graph-symbols.ts index fc33f45..0329a30 100644 --- a/src/services/graph-symbols.ts +++ b/src/services/graph-symbols.ts @@ -470,10 +470,8 @@ function extractFromJvm( : ["class_declaration", "interface_declaration", "enum_declaration", "object_declaration"]; for (const k of classKinds) { for (const cls of safeFindAll(root, k)) { - const nameNode = cls.find({ rule: { kind: "type_identifier" } }) - ?? cls.find({ rule: { kind: "identifier" } }); - if (!nameNode) continue; - const name = nameNode.text(); + const name = extractJvmTypeName(cls.text(), langKey); + if (!name) continue; const r = cls.range(); const startLine = r.start.line + 1; const endLine = r.end.line + 1; @@ -496,10 +494,8 @@ function extractFromJvm( : ["method_declaration", "constructor_declaration"]; for (const k of methodKinds) { for (const m of safeFindAll(root, k)) { - const nameNode = m.find({ rule: { kind: "identifier" } }) - ?? m.find({ rule: { kind: "simple_identifier" } }); - if (!nameNode) continue; - const name = nameNode.text(); + const name = extractJvmCallableName(m.text()); + if (!name) continue; const r = m.range(); const startLine = r.start.line + 1; const endLine = r.end.line + 1; @@ -533,6 +529,32 @@ function extractFromJvm( return { symbols, rawCalls }; } +function stripJvmAnnotations(text: string): string { + return text + .split("\n") + .filter((line) => !line.trim().startsWith("@")) + .join("\n"); +} + +function extractJvmTypeName(text: string, langKey: string): string | null { + const withoutAnnotations = stripJvmAnnotations(text); + const header = withoutAnnotations.split("{", 1)[0] ?? withoutAnnotations; + const pattern = langKey === "scala" + ? /\b(?:class|object|trait)\s+([A-Za-z_$][\w$]*)\b/ + : /\b(?:class|interface|enum|object)\s+([A-Za-z_$][\w$]*)\b/; + return header.match(pattern)?.[1] ?? null; +} + +function extractJvmCallableName(text: string): string | null { + const withoutAnnotations = stripJvmAnnotations(text); + const signature = withoutAnnotations + .split("{", 1)[0] + .split("=", 1)[0] + .trim(); + const matches = Array.from(signature.matchAll(/([A-Za-z_$][\w$]*)\s*\(/g)); + return matches.length > 0 ? matches[matches.length - 1][1] : null; +} + // ── C# ────────────────────────────────────────────────────────────────── function extractFromCSharp( diff --git a/tests/unit/graph-symbols.test.ts b/tests/unit/graph-symbols.test.ts index 929699f..dc8180f 100644 --- a/tests/unit/graph-symbols.test.ts +++ b/tests/unit/graph-symbols.test.ts @@ -149,6 +149,41 @@ public class Foo { expect(names).toContain("baz"); }); + it("prefers the declared Java class name over parameter types in Spring Boot entrypoints", () => { + const src = ` +@SpringBootApplication +public class WorkflowFlowableApplication { + public static void main(String[] args) { + SpringApplication.run(WorkflowFlowableApplication.class, args); + } +} +`; + const out = extractSymbolsAndCalls(src, "java" as unknown as Lang, ".java", "WorkflowFlowableApplication.java"); + const names = out.symbols.map((s) => s.name); + expect(names).toContain("WorkflowFlowableApplication"); + expect(names).not.toContain("String"); + expect(names).toContain("main"); + }); + + it("does not treat Java test annotations as method names", () => { + const src = ` +class SecurityAuthClientRequireSubjectTest { + @AfterEach + void cleanup() {} + + @Test + void requireSubjectThrows() {} +} +`; + const out = extractSymbolsAndCalls(src, "java" as unknown as Lang, ".java", "SecurityAuthClientRequireSubjectTest.java"); + const names = out.symbols.map((s) => s.name); + expect(names).toContain("SecurityAuthClientRequireSubjectTest"); + expect(names).toContain("cleanup"); + expect(names).toContain("requireSubjectThrows"); + expect(names).not.toContain("AfterEach"); + expect(names).not.toContain("Test"); + }); + it("extracts Kotlin top-level fun and class methods", () => { const src = ` fun greet(name: String): String = "Hi" From 6a76ad478275d6e65d58d00684414f4936ef4f83 Mon Sep 17 00:00:00 2001 From: Shawn Date: Mon, 4 May 2026 16:07:01 +0800 Subject: [PATCH 2/3] fix: cover JVM annotation and Scala callable edge cases --- src/services/graph-symbols.ts | 8 +++++++- tests/unit/graph-symbols.test.ts | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/services/graph-symbols.ts b/src/services/graph-symbols.ts index 0329a30..b435cb2 100644 --- a/src/services/graph-symbols.ts +++ b/src/services/graph-symbols.ts @@ -532,7 +532,9 @@ function extractFromJvm( function stripJvmAnnotations(text: string): string { return text .split("\n") - .filter((line) => !line.trim().startsWith("@")) + .map((line) => + line.replace(/^\s*(?:@(?:[\w$]+:)?[\w$.]+(?:\([^)]*\))?\s*)+/, "") + ) .join("\n"); } @@ -551,6 +553,10 @@ function extractJvmCallableName(text: string): string | null { .split("{", 1)[0] .split("=", 1)[0] .trim(); + const scalaDefMatches = Array.from(signature.matchAll(/\bdef\s+([A-Za-z_$][\w$]*)\b/g)); + if (scalaDefMatches.length > 0) { + return scalaDefMatches[scalaDefMatches.length - 1][1]; + } const matches = Array.from(signature.matchAll(/([A-Za-z_$][\w$]*)\s*\(/g)); return matches.length > 0 ? matches[matches.length - 1][1] : null; } diff --git a/tests/unit/graph-symbols.test.ts b/tests/unit/graph-symbols.test.ts index dc8180f..2ac6a48 100644 --- a/tests/unit/graph-symbols.test.ts +++ b/tests/unit/graph-symbols.test.ts @@ -184,6 +184,19 @@ class SecurityAuthClientRequireSubjectTest { expect(names).not.toContain("Test"); }); + it("preserves Java declarations when annotations share the same line", () => { + const src = ` +class InlineAnnotationTest { + @Test void cleanup() {} +} +`; + const out = extractSymbolsAndCalls(src, "java" as unknown as Lang, ".java", "InlineAnnotationTest.java"); + const names = out.symbols.map((s) => s.name); + expect(names).toContain("InlineAnnotationTest"); + expect(names).toContain("cleanup"); + expect(names).not.toContain("Test"); + }); + it("extracts Kotlin top-level fun and class methods", () => { const src = ` fun greet(name: String): String = "Hi" @@ -203,6 +216,8 @@ class Bar { const src = ` class Foo { def bar(): Int = 1 + def size: Int = 1 + def now = Instant.now() } object Main { @@ -212,6 +227,8 @@ object Main { const out = extractSymbolsAndCalls(src, "scala" as unknown as Lang, ".scala", "Main.scala"); const names = out.symbols.map((s) => s.name); expect(names).toContain("bar"); + expect(names).toContain("size"); + expect(names).toContain("now"); expect(names).toContain("main"); }); }); From 1dbc1eb398014d418f97879a875d003c28f8b608 Mon Sep 17 00:00:00 2001 From: Shawn Date: Mon, 4 May 2026 16:32:51 +0800 Subject: [PATCH 3/3] test: cover JVM annotations with parameters --- tests/unit/graph-symbols.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit/graph-symbols.test.ts b/tests/unit/graph-symbols.test.ts index 2ac6a48..99f4710 100644 --- a/tests/unit/graph-symbols.test.ts +++ b/tests/unit/graph-symbols.test.ts @@ -173,6 +173,9 @@ class SecurityAuthClientRequireSubjectTest { @Test void requireSubjectThrows() {} + + @Test(timeout = 1000) + void fastTest() {} } `; const out = extractSymbolsAndCalls(src, "java" as unknown as Lang, ".java", "SecurityAuthClientRequireSubjectTest.java"); @@ -180,6 +183,7 @@ class SecurityAuthClientRequireSubjectTest { expect(names).toContain("SecurityAuthClientRequireSubjectTest"); expect(names).toContain("cleanup"); expect(names).toContain("requireSubjectThrows"); + expect(names).toContain("fastTest"); expect(names).not.toContain("AfterEach"); expect(names).not.toContain("Test"); });