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): make a Java @interface and its elements symbols (#3670)

Swati Ahuja44128b865b66
diff --git a/packages/core/src/repowise/core/ingestion/extractors/signatures.py b/packages/core/src/repowise/core/ingestion/extractors/signatures.py
index 4cb3c0337..d59369f3e 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/signatures.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/signatures.py
@@ -51,6 +51,11 @@ def build_signature(node_type: str, name: str, params_text: str, def_node: Node,
return base
if node_type == "interface_declaration":
return f"interface {name}"
+ if node_type == "annotation_type_declaration":
+ return f"@interface {name}"
+ if node_type == "annotation_type_element_declaration":
+ # Java: an element takes no arguments and its declared type is what it returns.
+ return f"{name}(){_ret(('type',))}"
if node_type == "type_alias_declaration":
return f"type {name}"
if node_type == "enum_declaration":
diff --git a/packages/core/src/repowise/core/ingestion/language_configs.py b/packages/core/src/repowise/core/ingestion/language_configs.py
index cc22c18de..c5b17e704 100644
--- a/packages/core/src/repowise/core/ingestion/language_configs.py
+++ b/packages/core/src/repowise/core/ingestion/language_configs.py
@@ -217,6 +217,9 @@ LANGUAGE_CONFIGS: dict[str, LanguageConfig] = {
"interface_declaration": "interface",
"enum_declaration": "enum",
"record_declaration": "class", # Java 16+ records
+ # ``@interface Ann { String value(); }``: ``ann.value()`` calls an element.
+ "annotation_type_declaration": "interface",
+ "annotation_type_element_declaration": "method",
"method_declaration": "method",
"constructor_declaration": "function",
},
@@ -225,7 +228,13 @@ LANGUAGE_CONFIGS: dict[str, LanguageConfig] = {
visibility_fn=java_visibility,
parent_extraction="nesting",
parent_class_types=frozenset(
- {"class_declaration", "interface_declaration", "enum_declaration", "record_declaration"}
+ {
+ "class_declaration",
+ "interface_declaration",
+ "enum_declaration",
+ "record_declaration",
+ "annotation_type_declaration",
+ }
),
anonymous_class_types=frozenset({"object_creation_expression"}),
),
diff --git a/packages/core/src/repowise/core/ingestion/queries/java.scm b/packages/core/src/repowise/core/ingestion/queries/java.scm
index de3ca28cf..8ebc17f62 100644
--- a/packages/core/src/repowise/core/ingestion/queries/java.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/java.scm
@@ -31,6 +31,17 @@
name: (identifier) @symbol.name
) @symbol.def
+; An annotation type, and its elements as its methods: ``ann.value()`` calls one.
+(annotation_type_declaration
+ (modifiers)? @symbol.modifiers
+ name: (identifier) @symbol.name
+) @symbol.def
+
+(annotation_type_element_declaration
+ (modifiers)? @symbol.modifiers
+ name: (identifier) @symbol.name
+) @symbol.def
+
; One pattern, modifiers optional: two overlapping patterns kept whichever
; matched first, which dropped either the modifiers or the parameters that
; name an overload.
diff --git a/packages/core/src/repowise/core/upgrade/version.py b/packages/core/src/repowise/core/upgrade/version.py
index 2ecc57f0f..f68a346e0 100644
--- a/packages/core/src/repowise/core/upgrade/version.py
+++ b/packages/core/src/repowise/core/upgrade/version.py
@@ -77,7 +77,8 @@ STORE_FORMAT_VERSION: int = 2
#:
#: v8: Java, C# and C++ overloads of one arity carry their erased parameter
#: types (``put#1(String)``, ``put#1(int)``), and a method in a Java enum
-#: constant's body sits under the constant (``Op::PLUS::apply``); the first
+#: constant's body sits under the constant (``Op::PLUS::apply``), and a Java
+#: ``@interface`` and its elements are an ``interface`` and its methods; the first
#: update re-keys those symbols and graph nodes. A Swift ``extension`` is an
#: ``impl``, no longer a ``class``. Swift parses with tree-sitter-swift 0.7, so
#: a type body holding ``#if`` keeps its members.
diff --git a/tests/unit/ingestion/parser/test_java.py b/tests/unit/ingestion/parser/test_java.py
index 8b90b7725..bcb050944 100644
--- a/tests/unit/ingestion/parser/test_java.py
+++ b/tests/unit/ingestion/parser/test_java.py
@@ -83,6 +83,18 @@ class TestJavaParser:
module_paths = [i.module_path for i in result.imports]
assert any("ArrayList" in p for p in module_paths)
+ def test_annotation_type_elements_are_its_methods(self, parser: ASTParser) -> None:
+ source = (
+ b"package p;\npublic @interface Since {\n double value();\n"
+ b" String[] names() default {};\n}\n"
+ )
+ result = parser.parse_file(_make_file_info("p/Since.java", "java"), source)
+ assert sorted((s.id, s.kind, s.signature) for s in result.symbols) == [
+ ("p/Since.java::Since", "interface", "@interface Since"),
+ ("p/Since.java::Since::names", "method", "names() -> String[]"),
+ ("p/Since.java::Since::value", "method", "value() -> double"),
+ ]
+
def test_own_field_receiver_is_captured(self, parser: ASTParser) -> None:
"""``this.field.m()`` must carry ``field``, not arrive receiver-less.
diff --git a/tests/unit/ingestion/test_jvm_calls.py b/tests/unit/ingestion/test_jvm_calls.py
index ebc27403d..768635d4c 100644
--- a/tests/unit/ingestion/test_jvm_calls.py
+++ b/tests/unit/ingestion/test_jvm_calls.py
@@ -229,3 +229,18 @@ class TestKotlinImportBindings:
assert any(
u.endswith("App.kt::run") and v.endswith("Routing.kt::routing") for u, v, _ in edges
), edges
+
+
+class TestJavaAnnotationElements:
+ def test_an_element_call_binds_to_the_annotation_type(self, tmp_path: Path) -> None:
+ """``since.value()`` reads an element of the ``@interface``, a method of it."""
+ files = {
+ "p/Since.java": ("java", "package p;\npublic @interface Since {\n double value();\n}\n"),
+ "p/Use.java": (
+ "java",
+ "package p;\nclass Use {\n"
+ " boolean ok(Since since) {\n return since.value() > 1;\n }\n}\n",
+ ),
+ }
+ edges = _resolve_calls(_parse_all(tmp_path, files), tmp_path)
+ assert [(u, v) for u, v, _ in edges] == [("p/Use.java::Use::ok", "p/Since.java::Since::value")]