fix(extract): emit references for Java field types (#1485)

Java field declarations produced no `references` edge for their type, so a class's
data dependencies (its field types) were missing from the graph even though
parameter and return types were already captured. The field handler now collects
the declared type via the same `_java_collect_type_refs` helper used elsewhere,
preserving the `field` and `generic_arg` contexts and skipping primitives
(int/boolean/etc.), matching the existing C#/PHP/Kotlin field handlers.

Ported from PR #1485 by @oleksii-tumanov.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
oleksii-tumanov
2026-06-27 10:19:15 +01:00
committed by safishamsi
co-authored by Claude Opus 4.8
parent 8994b5500c
commit 31b3752902
2 changed files with 35 additions and 0 deletions
+16
View File
@@ -2964,6 +2964,22 @@ def _extract_generic(
"references", line, context="field")
return
if (config.ts_module == "tree_sitter_java"
and t == "field_declaration"
and parent_class_nid):
type_node = node.child_by_field_name("type")
if type_node is not None:
line = node.start_point[0] + 1
refs: list[tuple[str, str]] = []
_java_collect_type_refs(type_node, source, False, refs)
for ref_name, role in refs:
ctx = "generic_arg" if role == "generic_arg" else "field"
target_nid = ensure_named_node(ref_name, line)
if target_nid != parent_class_nid:
add_edge(parent_class_nid, target_nid, "references",
line, context=ctx)
return
if (config.ts_module == "tree_sitter_php"
and t == "property_declaration"
and parent_class_nid):
+19
View File
@@ -344,6 +344,25 @@ def test_java_parameter_return_generic_and_attribute_contexts():
assert ("build", "Override") in _edge_labels(result, "references", "attribute")
def test_java_field_type_references_have_field_context(tmp_path):
source = tmp_path / "Fields.java"
source.write_text(
"class PaymentGateway {}\n"
"class Handler {}\n"
"class CheckoutService {\n"
" PaymentGateway gateway;\n"
" List<Handler> handlers;\n"
"}\n"
)
result = extract_java(source)
assert ("CheckoutService", "PaymentGateway") in _edge_labels(
result, "references", "field"
)
assert ("CheckoutService", "Handler") in _edge_labels(
result, "references", "generic_arg"
)
def test_csharp_field_type_references_have_field_context():
r = extract_csharp(FIXTURES / "sample.cs")
refs = _references(r)