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): choose Java overloads by class literal, concatenation and lambda shape (#4821)

Swati Ahuja33090ff99773
diff --git a/packages/core/src/repowise/core/ingestion/call_overload_selection.py b/packages/core/src/repowise/core/ingestion/call_overload_selection.py
index 32dc17cc3..3deb75c3b 100644
--- a/packages/core/src/repowise/core/ingestion/call_overload_selection.py
+++ b/packages/core/src/repowise/core/ingestion/call_overload_selection.py
@@ -7,9 +7,10 @@ member by argument count, then by the typed arguments (Java's most-specific rule
from __future__ import annotations
from collections import defaultdict
-from collections.abc import Callable
+from collections.abc import Callable, Mapping
from dataclasses import replace
from functools import cache
+from types import MappingProxyType
from .call_receiver_typing import _FUNCTION_KINDS, _TYPE_KINDS
from .language_data import get_builtin_types
@@ -97,14 +98,25 @@ _JAVA_LITERAL_LEVELS: dict[str, dict[str, int]] = {
_UNJUDGED_LEVEL = {**dict.fromkeys(_JAVA_WIDENING, 2), "String": 1, "null": 1}
-def _argument_fit(parameter: str, argument: str) -> int | None:
+_NO_REPO_TYPES: Mapping[str, bool] = MappingProxyType({})
+
+
+def _argument_fit(
+ parameter: str, argument: str, repo_types: Mapping[str, bool] = _NO_REPO_TYPES
+) -> int | None:
"""How near a Java argument reaches *parameter*: 0 exactly, 1 widened, 2 boxed; None never.
*argument* is a literal's type or a named argument's declared one; a
named ``String`` or primitive takes the literal path. A coarse form of the
most-specific rule: a call ``f(true)`` takes ``f(boolean)`` over
- ``f(Object)``, and ``f('c')`` takes ``f(int)`` over it.
+ ``f(Object)``, and ``f('c')`` takes ``f(int)`` over it. *repo_types*
+ says which repository type names are interfaces, for a lambda.
"""
+ if argument.startswith("->"):
+ return _lambda_fit(parameter, argument, repo_types)
+ if argument == "Class" and parameter.rstrip("?") == "Class":
+ # ``Class<?>`` erases to ``Class?``; a class literal fits it exactly.
+ return 0
levels = _JAVA_LITERAL_LEVELS.get(argument)
if levels is None:
return _reference_fit(parameter, argument)
@@ -115,6 +127,57 @@ def _argument_fit(parameter: str, argument: str) -> int | None:
return levels.get(parameter)
+# JDK functional interfaces: (parameter count, whether the function returns a value).
+_JAVA_FUNCTIONAL_SHAPES = {
+ "Runnable": (0, False), "Callable": (0, True), "Supplier": (0, True),
+ "Consumer": (1, False), "Function": (1, True), "Predicate": (1, True),
+ "UnaryOperator": (1, True), "BiConsumer": (2, False), "BiFunction": (2, True),
+ "BiPredicate": (2, True), "BinaryOperator": (2, True), "Comparator": (2, True),
+}
+
+
+def _lambda_fit(parameter: str, argument: str, repo_types: Mapping[str, bool]) -> int | None:
+ """How a lambda of shape ``->N:value|void|either`` reaches *parameter*.
+
+ 0 for a JDK functional interface of that shape, None for one of another
+ shape or a type no lambda targets, 1 for any other interface, which may
+ be a repository functional interface of the right shape. A body that
+ suits both reaches a void interface at 1, so the value-returning one
+ wins, as the most-specific rule has it.
+ """
+ count, _, result = argument[2:].partition(":")
+ shape = _JAVA_FUNCTIONAL_SHAPES.get(parameter)
+ if shape is not None:
+ takes, returns = shape
+ if takes != int(count):
+ return None
+ if result == "either":
+ return 0 if returns else 1
+ return 0 if returns == (result == "value") else None
+ return 1 if _may_take_lambda(parameter, repo_types) else None
+
+
+def _may_take_lambda(parameter: str, repo_types: Mapping[str, bool]) -> bool:
+ """Whether *parameter* may be a functional interface: a lambda converts to no class.
+
+ A repository name answers by its kind (*repo_types*); any other name
+ may be a library interface unless it is a known JDK class.
+ """
+ if parameter in repo_types:
+ return repo_types[parameter]
+ return parameter not in _NO_LAMBDA_TARGETS and not parameter.endswith("[]")
+
+
+# Parameter types no lambda converts to: primitives and the JDK classes a
+# parameter commonly names.
+_NO_LAMBDA_TARGETS = _JAVA_PRIMITIVES | _JAVA_FINAL_PARAMETERS | frozenset(
+ {"Object", "", "Class", "Enum", "Record", "Number", "Void", "Thread", "Throwable",
+ "Exception", "RuntimeException", "Error", "StringBuilder", "StringBuffer", "Optional",
+ "ArrayList", "LinkedList", "HashMap", "LinkedHashMap", "TreeMap", "HashSet",
+ "LinkedHashSet", "TreeSet", "ArrayDeque", "CompletableFuture"}
+)
+
+
def _reference_fit(parameter: str, argument: str) -> int | None:
"""How a value of class or array type *argument* reaches *parameter*.
@@ -157,7 +220,10 @@ def _array_fit(parameter: str, argument: str) -> int | None:
def _closest_fit(
- members: list[tuple[str, list[str]]], argument_types: tuple[str | None, ...], expanded: bool
+ members: list[tuple[str, list[str]]],
+ argument_types: tuple[str | None, ...],
+ expanded: bool,
+ repo_types: Mapping[str, bool] = _NO_REPO_TYPES,
) -> str | None:
"""The one member the typed arguments fit most closely, or None on a tie.
@@ -170,7 +236,9 @@ def _closest_fit(
ranked: dict[int, list[str]] = defaultdict(list)
for member, parameters in members:
fits = [
- _argument_fit(_parameter_taking(parameters, argument_types, index, expanded), argument)
+ _argument_fit(
+ _parameter_taking(parameters, argument_types, index, expanded), argument, repo_types
+ )
for index, argument in enumerate(argument_types)
if argument is not None
]
@@ -636,7 +704,7 @@ class OverloadSelectionMixin:
if self._symbols_by_id[found[0]].language == "apex":
closest = _exact_apex_fit(typed, argument_types, self._folded_type_names())
else:
- closest = _closest_fit(typed, argument_types, expanded)
+ closest = _closest_fit(typed, argument_types, expanded, self._jvm_lambda_type_map())
return found if closest is None else [closest]
def _csharp_takers(self, found: list[str], argument_types: tuple[str | None, ...]) -> list[str]:
@@ -740,6 +808,16 @@ class OverloadSelectionMixin:
paths = {self._symbol_paths_by_id[c] for c in classes}
return "converts" if any("operator" in self._text_of(p) for p in paths) else "value"
+ def _jvm_lambda_type_map(self) -> dict[str, bool]:
+ """{repository JVM type name: whether a type of that name is an interface}, built once."""
+ if self._jvm_lambda_types is None:
+ kinds: dict[str, bool] = {}
+ for symbol in self._symbols_by_id.values():
+ if symbol.language in ("java", "kotlin") and symbol.kind in _TYPE_KINDS:
+ kinds[symbol.name] = kinds.get(symbol.name, False) or symbol.kind == "interface"
+ self._jvm_lambda_types = kinds
+ return self._jvm_lambda_types
+
def _folded_type_names(self) -> frozenset[str]:
"""The repository's Apex type names, lowercased, built once."""
if self._folded_types is None:
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index 583beb87f..b4448491e 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -395,6 +395,7 @@ class CallResolver(
# {C# type name: the type symbols declaring it}, then each name's shape.
self._csharp_type_shapes: dict[str, set[str]] | None = None
self._csharp_shape_answers: dict[str, str | None] = {}
+ self._jvm_lambda_types: dict[str, bool] | None = None
# Narrowed to C# for the extension index: the set above is a bare
# cross-language name match, so a type of that name in any language
# would admit an extension on the BCL type it shadows.
diff --git a/packages/core/src/repowise/core/ingestion/models.py b/packages/core/src/repowise/core/ingestion/models.py
index bef6f1a03..9419fe63d 100644
--- a/packages/core/src/repowise/core/ingestion/models.py
+++ b/packages/core/src/repowise/core/ingestion/models.py
@@ -381,8 +381,9 @@ class CallSite:
# COBOL ``CALL`` / ``LINK`` / ``XCTL``: names a PROGRAM-ID, never a paragraph.
program_call: bool = False
# Java: each argument's type where it is a literal (``"String"``, ``"int"``,
- # ``"null"``), else None; None overall when no argument is one. C# writes a
- # lambda's or a construction's shape here (``"=>2"``, ``"new Foo"``).
+ # ``"null"``), a class literal (``"Class"``), a String concatenation or a
+ # lambda (``"->0:value"``), else None; None overall when no argument is one.
+ # C# writes a lambda's or a construction's shape here (``"=>2"``, ``"new Foo"``).
argument_types: tuple[str | None, ...] | None = None
# Java and C#: each argument that is a bare name (``start``), else None; None overall
# when no argument is one. The resolver reads its declared type.
diff --git a/packages/core/src/repowise/core/ingestion/parser_helpers.py b/packages/core/src/repowise/core/ingestion/parser_helpers.py
index 77643eda1..51710016d 100644
--- a/packages/core/src/repowise/core/ingestion/parser_helpers.py
+++ b/packages/core/src/repowise/core/ingestion/parser_helpers.py
@@ -537,9 +537,88 @@ def _java_literal_type(node: Node, src: str) -> str | None:
return "long" if node_text(node, src).endswith(("l", "L")) else "int"
if node.type in _JAVA_FLOATING_LITERALS:
return "float" if node_text(node, src).endswith(("f", "F")) else "double"
+ return _java_expression_type(node, src)
+
+
+def _java_expression_type(node: Node, src: str) -> str | None:
+ """A class literal, a String concatenation or a lambda's shape; else a plain literal."""
+ if node.type == "class_literal":
+ return "Class"
+ if node.type == "binary_expression":
+ return _java_concatenation_type(node, src)
+ if node.type == "lambda_expression":
+ return _java_lambda_shape(node)
return _JAVA_LITERAL_TYPES.get(node.type)
+def _java_concatenation_type(node: Node, src: str) -> str | None:
+ """``"id " + id`` is a String: a ``+`` with a String literal operand."""
+ operator = node.child_by_field_name("operator")
+ if operator is None or node_text(operator, src) != "+":
+ return None
+ operands = (node.child_by_field_name("left"), node.child_by_field_name("right"))
+ if any(o is not None and _java_literal_type(o, src) == "String" for o in operands):
+ return "String"
+ return None
+
+
+# Expression bodies that suit a void and a value-returning interface alike.
+_JAVA_EITHER_BODIES = frozenset(
+ {"assignment_expression", "update_expression", "object_creation_expression"}
+)
+
+
+def _java_lambda_shape(node: Node) -> str | None:
+ """A lambda as ``->N:value``, ``->N:void`` or ``->N:either``, N its parameter count.
+
+ Read by overload choice against functional interfaces (``Runnable`` vs
+ ``Supplier``). A body that is a bare method call may or may not yield a
+ value, and a block ending in a throw fits both, so neither is judged.
+ """
+ body = node.child_by_field_name("body")
+ if body is None or body.type == "method_invocation":
+ return None
+ count = _lambda_parameter_count(node)
+ if body.type in _JAVA_EITHER_BODIES:
+ return f"->{count}:either"
+ if body.type != "block":
+ return f"->{count}:value"
+ returns = _block_returns(body)
+ if not returns and _ends_in_throw(body):
+ return None
+ return f"->{count}:{'value' if any(returns) else 'void'}"
+
+
+def _lambda_parameter_count(node: Node) -> int:
+ params = node.child_by_field_name("parameters")
+ if params is None:
+ return 0
+ if params.type == "identifier":
+ return 1
+ return sum(1 for c in params.named_children if "comment" not in c.type)
+
+
+def _ends_in_throw(block: Node) -> bool:
+ statements = [c for c in block.named_children if "comment" not in c.type]
+ return bool(statements) and statements[-1].type == "throw_statement"
+
+
+def _block_returns(block: Node) -> list[bool]:
+ """Whether each ``return`` in a lambda's own block carries a value.
+
+ A nested lambda's or class body's returns belong to it, not this lambda.
+ """
+ found: list[bool] = []
+ stack = list(block.named_children)
+ while stack:
+ node = stack.pop()
+ if node.type == "return_statement":
+ found.append(node.named_child_count > 0)
+ elif node.type not in ("lambda_expression", "class_body"):
+ stack.extend(node.named_children)
+ return found
+
+
def _apex_literal_type(node: Node, src: str) -> str | None:
if node.type == "int":
return "Long" if node_text(node, src).endswith(("l", "L")) else "Integer"
diff --git a/tests/unit/ingestion/test_overload_identity.py b/tests/unit/ingestion/test_overload_identity.py
index 8ef2afb6f..671924838 100644
--- a/tests/unit/ingestion/test_overload_identity.py
+++ b/tests/unit/ingestion/test_overload_identity.py
@@ -1310,6 +1310,100 @@ class TestJavaLiteralArguments:
(call,) = _parse("A.java", "java", text).calls
assert call.argument_types == ("int", "int", "int", "long", "boolean", "float", None, None)
+ def test_class_literals_concatenations_and_lambda_shapes_are_read(self) -> None:
+ text = (
+ "class A {\n void go(int x) {\n"
+ ' f(A.class, "id " + x, x + 1, () -> { run(); }, () -> { return 1; },\n'
+ " () -> new A(), (a, b) -> a, () -> run(), () -> { throw new E(); });\n"
+ " }\n}\n"
+ )
+ call = next(c for c in _parse("A.java", "java", text).calls if c.target_name == "f")
+ assert call.argument_types == (
+ "Class", "String", None, "->0:void", "->0:value", "->0:either", "->2:value", None, None
+ )
+
+ def test_a_nested_return_or_a_guarded_throw_leaves_a_block_void(self) -> None:
+ """A nested lambda's ``return`` is its own; a throw under an ``if`` can complete normally."""
+ text = (
+ "class A {\n void go(boolean ready) {\n"
+ " f(() -> { Runnable r = () -> { return 1; }; },\n"
+ " () -> { if (ready) throw new E(); });\n"
+ " }\n}\n"
+ )
+ call = next(c for c in _parse("A.java", "java", text).calls if c.target_name == "f")
+ assert call.argument_types == ("->0:void", "->0:void")
+
+ _ACCESS = (
+ "package app;\nimport java.util.function.Supplier;\npublic class Access {\n"
+ " public static void as(String t, Runnable r) { r.run(); }\n"
+ " public static <T> T as(String t, Supplier<T> s) { return s.get(); }\n"
+ " void use(String t) {\n as(t, BODY);\n }\n}\n"
+ )
+
+ @pytest.mark.parametrize(
+ ("body", "member"),
+ [
+ ("() -> { use(t); }", "as#2(String,Runnable)"),
+ ("() -> { return 1; }", "as#2(String,Supplier)"),
+ ("() -> new Access()", "as#2(String,Supplier)"),
+ ("() -> { if (t == null) throw new IllegalStateException(); }", "as#2(String,Runnable)"),
+ ("() -> { Supplier<Integer> s = () -> { return 1; }; }", "as#2(String,Runnable)"),
+ ],
+ )
+ def test_a_lambda_takes_the_functional_interface_of_its_shape(
+ self, tmp_path: Path, body: str, member: str
+ ) -> None:
+ text = self._ACCESS.replace("BODY", body)
+ edges = _edges(tmp_path, {"app/Access.java": ("java", text)})
+ assert ("app/Access.java::Access::use", f"app/Access.java::Access::{member}") in edges
+
+ @pytest.mark.parametrize(
+ ("first", "member"),
+ [
+ # A lambda converts to no class: not a JDK one, not a repository one.
+ ("Throwable", "f#2(Handler,int)"),
+ ("Task", "f#2(Handler,int)"),
+ # A library type may be a functional interface, so it ties and the
+ # set keeps its first member.
+ ("Listener", "f#2(Listener,int)"),
+ ],
+ )
+ def test_a_lambda_takes_only_a_parameter_that_may_be_an_interface(
+ self, tmp_path: Path, first: str, member: str
+ ) -> None:
+ text = (
+ "package app;\ninterface Handler { void handle(); }\nclass Task { }\n"
+ "public class S {\n"
+ f" void f({first} x, int i) {{ }}\n"
+ " void f(Handler h, int i) { }\n"
+ " void use() {\n f(() -> { }, 1);\n }\n}\n"
+ )
+ edges = _edges(tmp_path, {"app/S.java": ("java", text)})
+ assert edges == {("app/S.java::S::use", f"app/S.java::S::{member}")}
+
+ def test_a_class_literal_takes_the_class_parameter(self, tmp_path: Path) -> None:
+ text = (
+ "package app;\npublic class NotFound extends RuntimeException {\n"
+ " public NotFound(String message, Throwable cause) { }\n"
+ " public NotFound(Class<?> type, Object id) { }\n"
+ " static NotFound of(Long id) {\n return new NotFound(NotFound.class, id);\n }\n}\n"
+ )
+ edges = _edges(tmp_path, {"app/NotFound.java": ("java", text)})
+ assert (
+ "app/NotFound.java::NotFound::of", "app/NotFound.java::NotFound::NotFound#2(Class?,Object)"
+ ) in edges
+
+ def test_a_body_that_suits_both_still_takes_a_void_interface(self, tmp_path: Path) -> None:
+ """``run(() -> n++, 1)``: the lambda suits Runnable, so the literal decides."""
+ text = (
+ "package app;\npublic class R {\n int n;\n"
+ " void run(Runnable r, String s) { }\n"
+ " void run(Runnable r, int i) { }\n"
+ " void use() {\n run(() -> n++, 1);\n }\n}\n"
+ )
+ edges = _edges(tmp_path, {"app/R.java": ("java", text)})
+ assert edges == {("app/R.java::R::use", "app/R.java::R::run#2(Runnable,int)")}
+
def test_a_tie_that_differs_on_a_non_literal_argument_is_not_broken(
self, tmp_path: Path
) -> None: