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 #2795: Java record canonical constructor now takes the record's own access level (#2798)

Kratos0f788a75d63d
diff --git a/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/_helpers.py b/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/_helpers.py
index de343a925..eb8f194a6 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/_helpers.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/_helpers.py
@@ -27,11 +27,14 @@ def build_synthetic_symbol(
file_info: FileInfo,
parent_name: str | None,
decorators: list[str] | None = None,
+ visibility: str = "public",
) -> Symbol:
"""Construct a ``Symbol`` instance for a generator-synthesised name.
The synthesised symbol carries no docstring (the user never wrote it)
- and defaults to public visibility — generators emit public surfaces.
+ and defaults to public visibility — generators emit public surfaces
+ (callers whose language computes a declaration's real access can pass
+ ``visibility`` explicitly).
Symbol IDs follow the same ``<path>::[<parent>::]<name>`` shape the
real symbol pass uses, so the existing graph / dead-code passes
treat them identically.
@@ -56,7 +59,7 @@ def build_synthetic_symbol(
end_line=end_line,
docstring=None,
decorators=decorators or [],
- visibility="public",
+ visibility=visibility,
is_async=False,
language=file_info.language,
parent_name=parent_name,
diff --git a/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/java_records.py b/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/java_records.py
index 8a17543d2..6f90f51e7 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/java_records.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/java_records.py
@@ -17,6 +17,7 @@ from typing import TYPE_CHECKING
from ...models import FileInfo, Symbol
from ..helpers import node_text
+from ..visibility import refine_java_visibility
from ._helpers import build_synthetic_symbol
if TYPE_CHECKING:
@@ -72,14 +73,23 @@ def java_record_synthetic_symbols(
line = node.start_point[0] + 1
components = _record_components(node, src)
- # Canonical constructor (same name as the record).
+ # Canonical constructor (same name as the record). Per JLS 8.10.4
+ # it takes the record's own access level, for both the implicit and
+ # the explicit compact form — reuse the shared visibility helper.
+ ctor_visibility = refine_java_visibility(node)
+ ctor_keyword = {
+ "public": "public ",
+ "private": "private ",
+ "protected": "protected ",
+ }.get(ctor_visibility, "") # "internal" = package-private: no keyword
out.append(build_synthetic_symbol(
name=record_name, kind="function",
- signature=f"public {record_name}("
+ signature=f"{ctor_keyword}{record_name}("
+ ", ".join(f"{t} {n}" for n, t in components)
+ ")",
start_line=line, end_line=line,
file_info=file_info, parent_name=record_name,
+ visibility=ctor_visibility,
))
# Component accessors — same name as the component, zero args.
diff --git a/tests/unit/ingestion/test_java_extractors.py b/tests/unit/ingestion/test_java_extractors.py
index 7ec4402a8..86e0a7702 100644
--- a/tests/unit/ingestion/test_java_extractors.py
+++ b/tests/unit/ingestion/test_java_extractors.py
@@ -53,6 +53,44 @@ class TestJavaRecords:
names = {s.name for s in result.symbols}
assert "Point" in names
+ def test_implicit_canonical_ctor_takes_record_access(self, parser: ASTParser) -> None:
+ # JLS 8.10.4: an implicit canonical constructor takes the record's
+ # own access level — package-private record => package-private ctor.
+ src = b"package x;\nrecord Foo(int a, int b) {}\n"
+ result = parser.parse_file(_file(), src)
+ ctor = next(s for s in result.symbols if s.name == "Foo" and s.kind == "function")
+ assert ctor.visibility == "internal"
+ assert ctor.signature == "Foo(int a, int b)"
+
+ def test_compact_canonical_ctor_takes_record_access(self, parser: ASTParser) -> None:
+ # An explicit compact constructor with no access keyword is
+ # package-private too — no separate code path, one fix covers both.
+ src = (
+ b"package x;\nrecord Bar(int a, int b) {\n"
+ b" Bar {\n if (a < 0) throw new IllegalArgumentException();\n }\n}\n"
+ )
+ result = parser.parse_file(_file(), src)
+ ctor = next(s for s in result.symbols if s.name == "Bar" and s.kind == "function")
+ assert ctor.visibility == "internal"
+ assert ctor.signature == "Bar(int a, int b)"
+
+ def test_public_record_keeps_public_ctor(self, parser: ASTParser) -> None:
+ src = b"package x;\npublic record Baz(int a) {}\n"
+ result = parser.parse_file(_file(), src)
+ ctor = next(s for s in result.symbols if s.name == "Baz" and s.kind == "function")
+ assert ctor.visibility == "public"
+ assert ctor.signature == "public Baz(int a)"
+
+ def test_record_accessors_stay_public(self, parser: ASTParser) -> None:
+ # Accessors (and equals/hashCode/toString) are mandated public by
+ # the record contract regardless of the record's own access.
+ src = b"package x;\nrecord Foo(int a, int b) {}\n"
+ result = parser.parse_file(_file(), src)
+ accessors = {s.name: s for s in result.symbols if s.kind == "method"}
+ assert accessors["a"].visibility == "public"
+ assert accessors["a"].signature == "public int a()"
+ assert accessors["toString"].visibility == "public"
+
class TestJavaBindings:
def test_import_produces_binding(self, parser: ASTParser) -> None: