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): give a Java call on its own field a receiver (#1658)

Raghav Chamadiya0894867cbb23
diff --git a/packages/core/src/repowise/core/ingestion/queries/java.scm b/packages/core/src/repowise/core/ingestion/queries/java.scm
index 5c604fe7b..5e1cf79d0 100644
--- a/packages/core/src/repowise/core/ingestion/queries/java.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/java.scm
@@ -80,6 +80,29 @@
arguments: (argument_list) @call.arguments
) @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
+; receiver at all — which the resolver reads as an implicit receiver on the
+; caller's own class.
+;
+; ``this``/``super`` only, which is the rule every sibling grammar keeps: a
+; captured receiver must name something in the *caller's* scope, because the
+; receiver strategies type a field against the caller's class. Lifting the
+; nearest name out of ``a.b.method()`` would offer ``b`` — which belongs to
+; ``a``'s type — to be typed as a field of the caller, and bind a same-named
+; one. ``Outer.this.method()`` declines on its own, since a qualified ``this``
+; is a ``this`` node and not an ``identifier``, and that is right: it is an
+; implicit receiver.
+(method_invocation
+ object: (field_access
+ object: [(this) (super)]
+ field: (identifier) @call.receiver
+ )
+ name: (identifier) @call.target
+ arguments: (argument_list) @call.arguments
+) @call.site
+
; Chained method call: obj.method1().method2(args)
(method_invocation
object: (method_invocation)
diff --git a/tests/unit/ingestion/parser/test_java.py b/tests/unit/ingestion/parser/test_java.py
index c71372ab9..3ff35f59b 100644
--- a/tests/unit/ingestion/parser/test_java.py
+++ b/tests/unit/ingestion/parser/test_java.py
@@ -36,6 +36,31 @@ public class Calculator {
"""
+FIELD_RECEIVER_SOURCE = b"""package com.repowise.sample;
+
+public class Clinic {
+
+ private final OwnerRepository owners;
+ private final Basket basket;
+
+ public void report() {
+ this.owners.findAll();
+ super.audit.record();
+ this.basket.items.size();
+ }
+
+ class Inner {
+ void run() {
+ Clinic.this.reset();
+ }
+ }
+
+ void reset() {
+ }
+}
+"""
+
+
class TestJavaParser:
def test_finds_class(self, parser: ASTParser) -> None:
fi = _make_file_info("java_pkg/Calculator.java", "java")
@@ -57,3 +82,31 @@ class TestJavaParser:
assert len(result.imports) >= 2
module_paths = [i.module_path for i in result.imports]
assert any("ArrayList" in p for p in module_paths)
+
+ def test_own_field_receiver_is_captured(self, parser: ASTParser) -> None:
+ """``this.field.m()`` must carry ``field``, not arrive receiver-less.
+
+ The bare-call pattern matches every invocation, so a shape no
+ receiver-carrying pattern claims reads as an implicit receiver on the
+ caller's own class and binds by bare name.
+ """
+ fi = _make_file_info("java_pkg/Calculator.java", "java")
+ result = parser.parse_file(fi, FIELD_RECEIVER_SOURCE)
+ by_target = {c.target_name: c for c in result.calls if c.receiver_name}
+ assert by_target["findAll"].receiver_name == "owners"
+ assert by_target["record"].receiver_name == "audit"
+
+ def test_other_objects_field_is_not_a_receiver(self, parser: ASTParser) -> None:
+ """Only a name in the caller's own scope may be captured.
+
+ ``items`` in ``this.basket.items.size()`` is a field of ``Basket``, and
+ the receiver strategies type a field against the *caller's* class — so
+ capturing it would offer it to be bound to a same-named field of the
+ caller. ``Outer.this.m()`` is a real implicit receiver and keeps none.
+ """
+ fi = _make_file_info("java_pkg/Calculator.java", "java")
+ result = parser.parse_file(fi, FIELD_RECEIVER_SOURCE)
+ for target in ("size", "reset"):
+ assert all(
+ c.receiver_name is None for c in result.calls if c.target_name == target
+ ), target