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(java): count a static member import as a use of its declaring type (#2977)

Raghav Chamadiyaed705a44e04b
diff --git a/packages/core/src/repowise/core/ingestion/extractors/bindings/java.py b/packages/core/src/repowise/core/ingestion/extractors/bindings/java.py
index 6ee5e3de5..f8aeeef52 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/bindings/java.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/bindings/java.py
@@ -9,14 +9,24 @@ from ..helpers import node_text
def extract_java_bindings(stmt_node: Node, src: str) -> tuple[list[str], list[NamedBinding]]:
- """Extract bindings from Java import declarations."""
+ """Extract bindings from Java import declarations.
+
+ ``import static com.foo.Bar.member`` binds ``member`` and uses its
+ declaring type, so ``Bar`` joins the imported names. It gets no binding:
+ a static import does not bring the type's bare name into scope.
+ """
+ kinds = {child.type for child in stmt_node.children}
+ # ``import static a.b.C.*`` parses ``C`` as the last name, already the type.
+ names_member = "static" in kinds and "asterisk" not in kinds
for child in stmt_node.children:
if child.type == "scoped_identifier":
- full = node_text(child, src)
- local = full.rsplit(".", 1)[-1]
+ owner, _, local = node_text(child, src).rpartition(".")
if local == "*":
return ["*"], [NamedBinding(local_name="*", exported_name=None, source_file=None)]
- return [local], [NamedBinding(local_name=local, exported_name=local, source_file=None)]
+ names = [local]
+ if names_member and owner:
+ names.append(owner.rpartition(".")[2])
+ return names, [NamedBinding(local_name=local, exported_name=local, source_file=None)]
if child.type == "asterisk":
return ["*"], [NamedBinding(local_name="*", exported_name=None, source_file=None)]
return [], []
diff --git a/tests/unit/ingestion/test_java_extractors.py b/tests/unit/ingestion/test_java_extractors.py
index 86e0a7702..30c2281f9 100644
--- a/tests/unit/ingestion/test_java_extractors.py
+++ b/tests/unit/ingestion/test_java_extractors.py
@@ -99,6 +99,24 @@ class TestJavaBindings:
modules = [imp.module_path for imp in result.imports]
assert "com.example.Foo" in modules
+ def test_static_member_import_names_its_declaring_type(self, parser: ASTParser) -> None:
+ src = b"package x;\nimport static com.example.Outer.Bar.baz;\npublic class App {}\n"
+ (imp,) = parser.parse_file(_file(), src).imports
+ assert imp.imported_names == ["baz", "Bar"]
+ # Only the member is bound: the type's bare name is not in scope.
+ assert [b.local_name for b in imp.bindings] == ["baz"]
+
+ def test_static_wildcard_import_names_only_the_type(self, parser: ASTParser) -> None:
+ # The member is "*", so the last name is already the type.
+ src = b"package x;\nimport static com.example.Bar.*;\npublic class App {}\n"
+ (imp,) = parser.parse_file(_file(), src).imports
+ assert imp.imported_names == ["Bar"]
+
+ def test_plain_import_names_only_the_type(self, parser: ASTParser) -> None:
+ src = b"package x;\nimport com.example.Foo;\npublic class App {}\n"
+ (imp,) = parser.parse_file(_file(), src).imports
+ assert imp.imported_names == ["Foo"]
+
class TestJavaModuleDocstring:
def test_module_javadoc_extracted(self, parser: ASTParser) -> None: