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 a Java Outer.Inner.m() receiver by its whole type path (#4835)

Swati Ahujac4fb230cba81
diff --git a/packages/core/src/repowise/core/ingestion/call_language_strategies.py b/packages/core/src/repowise/core/ingestion/call_language_strategies.py
index 3bec2bb51..ff84a75c0 100644
--- a/packages/core/src/repowise/core/ingestion/call_language_strategies.py
+++ b/packages/core/src/repowise/core/ingestion/call_language_strategies.py
@@ -140,6 +140,7 @@ _LANGUAGE_CALL_STRATEGIES: dict[str, _LanguageCallStrategies] = {
"java": replace(
_JVM_STRATEGIES,
free=("_resolve_java_same_package_unique",),
+ member=("_resolve_java_member_type_call", "_resolve_jvm_receiver_same_package"),
member_fallback=_TYPED_RECEIVER,
member_bare_fallback=False,
inherited_typed_receiver=True,
@@ -691,6 +692,34 @@ class LanguageStrategiesMixin:
return None
return ResolvedCall(caller_id, sym_id, 0.90, call.line, "receiver_same_package")
+ def _resolve_java_member_type_call(
+ self,
+ file_path: str,
+ call: CallSite,
+ caller_id: str,
+ ) -> ResolvedCall | None:
+ """``Outer.Inner.method()``: the method of the type the whole path names.
+
+ The path is read as Java reads it from the caller, so the ``Inner`` of
+ another imported type that also nests one is never taken, and a type
+ that lacks the method answers nothing. A path naming no repository
+ type (an enum constant, an external type) is resolved as its
+ receiver-less copy was before the grammar captured the path.
+ """
+ receiver = call.receiver_name or ""
+ if "." not in receiver:
+ return None
+ type_id = self._java_written_type(file_path, caller_id, receiver)
+ if type_id is None:
+ return self._resolve_one(file_path, replace(call, receiver_name=None))
+ found = self._java_member_target(file_path, call, caller_id, type_id)
+ if found is None:
+ return None
+ sym_id, scope = found
+ # ``receiver_same_file`` / ``_same_package`` / ``_import``: the type is written here.
+ confidence = {"same_file": 0.93, "same_package": 0.90}.get(scope, 0.88)
+ return ResolvedCall(caller_id, sym_id, confidence, call.line, f"receiver_{scope}")
+
def _resolve_go_package_call(
self,
file_path: str,
diff --git a/packages/core/src/repowise/core/ingestion/parser/calls.py b/packages/core/src/repowise/core/ingestion/parser/calls.py
index 0e51b4c4a..047ffdaa9 100644
--- a/packages/core/src/repowise/core/ingestion/parser/calls.py
+++ b/packages/core/src/repowise/core/ingestion/parser/calls.py
@@ -24,6 +24,10 @@ from ..python_tasks import is_fire_and_forget
_node_text = node_text
+# ``Outer.Inner``: capitalised names, each after the first holding a lower-case
+# letter, so ``X.CONST`` is not one. Same shape as the java.scm member-type pattern.
+_JAVA_MEMBER_TYPE_PATH = re.compile(r"[A-Z][A-Za-z0-9_]*(?:\.[A-Z][A-Za-z0-9_]*[a-z][A-Za-z0-9_]*)+")
+
def _call_receiver_from_node(node: Node, src: str) -> CallReceiver | None:
"""Describe an inner call captured as another call's receiver.
@@ -65,6 +69,17 @@ def _call_receiver_from_node(node: Node, src: str) -> CallReceiver | None:
return CallReceiver(target_name, receiver_name, argument_count)
+def _java_member_type_receiver(node: Node, inner: CallReceiver, src: str) -> CallReceiver:
+ """*inner* with its ``Outer.Inner`` receiver, as the chain head ``Outer.Inner.m()`` writes it."""
+ obj = node.child_by_field_name("object")
+ if obj is None or obj.type != "field_access":
+ return inner
+ path = "".join(_node_text(obj, src).split())
+ if not _JAVA_MEMBER_TYPE_PATH.fullmatch(path):
+ return inner
+ return replace(inner, receiver_name=path)
+
+
def _kotlin_call_receiver(call: Node, src: str) -> CallReceiver | None:
"""Describe a Kotlin call captured as another call's receiver."""
named = call.named_children
@@ -199,7 +214,12 @@ def _dart_receiver_path(head: Node, target: Node, src: str) -> str | None:
# queries widen only these two), and what a plain dotted path of names looks
# like once whitespace is gone.
_PATH_RECEIVERS = frozenset(
- {("typescript", "member_expression"), ("python", "attribute"), ("apex", "field_access")}
+ {
+ ("typescript", "member_expression"),
+ ("python", "attribute"),
+ ("apex", "field_access"),
+ ("java", "field_access"),
+ }
)
_DOTTED_PATH = re.compile(r"[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+")
@@ -382,6 +402,8 @@ def _call_from_match(
if receiver_call_nodes
else None
)
+ if language == "java" and receiver_call is not None and receiver_call.receiver_name is None:
+ receiver_call = _java_member_type_receiver(receiver_call_nodes[0], receiver_call, src)
if receiver_call_nodes and receiver_call is None and language == "kotlin":
# Avoid resolving an unsupported chained call by its bare name.
return None
diff --git a/packages/core/src/repowise/core/ingestion/queries/java.scm b/packages/core/src/repowise/core/ingestion/queries/java.scm
index 8ebc17f62..8fe2d908e 100644
--- a/packages/core/src/repowise/core/ingestion/queries/java.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/java.scm
@@ -81,6 +81,17 @@
arguments: (argument_list) @call.arguments
) @call.site
+; Static call on a member type: Outer.Inner.method(args). Only a path of
+; capitalised names whose later segments hold a lower-case letter, so a
+; constant (``X.CONST.m()``) or a field path (``a.b.m()``) stays bare. The
+; resolver reads the whole path as a type, never its last segment alone.
+(method_invocation
+ object: (field_access) @call.receiver
+ name: (identifier) @call.target
+ arguments: (argument_list) @call.arguments
+ (#match? @call.receiver "^[A-Z][A-Za-z0-9_]*(\\s*[.]\\s*[A-Z][A-Za-z0-9_]*[a-z][A-Za-z0-9_]*)+$")
+) @call.site
+
; Method call on the caller's own field: this.field.method(args).
; The pattern above constrains ``object`` to a bare identifier and the bare
; pattern constrains nothing, so without this a field receiver arrives with no
diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.py
index d3d146fc6..d4e74261a 100644
--- a/tests/unit/ingestion/test_call_resolver_strategies.py
+++ b/tests/unit/ingestion/test_call_resolver_strategies.py
@@ -1771,6 +1771,80 @@ class TestJavaMemberTypes:
assert callees == ["c/Inner.java::Inner::y", "c/Inner.java::Inner::z"]
+class TestJavaQualifiedTypeReceiver:
+ """``B.Create.make()`` is ``B``'s nested ``Create``, though ``A`` nests one too."""
+
+ @staticmethod
+ def _outer(name: str, extra: str = "") -> str:
+ return (
+ f"package p;\npublic interface {name} {{\n class Create {{\n"
+ " public static Create make() { return null; }\n"
+ f" public void run() {{}}\n{extra} }}\n}}\n"
+ )
+
+ def _targets(self, tmp_path: Path, body: str, imports: str) -> list[tuple[str, str]]:
+ files = {
+ "p/A.java": self._outer("A", " public void onlyInA() {}\n"),
+ "p/B.java": self._outer("B"),
+ "u/Use.java": f"package u;\n{imports}class Use {{\n void go() {{\n"
+ f" {body}\n }}\n}}\n",
+ }
+ parsed = _parse_all(tmp_path, {k: ("java", v) for k, v in files.items()})
+ _link_imports(parsed, {"u/Use.java": {"p.A": "p/A.java", "p.B": "p/B.java"}})
+ imported = {"u/Use.java": {"p/A.java", "p/B.java"}, "p/A.java": set(), "p/B.java": set()}
+ resolver = CallResolver(parsed, imported, repo_path=str(tmp_path))
+ calls = parsed["u/Use.java"].calls
+ return sorted((rc.callee_id, rc.origin) for rc in resolver.resolve_file("u/Use.java", calls))
+
+ @pytest.mark.parametrize(
+ "imports", ["import p.A;\nimport p.B;\n", "import p.B;\nimport p.A;\n"], ids=["ab", "ba"]
+ )
+ def test_the_whole_path_names_the_type(self, tmp_path: Path, imports: str) -> None:
+ assert self._targets(tmp_path, "B.Create.make();", imports) == [
+ ("p/B.java::Create::make", "receiver_import")
+ ]
+
+ def test_a_chain_types_its_head_by_the_whole_path(self, tmp_path: Path) -> None:
+ imports = "import p.A;\nimport p.B;\n"
+ assert self._targets(tmp_path, "B.Create.make().run();", imports) == [
+ ("p/B.java::Create::make", "receiver_import"),
+ ("p/B.java::Create::run", "return_type_import"),
+ ]
+
+ def test_a_type_that_lacks_the_method_answers_nothing(self, tmp_path: Path) -> None:
+ """Only ``A.Create`` declares ``onlyInA``; the last segment alone would bind it."""
+ assert self._targets(tmp_path, "B.Create.onlyInA();", "import p.A;\nimport p.B;\n") == []
+
+ @pytest.mark.parametrize("receiver", ["X.CONST", "a.b", "X.inner"])
+ def test_a_path_that_is_not_a_member_type_stays_bare(
+ self, tmp_path: Path, receiver: str
+ ) -> None:
+ source = f"class Use {{\n void go() {{\n {receiver}.m();\n }}\n}}\n"
+ parsed = _parse_all(tmp_path, {"Use.java": ("java", source)})
+ assert [c.receiver_name for c in parsed["Use.java"].calls] == [None]
+
+ def test_a_path_written_across_lines_is_read_whole(self, tmp_path: Path) -> None:
+ body = "B\n .Create\n .make()\n .run();"
+ assert self._targets(tmp_path, body, "import p.A;\nimport p.B;\n") == [
+ ("p/B.java::Create::make", "receiver_import"),
+ ("p/B.java::Create::run", "return_type_import"),
+ ]
+
+ def test_a_path_naming_no_type_keeps_its_bare_reading(self, tmp_path: Path) -> None:
+ """``Color.Red`` is an enum constant: ``m`` binds as it did before the path was read."""
+ files = {
+ "p/Color.java": "package p;\npublic enum Color {\n Red;\n public void m() {}\n}\n",
+ "p/Use.java": "package p;\nclass Use {\n void go() {\n Color.Red.m();\n }\n}\n",
+ }
+ parsed = _parse_all(tmp_path, {k: ("java", v) for k, v in files.items()})
+ resolver = CallResolver(parsed, {k: set() for k in parsed}, repo_path=str(tmp_path))
+ calls = parsed["p/Use.java"].calls
+ assert [c.receiver_name for c in calls] == ["Color.Red"]
+ assert [rc.callee_id for rc in resolver.resolve_file("p/Use.java", calls)] == [
+ "p/Color.java::Color::m"
+ ]
+
+
class TestOwnerAwareModuleTier:
"""The module and import-name tiers pick an owner, not just a file.