Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(graph): resolve Java's qualified S.super.m() through the super walk (#3671)

Swati Ahuja33e330390f4b
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index 069e131ed..ba98f2e6d 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -1880,8 +1880,11 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
close it.
"""
class_id = _extract_class_id(caller_id)
+ skip = 1
+ if call.scope_name:
+ class_id, skip = self._qualified_super(caller_id, call.scope_name)
mro = self._mro(class_id) if class_id is not None else None
- for ancestor in (mro or ())[1:]:
+ for ancestor in (mro or ())[skip:]:
if ancestor.startswith(_EXTERNAL_PREFIX):
return None
sym_id = self._declares(ancestor, call.target_name)
@@ -1889,6 +1892,24 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
return ResolvedCall(caller_id, sym_id, 0.90, call.line, "self_inherited")
return None
+ def _qualified_super(self, caller_id: str, qualifier: str) -> tuple[str | None, int]:
+ """Where Java's ``S.super.m()`` starts its walk, and how many classes it skips.
+
+ ``S`` names an enclosing class, whose superclass is meant, or a direct
+ superinterface of the caller's class, whose own ``m`` is.
+ """
+ def named(type_id: str) -> bool:
+ return getattr(self._symbols_by_id.get(type_id), "name", None) == qualifier
+
+ class_id = self._caller_class_id(caller_id)
+ node = class_id
+ while "::" in node:
+ if named(node):
+ return node, 1
+ node = outer_scope(node)
+ interfaces = [p for p in self._heritage_parents.get(class_id, ()) if named(p)]
+ return (interfaces[0], 0) if len(interfaces) == 1 else (None, 0)
+
def _mro(self, class_id: str, visiting: frozenset[str] = frozenset()) -> tuple[str, ...] | None:
"""Python's C3 linearization of *class_id*, or None when it cannot be built.
diff --git a/packages/core/src/repowise/core/ingestion/models.py b/packages/core/src/repowise/core/ingestion/models.py
index c5cac721d..41818950e 100644
--- a/packages/core/src/repowise/core/ingestion/models.py
+++ b/packages/core/src/repowise/core/ingestion/models.py
@@ -326,7 +326,8 @@ class CallSite:
receiver_call: CallReceiver | None = None # set only for a captured chained call
# C++ `Ns::f()` / `Class::m()`: the qualifier as written. Kept apart from
# ``receiver_name`` because that field routes to the member strategies,
- # and cpp has none -- a scoped call must stay on the free-call path.
+ # and cpp has none -- a scoped call must stay on the free-call path. Java
+ # writes the ``S`` of ``S.super.m()`` here.
scope_name: str | None = None
edge_type: CallSiteEdgeType = "calls" # see ``CallSiteEdgeType``
supplied_props: set[str] | None = None # prop names supplied in JSX element (None if unknown/spread)
diff --git a/packages/core/src/repowise/core/ingestion/parser.py b/packages/core/src/repowise/core/ingestion/parser.py
index a64ae6b87..d4c7b64bd 100644
--- a/packages/core/src/repowise/core/ingestion/parser.py
+++ b/packages/core/src/repowise/core/ingestion/parser.py
@@ -469,6 +469,10 @@ def _receiver_text(language: str, node: Node, src: str) -> str | None:
names: a call, subscript or optional hop inside it types nothing, and
dropping the site keeps it off the bare-call tiers.
"""
+ sibling = node.next_named_sibling
+ if language == "java" and node.type != "super" and getattr(sibling, "type", None) == "super":
+ # ``S.super.m()``: ``S`` qualifies the ``super`` its own pattern captures.
+ return None
text = _node_text(node, src).strip()
if (language, node.type) not in _PATH_RECEIVERS:
return text
diff --git a/packages/core/src/repowise/core/ingestion/queries/java.scm b/packages/core/src/repowise/core/ingestion/queries/java.scm
index 5d6d13d92..de3ca28cf 100644
--- a/packages/core/src/repowise/core/ingestion/queries/java.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/java.scm
@@ -107,6 +107,16 @@
arguments: (argument_list) @call.arguments
) @call.site
+; Qualified super: ``S.super.m()``, where ``S`` is an enclosing class or a
+; superinterface. The plain member pattern above also matches it, with ``S`` as
+; the receiver; the parser drops that copy.
+(method_invocation
+ object: (identifier) @call.scope
+ (super) @call.receiver
+ name: (identifier) @call.target
+ arguments: (argument_list) @call.arguments
+) @call.site
+
; Cast receiver: ((Foo) x).bar(args) names its type at the call site, like the
; fluent construction below. A scoped type (``(a.Foo) x``) is left uncaptured,
; and the anchors keep an intersection cast (``(Foo & Bar) x``) out.
diff --git a/tests/unit/ingestion/parser/test_java.py b/tests/unit/ingestion/parser/test_java.py
index 4833d6bc3..8b90b7725 100644
--- a/tests/unit/ingestion/parser/test_java.py
+++ b/tests/unit/ingestion/parser/test_java.py
@@ -169,3 +169,10 @@ class TestReceiverlessTwin:
self, parser: ASTParser
) -> None:
assert self._sites(parser, "set")[-2:] == [(15, None), (15, "data")]
+
+
+def test_a_qualified_super_call_keeps_its_qualifier(parser: ASTParser) -> None:
+ """``Sub.super.m()`` arrives as a ``super`` call scoped by ``Sub``, never as ``Sub.m()``."""
+ source = b"package b;\nclass Sub extends Base {\n class In { void q() { Sub.super.m(); } }\n}\n"
+ calls = parser.parse_file(_make_file_info("b/Sub.java", "java"), source).calls
+ assert [(c.receiver_name, c.scope_name, c.target_name) for c in calls] == [("super", "Sub", "m")]
diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.py
index f5da9a867..a2ac75278 100644
--- a/tests/unit/ingestion/test_call_resolver_strategies.py
+++ b/tests/unit/ingestion/test_call_resolver_strategies.py
@@ -1577,7 +1577,24 @@ class TestJavaReceiverRecall:
(c.line, c.receiver_name or "") for c in parsed["Use.java"].calls
) == [(3, ""), (4, "Node")]
- @pytest.mark.xfail(strict=True, reason="`S.super.m()` arrives as `S.m()`; the qualifier is lost")
+ def test_a_qualified_super_reaches_the_named_superinterface(self, tmp_path: Path) -> None:
+ """``I.super.m()`` is ``I``'s default ``m``, not the superclass's."""
+ files = {
+ "a/Base.java": "package a;\npublic class Base {\n public void m() {}\n}\n",
+ "a/I.java": "package a;\npublic interface I {\n default void m() {}\n}\n",
+ "a/Sub.java": "package a;\npublic class Sub extends Base implements I {\n"
+ " public void m() {\n I.super.m();\n }\n}\n",
+ }
+ parsed = _parse_all(tmp_path, {k: ("java", v) for k, v in files.items()})
+ parents = {"a/Sub.java::Sub": {"a/Base.java::Base", "a/I.java::I"}}
+ resolver = CallResolver(
+ parsed, {k: set() for k in parsed}, repo_path=str(tmp_path), heritage_parents=parents
+ )
+ calls = parsed["a/Sub.java"].calls
+ assert [(rc.callee_id, rc.origin) for rc in resolver.resolve_file("a/Sub.java", calls)] == [
+ ("a/I.java::I::m", "self_inherited")
+ ]
+
def test_a_qualified_super_reaches_the_qualifiers_superclass(self, tmp_path: Path) -> None:
members = " class In {\n void q() { Sub.super.m(); }\n }"
assert self._targets(tmp_path, "", members=members) == [