@@ -230,6 +230,7 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
230230 const CBMLangSpec *spec);
231231static TSNode find_class_body(TSNode class_node, CBMLanguage lang);
232232static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
233+ static void extract_record_accessors(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
233234static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
234235
235236// --- Helpers ---
@@ -8317,6 +8318,10 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
83178318 extract_enum_members(ctx, node, class_qn);
83188319 }
83198320
8321+ if (ctx->language == CBM_LANG_JAVA && strcmp(kind, "record_declaration") == 0) {
8322+ extract_record_accessors(ctx, node, class_qn);
8323+ }
8324+
83208325 // Extract methods inside the class
83218326 int methods_from = ctx->result->defs.count;
83228327 extract_class_methods(ctx, node, class_qn, spec);
@@ -9642,6 +9647,121 @@ static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *cl
96429647 }
96439648}
96449649
9650+ /* Whether the record body declares this accessor itself (JLS §8.10.3 lets a
9651+ * record override any of them), so the implicit one is not emitted twice. */
9652+ static bool record_declares_accessor(CBMExtractCtx *ctx, TSNode node, const char *comp_name) {
9653+ TSNode body = find_class_body(node, ctx->language);
9654+ if (ts_node_is_null(body)) {
9655+ return false;
9656+ }
9657+ uint32_t mc = ts_node_named_child_count(body);
9658+ for (uint32_t mi = 0; mi < mc; mi++) {
9659+ TSNode member = ts_node_named_child(body, mi);
9660+ if (strcmp(ts_node_type(member), "method_declaration") != 0) {
9661+ continue;
9662+ }
9663+ TSNode mname = ts_node_child_by_field_name(member, TS_FIELD("name"));
9664+ if (ts_node_is_null(mname)) {
9665+ continue;
9666+ }
9667+ char *name = cbm_node_text(ctx->arena, mname, ctx->source);
9668+ if (!name || strcmp(name, comp_name) != 0) {
9669+ continue;
9670+ }
9671+ /* Same name, no parameters — that is the accessor. */
9672+ TSNode params = ts_node_child_by_field_name(member, TS_FIELD("parameters"));
9673+ if (ts_node_is_null(params)) {
9674+ return true;
9675+ }
9676+ /* Comments are named extras, not parameters. */
9677+ bool has_parameters = false;
9678+ uint32_t pc = ts_node_named_child_count(params);
9679+ for (uint32_t pi = 0; pi < pc; pi++) {
9680+ if (!ts_node_is_extra(ts_node_named_child(params, pi))) {
9681+ has_parameters = true;
9682+ break;
9683+ }
9684+ }
9685+ if (!has_parameters) {
9686+ return true;
9687+ }
9688+ }
9689+ return false;
9690+ }
9691+
9692+ /* Java records: the compiler writes one accessor per component, so `p.x()`
9693+ * calls a method that appears nowhere in the source. Without a definition for
9694+ * it the call has nothing to resolve to and falls through to the project-wide
9695+ * name registry, which binds it to the first `x` in the tree — typically an
9696+ * unrelated class's private field. Emitting the accessors makes the record's
9697+ * own API visible, exactly as the language defines it (JLS §8.10.3). */
9698+ static void extract_record_accessors(CBMExtractCtx *ctx, TSNode node, const char *class_qn) {
9699+ CBMArena *a = ctx->arena;
9700+ TSNode params = ts_node_child_by_field_name(node, TS_FIELD("parameters"));
9701+ if (ts_node_is_null(params)) {
9702+ return;
9703+ }
9704+ uint32_t pc = ts_node_named_child_count(params);
9705+ for (uint32_t pi = 0; pi < pc; pi++) {
9706+ TSNode comp = ts_node_named_child(params, pi);
9707+ bool spread = strcmp(ts_node_type(comp), "spread_parameter") == 0;
9708+ if (!spread && strcmp(ts_node_type(comp), "formal_parameter") != 0) {
9709+ continue;
9710+ }
9711+ TSNode cname = ts_node_child_by_field_name(comp, TS_FIELD("name"));
9712+ TSNode ctype = ts_node_child_by_field_name(comp, TS_FIELD("type"));
9713+ if (spread) {
9714+ /* Java varargs have an unnamed type child and a nested declarator. */
9715+ TSNode decl = cbm_find_child_by_kind(comp, "variable_declarator");
9716+ if (ts_node_is_null(decl)) {
9717+ continue;
9718+ }
9719+ cname = ts_node_child_by_field_name(decl, TS_FIELD("name"));
9720+ uint32_t nc = ts_node_named_child_count(comp);
9721+ for (uint32_t ci = 0; ci < nc; ci++) {
9722+ TSNode child = ts_node_named_child(comp, ci);
9723+ if (!ts_node_is_extra(child) && strcmp(ts_node_type(child), "modifiers") != 0) {
9724+ ctype = child;
9725+ break;
9726+ }
9727+ }
9728+ }
9729+ if (ts_node_is_null(cname)) {
9730+ continue;
9731+ }
9732+ char *comp_name = cbm_node_text(a, cname, ctx->source);
9733+ if (!comp_name || !comp_name[0] || record_declares_accessor(ctx, node, comp_name)) {
9734+ continue;
9735+ }
9736+ CBMDefinition adef;
9737+ memset(&adef, 0, sizeof(adef));
9738+ adef.name = comp_name;
9739+ adef.qualified_name = cbm_arena_sprintf(a, "%s.%s", class_qn, comp_name);
9740+ adef.label = "Method";
9741+ adef.file_path = ctx->rel_path;
9742+ adef.start_line = ts_node_start_point(comp).row + TS_LINE_OFFSET;
9743+ adef.end_line = ts_node_end_point(comp).row + TS_LINE_OFFSET;
9744+ adef.lines = 1;
9745+ adef.parent_class = class_qn;
9746+ adef.is_exported = true;
9747+ if (!ts_node_is_null(ctype)) {
9748+ const char *type = cbm_node_text(a, ctype, ctx->source);
9749+ adef.return_type = spread && type ? cbm_arena_sprintf(a, "%s[]", type) : type;
9750+ }
9751+ /* The compiler writes this member and the source never spells it: its
9752+ * node says so, and the pipeline registers it by qualified name only,
9753+ * so no short-name guess can land on it. */
9754+ const char **kv = (const char **)cbm_arena_alloc(a, CBM_SZ_3 * sizeof(char *));
9755+ if (kv) {
9756+ kv[0] = CBM_PROP_GENERATED;
9757+ kv[SKIP_ONE] = "record";
9758+ kv[PAIR_LEN] = NULL;
9759+ }
9760+ adef.extra_props = kv;
9761+ cbm_defs_push(&ctx->result->defs, a, adef);
9762+ }
9763+ }
9764+
96459765/* Resolve the identifier node from a destructure pattern child.
96469766 * pair_pattern → value field; shorthand/identifier → itself; others → first named child. */
96479767static TSNode destructure_ident(TSNode pat_child) {
0 commit comments