fix(extract): skip Java type-parameter references (#1518)
The generic-parent support (#1511) emitted a spurious references/generic_arg edge (and a sourceless stub node) for a bare type parameter — the T in `class Box<T> extends Container<T>` — because the Java extractor had no type- parameter tracking. _java_collect_type_refs now collects the in-scope type- parameter names (walking class/interface/record/method/constructor declarations, including bounded and multiple params) and skips references to them, while keeping every real type. Scoped to the declaring node, so a real class that shares a name with a type parameter elsewhere is still referenced. Ported from PR #1518 by @oleksii-tumanov. Closes the nit flagged when #1511 landed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
committed by
safishamsi
co-authored by
Claude Opus 4.8
parent
d177f04270
commit
8b9a99828a
+44
-6
@@ -644,16 +644,54 @@ def _csharp_attribute_names(method_node, source: bytes) -> list[str]:
|
||||
return names
|
||||
|
||||
|
||||
def _java_collect_type_refs(node, source: bytes, generic: bool, out: list[tuple[str, str]]) -> None:
|
||||
_JAVA_TYPE_PARAMETER_SCOPE_DECLARATIONS = frozenset({
|
||||
"class_declaration",
|
||||
"interface_declaration",
|
||||
"record_declaration",
|
||||
"method_declaration",
|
||||
"constructor_declaration",
|
||||
})
|
||||
|
||||
|
||||
def _java_type_parameters_in_scope(node, source: bytes) -> frozenset[str]:
|
||||
"""Return Java type-parameter names visible from ``node``."""
|
||||
names: set[str] = set()
|
||||
scope = node
|
||||
while scope is not None:
|
||||
if scope.type in _JAVA_TYPE_PARAMETER_SCOPE_DECLARATIONS:
|
||||
params = scope.child_by_field_name("type_parameters")
|
||||
if params is not None:
|
||||
for param in params.children:
|
||||
if param.type != "type_parameter":
|
||||
continue
|
||||
name_node = next(
|
||||
(child for child in param.children if child.type == "type_identifier"),
|
||||
None,
|
||||
)
|
||||
if name_node is not None:
|
||||
names.add(_read_text(name_node, source))
|
||||
scope = scope.parent
|
||||
return frozenset(names)
|
||||
|
||||
|
||||
def _java_collect_type_refs(
|
||||
node,
|
||||
source: bytes,
|
||||
generic: bool,
|
||||
out: list[tuple[str, str]],
|
||||
skip: frozenset[str] | None = None,
|
||||
) -> None:
|
||||
"""Walk a Java type expression; append (name, role) tuples."""
|
||||
if node is None:
|
||||
return
|
||||
if skip is None:
|
||||
skip = _java_type_parameters_in_scope(node, source)
|
||||
t = node.type
|
||||
if t in ("integral_type", "floating_point_type", "boolean_type", "void_type"):
|
||||
return
|
||||
if t == "type_identifier":
|
||||
name = _read_text(node, source)
|
||||
if name:
|
||||
if name and name not in skip:
|
||||
out.append((name, "generic_arg" if generic else "type"))
|
||||
return
|
||||
if t == "scoped_type_identifier":
|
||||
@@ -665,24 +703,24 @@ def _java_collect_type_refs(node, source: bytes, generic: bool, out: list[tuple[
|
||||
for c in node.children:
|
||||
if c.type in ("type_identifier", "scoped_type_identifier"):
|
||||
text = _read_text(c, source).rsplit(".", 1)[-1]
|
||||
if text:
|
||||
if text and (c.type == "scoped_type_identifier" or text not in skip):
|
||||
out.append((text, "generic_arg" if generic else "type"))
|
||||
break
|
||||
for c in node.children:
|
||||
if c.type == "type_arguments":
|
||||
for arg in c.children:
|
||||
if arg.is_named:
|
||||
_java_collect_type_refs(arg, source, True, out)
|
||||
_java_collect_type_refs(arg, source, True, out, skip)
|
||||
return
|
||||
if t == "array_type":
|
||||
for c in node.children:
|
||||
if c.is_named:
|
||||
_java_collect_type_refs(c, source, generic, out)
|
||||
_java_collect_type_refs(c, source, generic, out, skip)
|
||||
return
|
||||
if node.is_named:
|
||||
for c in node.children:
|
||||
if c.is_named:
|
||||
_java_collect_type_refs(c, source, generic, out)
|
||||
_java_collect_type_refs(c, source, generic, out, skip)
|
||||
|
||||
|
||||
def _java_annotation_names(declaration_node, source: bytes) -> list[str]:
|
||||
|
||||
@@ -137,6 +137,19 @@ def test_java_record_implements_interface(tmp_path: Path):
|
||||
assert implements, "record implementing an interface should emit an implements edge"
|
||||
|
||||
|
||||
def test_java_type_parameters_do_not_resolve_to_real_class(tmp_path: Path):
|
||||
real_type = _write(tmp_path / "T.java", "public class T {}\n")
|
||||
generic = _write(
|
||||
tmp_path / "Generic.java",
|
||||
"public class Generic<T> { java.util.List<T> values; }\n",
|
||||
)
|
||||
|
||||
result = extract([real_type, generic], cache_root=tmp_path)
|
||||
|
||||
references = _label_edges(result, {"references"})
|
||||
assert ("Generic", "references", "T") not in references
|
||||
|
||||
|
||||
def test_java_cross_file_constructor_call_resolves(tmp_path: Path):
|
||||
# #1373: `new Foo(...)` in a method body must produce a cross-file edge to the
|
||||
# Foo definition. Foo is NOT used as a return type here, so the edge can only
|
||||
|
||||
@@ -379,6 +379,34 @@ def test_java_generic_parents_include_type_argument_references(tmp_path):
|
||||
assert ("DerivedHandler", "Event") in refs
|
||||
|
||||
|
||||
def test_java_type_parameters_do_not_emit_references(tmp_path):
|
||||
source = tmp_path / "TypeParameters.java"
|
||||
source.write_text(
|
||||
"class Payload {}\n"
|
||||
"class Base<X> {}\n"
|
||||
"class Box<T> extends Base<T> {\n"
|
||||
" T value;\n"
|
||||
" List<T> values;\n"
|
||||
" <U> U convert(T input, List<U> mapped, List<Payload> retained) {\n"
|
||||
" return null;\n"
|
||||
" }\n"
|
||||
" <V> Box(V value) {}\n"
|
||||
"}\n"
|
||||
)
|
||||
|
||||
result = extract_java(source)
|
||||
|
||||
references = _references(result)
|
||||
assert not [edge for _, target, edge in references if target in {"T", "U", "V"}]
|
||||
assert not [
|
||||
node
|
||||
for node in result["nodes"]
|
||||
if node.get("label") in {"T", "U", "V"} and not node.get("source_file")
|
||||
]
|
||||
assert ("Box", "Base") in _edge_labels(result, "inherits")
|
||||
assert ("convert", "Payload") in _edge_labels(result, "references", "generic_arg")
|
||||
|
||||
|
||||
def test_java_parameter_return_generic_and_attribute_contexts():
|
||||
result = extract_java(FIXTURES / "sample.java")
|
||||
assert ("build", "HttpClient") in _edge_labels(result, "references", "parameter_type")
|
||||
|
||||
Reference in New Issue
Block a user