Skip to content

Commit 62a7aba

Browse files
authored
fix(java): simplify NodeID name to methodName(paramTypes) (#189)
* fix(java): simplify NodeID name to methodName(paramTypes) Java method NodeIDs were using the JAR-provided Descriptor/RawText directly, which often included modifiers, return type, throws clause, parameter names and annotations. Rebuild the name from MethodDetail's parameter list (TypeRawText, falling back to TypeFqcn) so NodeIDs are stable and concise — e.g. queryJwtToken(String,String,String). * fix(java): strip modifiers/return type from method NodeID GetName() only splits Descriptor at '(' so when the JAR returns the long form (e.g. "abstract void foo()"), modifiers and return type leaked into the NodeID name. Take the last whitespace-delimited token after GetName(), and drop empty parens for no-arg methods so the exported "Class.method" form stays clean.
1 parent b6f1fb5 commit 62a7aba

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

lang/collect/collect.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,7 @@ func (c *Collector) ScannerByJavaIPC(ctx context.Context) ([]*DocumentSymbol, er
830830
if m == nil {
831831
continue
832832
}
833-
name := m.Descriptor
834-
if name == "" {
835-
name = m.GetName()
836-
}
833+
name := buildJavaMethodID(m)
837834
if name == "" {
838835
continue
839836
}
@@ -2524,6 +2521,45 @@ func (c *Collector) extractRootIdentifier(node *sitter.Node, content []byte) str
25242521
return ""
25252522
}
25262523

2524+
// buildJavaMethodID generates the simplified NodeID.Name for a Java method:
2525+
// methodName(ParamRawType1,ParamRawType2,...)
2526+
// Strips access modifiers, static/final/etc., annotations, return type, throws,
2527+
// and parameter names. Prefers ParameterDetail.TypeRawText (preserves generics
2528+
// and array notation as written) and falls back to TypeFqcn.
2529+
func buildJavaMethodID(m *javapb.MethodDetail) string {
2530+
if m == nil {
2531+
return ""
2532+
}
2533+
name := strings.TrimSpace(m.GetName())
2534+
// GetName() may still contain modifiers + return type when the JAR's
2535+
// Descriptor uses the long form (e.g. "abstract void foo"). Take the last
2536+
// token after any space.
2537+
if sp := strings.LastIndexByte(name, ' '); sp >= 0 {
2538+
name = name[sp+1:]
2539+
}
2540+
if name == "" {
2541+
return ""
2542+
}
2543+
types := make([]string, 0, len(m.Parameters))
2544+
for _, p := range m.Parameters {
2545+
if p == nil {
2546+
continue
2547+
}
2548+
t := p.TypeRawText
2549+
if t == "" {
2550+
t = p.TypeFqcn
2551+
}
2552+
if t == "" {
2553+
continue
2554+
}
2555+
types = append(types, t)
2556+
}
2557+
if len(types) == 0 {
2558+
return name
2559+
}
2560+
return name + "(" + strings.Join(types, ",") + ")"
2561+
}
2562+
25272563
// parseMethodSignature 从方法节点解析签名,保留方法名和参数类型
25282564
// 例如: public String queryJwtToken(String id, String tenantId, String idType) -> queryJwtToken(String, String, String)
25292565
// 例如: forwardLarkEvent(Map<String, Object>) -> forwardLarkEvent(Map<String, Object>)

0 commit comments

Comments
 (0)