Repository navigation
Description
Version: graphifyy 0.9.68 (also 0.9.15)
Problem
The tree-sitter engine harvests declaration annotations only for Java/Groovy
(_java_annotation_names, gated on those languages). Kotlin nests annotations
differently — modifiers -> annotation -> user_type for @Entity, or
annotation -> constructor_invocation -> user_type for @OneToMany(...) — so
nothing is emitted for Kotlin. In addition:
- primary-constructor
val/varparameters (the idiomatic place for Kotlin
entity fields) get no node, type edge, or annotation edge at all; - class-body
property_declarations attach their type edge to the class, with
no per-property node, so field-level annotations have nowhere to attach.
For Kotlin JPA/Spring code this drops essentially the whole domain model.
Repro
import jakarta.persistence.*
@Entity
@Table(name = "orders")
class Order(
@Id @GeneratedValue val id: Long? = null,
@ManyToOne(fetch = FetchType.LAZY) val customer: Customer,
@Column(nullable = false) var status: String
) {
@OneToMany(mappedBy = "order")
var lines: MutableList<OrderLine> = mutableListOf()
@get:Transient
val total: Int get() = 0
}
@Entity class Customer(@Id val id: Long)
@Entity class OrderLine(@Id val id: Long, @ManyToOne val order: Order)graphify extract . --code-only
Actual (0.9.68): 4 nodes / 4 edges — three contains edges and
Order -references/generic_arg-> OrderLine.
Expected: class annotation edges (Order -> Entity, Order -> Table),
per-field nodes (Order -defines/field-> .customer, …), field type edges
(.customer -> Customer, .order -> Order), and field annotation edges
(.customer -> ManyToOne, .status -> Column, .id -> Id,
.lines -> OneToMany, .total -> Transient). With the patch below: 20 nodes /
27 edges, 13 of them attribute edges.
Proposed fix
We've been running the attached patch in production since 0.9.15, and it's now
rebased onto 0.9.68. It adds _kotlin_annotation_names, _kotlin_member_name
and _kotlin_param_type_node helpers, and emits:
- class-level annotation edges (
references,context="attribute"); - a
.namenode per primary-constructorval/var, with type + annotation edges; - a
.namenode per class-body property (its type edges move from the class to
the property; initializer call collection is unchanged); - function/getter annotation edges for Kotlin.
Happy to open a PR with tests if this direction is welcome. One open question:
Java annotations are now import-qualified (jakarta.persistence.Entity), while
this patch still emits the simple name for Kotlin. Aligning the two would be a
natural follow-up.
Patch against 0.9.68 (graphify/extractors/engine.py)
--- a/graphify/extractors/engine.py
+++ b/graphify/extractors/engine.py
@@ -791,6 +791,70 @@
return c
return None
+def _kotlin_annotation_names(declaration_node, source: bytes) -> list[str]:
+ """Collect annotation names from a Kotlin declaration's `modifiers` child.
+ Kotlin nests the annotated type under `annotation -> user_type` (no args,
+ e.g. `@Entity`) or `annotation -> constructor_invocation -> user_type`
+ (with args, e.g. `@OneToMany(...)`), unlike Java's flat
+ `marker_annotation`/`annotation` with a direct name field. So
+ `_java_annotation_names` finds nothing on a Kotlin node, and its call sites
+ are gated to java/groovy anyway. Mirror it for Kotlin here.
+ """
+ names: list[str] = []
+ modifiers = None
+ for child in declaration_node.children:
+ if child.type == "modifiers":
+ modifiers = child
+ break
+ if modifiers is None:
+ return names
+ for anno in modifiers.children:
+ if anno.type != "annotation":
+ continue
+ user_type_node = None
+ for c in anno.children:
+ if c.type == "user_type":
+ user_type_node = c
+ break
+ if c.type == "constructor_invocation":
+ for cc in c.children:
+ if cc.type == "user_type":
+ user_type_node = cc
+ break
+ break
+ if user_type_node is None:
+ continue
+ name = _kotlin_user_type_name(user_type_node, source)
+ if name:
+ names.append(name)
+ return names
+
+def _kotlin_member_name(node, source: bytes) -> str | None:
+ """Property/param name for a Kotlin `class_parameter` (direct identifier) or
+ `property_declaration` (`variable_declaration -> identifier`)."""
+ if node.type == "class_parameter":
+ for c in node.children:
+ if c.type in ("identifier", "simple_identifier"):
+ return _read_text(c, source)
+ return None
+ for c in node.children:
+ if c.type == "variable_declaration":
+ for cc in c.children:
+ if cc.type in ("identifier", "simple_identifier"):
+ return _read_text(cc, source)
+ return None
+
+def _kotlin_param_type_node(cp):
+ """The `user_type` node of a Kotlin `class_parameter`, unwrapping `nullable_type`."""
+ for c in cp.children:
+ if c.type == "user_type":
+ return c
+ if c.type == "nullable_type":
+ for cc in c.children:
+ if cc.type == "user_type":
+ return cc
+ return None
+
def _kotlin_function_return_type_node(func_node):
"""Find the return-type node of a Kotlin function_declaration (the type after `: ` post-params)."""
saw_params = False
@@ -4097,6 +4161,54 @@
target = ensure_named_node(ref_name, line)
add_edge(class_nid, target, "references", line,
context="generic_arg")
+
+ # Class-level annotations (@Entity, @MappedSuperclass, @Embeddable,
+ # …). The Java harvester at the class site is gated to java/groovy,
+ # so Kotlin annotations never became edges. Emit the same
+ # references/attribute edges here.
+ for anno_name in _kotlin_annotation_names(node, source):
+ target_nid = ensure_named_node(anno_name, line)
+ if target_nid != class_nid:
+ add_edge(class_nid, target_nid, "references", line,
+ context="attribute")
+
+ # Primary-constructor `val`/`var` params are properties with no
+ # walk-visited node of their own (class-body property_declarations
+ # are handled during recursion). Give each its own node + type +
+ # annotation edges (@Column, @Id, @ManyToOne, @OneToMany, …),
+ # parallel to class-body properties, so JPA mapping/relationship
+ # annotations are queryable per field. Path is
+ # primary_constructor -> class_parameters -> class_parameter.
+ for child in node.children:
+ if child.type != "primary_constructor":
+ continue
+ for cps in child.children:
+ if cps.type != "class_parameters":
+ continue
+ for cp in cps.children:
+ if cp.type != "class_parameter":
+ continue
+ mline = cp.start_point[0] + 1
+ pname = _kotlin_member_name(cp, source)
+ pnid = class_nid
+ if pname:
+ pnid = _make_id(class_nid, pname)
+ add_node(pnid, f".{pname}", mline)
+ add_edge(class_nid, pnid, "defines", mline,
+ context="field")
+ ptype = _kotlin_param_type_node(cp)
+ if ptype is not None:
+ prefs: list[tuple[str, str]] = []
+ _kotlin_collect_type_refs(ptype, source, False, prefs)
+ for ref_name, role in prefs:
+ ctx = "generic_arg" if role == "generic_arg" else "field"
+ tgt = ensure_named_node(ref_name, mline)
+ if tgt != pnid:
+ add_edge(pnid, tgt, "references", mline, context=ctx)
+ for anno_name in _kotlin_annotation_names(cp, source):
+ tgt = ensure_named_node(anno_name, mline)
+ if tgt != pnid:
+ add_edge(pnid, tgt, "references", mline, context="attribute")
# Ruby: `class Dog < Animal` puts the base class in the `superclass`
# field (a `<` token followed by a constant or scope_resolution).
@@ -4764,16 +4876,30 @@
# Field-type references stay class-gated: top-level properties keep
# their pre-#2565 (no-references) behavior unchanged.
if parent_class_nid:
+ line = node.start_point[0] + 1
+ # Give the property its own node (parallels method nodes: `.name`),
+ # so its type + annotations attach per-property rather than lumping
+ # onto the class. Falls back to the class if the name is unresolvable
+ # (e.g. destructuring `val (a, b) = ...`).
+ prop_name = _kotlin_member_name(node, source)
+ prop_nid = parent_class_nid
+ if prop_name:
+ prop_nid = _make_id(parent_class_nid, prop_name)
+ add_node(prop_nid, f".{prop_name}", line)
+ add_edge(parent_class_nid, prop_nid, "defines", line, context="field")
type_node = _kotlin_property_type_node(node)
if type_node is not None:
- line = node.start_point[0] + 1
refs: list[tuple[str, str]] = []
_kotlin_collect_type_refs(type_node, source, False, refs)
for ref_name, role in refs:
ctx = "generic_arg" if role == "generic_arg" else "field"
target_nid = ensure_named_node(ref_name, line)
- if target_nid != parent_class_nid:
- add_edge(parent_class_nid, target_nid, "references", line, context=ctx)
+ if target_nid != prop_nid:
+ add_edge(prop_nid, target_nid, "references", line, context=ctx)
+ for anno_name in _kotlin_annotation_names(node, source):
+ target_nid = ensure_named_node(anno_name, line)
+ if target_nid != prop_nid:
+ add_edge(prop_nid, target_nid, "references", line, context="attribute")
# #2565: seed the initializer into initializer_nodes so walk_calls
# collects its calls (`val repo = createRepo()`), which previously
# died at the `return` below. Seeding the WHOLE expression (not just
@@ -5217,6 +5343,16 @@
add_edge(func_nid, target_nid, "references", line,
context="attribute")
annotation_targets.add(target_nid)
+
+ # Kotlin function/getter annotations (e.g. @Transient on a computed
+ # property getter). Members with their own node attach here; the Java
+ # site above is java-only.
+ if config.ts_module == "tree_sitter_kotlin":
+ for anno_name in _kotlin_annotation_names(node, source):
+ target_nid = ensure_named_node(anno_name, line)
+ if target_nid != func_nid:
+ add_edge(func_nid, target_nid, "references", line,
+ context="attribute")
if config.ts_module == "tree_sitter_php":
params_container = None
Activity
fix(extract): extract Kotlin annotations and primary constructor prop…