Skip to content

Commit bb33628

Browse files
committed
fix(java): extract method name from identifier, not return type
tree-sitter-java places type_identifier (return type) before identifier (method name) in method_declaration nodes. The generic _get_name() loop matched type_identifier first, causing methods to be indexed under their return type instead of their actual name. For example: * `public String getName()` was indexed as "String" instead of "getName", and * `public ConfigBean getUtilityIngestionBean()`was indexed as "ConfigBean". This broke callers_of,callees_of, and children_of queries for any Java method with a non-void, non-generic return type. Adds a Java-specific branch in _get_name() that returns the first identifier child for method_declaration nodes, following the same pattern as the Go fix (field_identifier) from PR #166. Kotlin & Scala is unaffected — its syntax places the name before the return type.
1 parent 14a9185 commit bb33628

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

code_review_graph/parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,6 +3357,16 @@ def _get_name(self, node, language: str, kind: str) -> Optional[str]:
33573357
for child in node.children:
33583358
if child.type == "field_identifier":
33593359
return child.text.decode("utf-8", errors="replace")
3360+
# Java methods: tree-sitter-java puts type_identifier or generic_type
3361+
# (return type) before identifier (method name). Must run before
3362+
# the generic loop, which would match the return type's
3363+
# type_identifier (e.g. "String", "ConfigBean").
3364+
# Constructors are fine — they have no return type node.
3365+
# Kotlin is unaffected: its syntax places the name before the type.
3366+
if language == "java" and node.type == "method_declaration":
3367+
for child in node.children:
3368+
if child.type == "identifier":
3369+
return child.text.decode("utf-8", errors="replace")
33603370
# Swift extensions: name is inside user_type > type_identifier
33613371
# (e.g. `extension MyClass: Protocol { ... }`)
33623372
if language == "swift" and node.type == "class_declaration":

tests/test_multilang.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ def test_finds_methods(self):
137137
assert "save" in names
138138
assert "getUser" in names
139139

140+
def test_method_names_not_return_types(self):
141+
"""Method names must be the actual name, not the return type.
142+
143+
tree-sitter-java puts type_identifier (return type) before
144+
identifier (method name). Without the Java-specific branch in
145+
_get_name the generic loop picks up the return type instead.
146+
"""
147+
funcs = [n for n in self.nodes if n.kind == "Function"]
148+
names = {f.name for f in funcs}
149+
# getName()/getEmail() return String — must not be indexed as "String"
150+
assert "getName" in names
151+
assert "getEmail" in names
152+
assert "getId" in names
153+
# createUser() returns User — must not be indexed as "User" (the class)
154+
assert "createUser" in names
155+
140156
def test_finds_imports(self):
141157
imports = [e for e in self.edges if e.kind == "IMPORTS_FROM"]
142158
assert len(imports) >= 2

0 commit comments

Comments
 (0)