lombok.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Lombok-generated member synthesis (Java, #912).
  3. *
  4. * Lombok generates getters/setters/builder/equals/hashCode/toString and the
  5. * `log` field at compile time, so they never appear in the source AST. Without
  6. * synthesis they're absent from the index and any `bean.getX()` / `Bean.builder()`
  7. * / `log.info()` call resolves to nothing — call chains break silently. We
  8. * synthesize the mechanical ones from the annotations + fields, mark them
  9. * (`lombok` decorator + a docstring naming the source annotation), and they then
  10. * resolve as ordinary call targets. These tests prove the synthesis, the call
  11. * resolution that motivated it, and the precision boundaries (static fields
  12. * skipped, hand-written members never overridden, a non-Lombok class is clean).
  13. */
  14. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  15. import * as fs from 'node:fs';
  16. import * as path from 'node:path';
  17. import * as os from 'node:os';
  18. import { CodeGraph } from '../src';
  19. describe('lombok synthesis', () => {
  20. let dir: string;
  21. beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'lombok-')); });
  22. afterEach(() => { fs.rmSync(dir, { recursive: true, force: true }); });
  23. const write = (rel: string, body: string) => {
  24. const p = path.join(dir, rel);
  25. fs.mkdirSync(path.dirname(p), { recursive: true });
  26. fs.writeFileSync(p, body);
  27. };
  28. type Row = { name: string; kind: string; decorators: string | null; docstring: string | null; signature: string | null };
  29. const load = async () => {
  30. const cg = await CodeGraph.init(dir, { silent: true });
  31. await cg.indexAll();
  32. const db = (cg as any).db.db;
  33. const nodes: Row[] = db.prepare(`SELECT name, kind, decorators, docstring, signature FROM nodes`).all();
  34. const calls: { src: string; tgt: string }[] = db
  35. .prepare(
  36. `SELECT s.name src, t.name tgt FROM edges e
  37. JOIN nodes s ON s.id = e.source JOIN nodes t ON t.id = e.target
  38. WHERE e.kind = 'calls'`
  39. )
  40. .all();
  41. cg.close?.();
  42. return { nodes, calls };
  43. };
  44. const isLombok = (n: Row | undefined) => !!n && (n.decorators ?? '').includes('lombok');
  45. it('synthesizes accessors that resolve as call targets, and the @Slf4j log field', async () => {
  46. write('model/User.java', `package model;
  47. import lombok.Data;
  48. import lombok.Builder;
  49. import lombok.extern.slf4j.Slf4j;
  50. @Data
  51. @Builder
  52. @Slf4j
  53. public class User {
  54. private String name;
  55. private boolean active;
  56. private static final int MAX = 10;
  57. }
  58. `);
  59. write('svc/UserService.java', `package svc;
  60. import model.User;
  61. class UserService {
  62. String describe(User user) {
  63. user.setActive(true);
  64. return user.getName();
  65. }
  66. User make() {
  67. return User.builder();
  68. }
  69. }
  70. `);
  71. const { nodes, calls } = await load();
  72. const byName = (name: string) => nodes.find((n) => n.name === name && isLombok(n));
  73. // Accessors + Data contract + builder are synthesized and marked.
  74. for (const m of ['getName', 'setName', 'isActive', 'setActive', 'builder', 'equals', 'hashCode', 'toString']) {
  75. expect(isLombok(byName(m)), `expected synthesized ${m}`).toBe(true);
  76. }
  77. expect(byName('getName')!.docstring).toMatch(/Lombok-generated/);
  78. expect(byName('getName')!.signature).toBe('String getName()');
  79. expect(byName('isActive')!.signature).toBe('boolean isActive()'); // boolean → is-prefix
  80. expect(byName('builder')!.signature).toContain('static ');
  81. // @Slf4j → a `log` field.
  82. expect(isLombok(nodes.find((n) => n.name === 'log' && n.kind === 'field'))).toBe(true);
  83. // PRECISION: a static field gets no accessor.
  84. expect(nodes.some((n) => n.name === 'getMAX' || n.name === 'getMax')).toBe(false);
  85. // THE FIX: calls to Lombok-generated methods resolve to their synthesized target.
  86. const resolved = (src: string, tgt: string) => calls.some((c) => c.src === src && c.tgt === tgt);
  87. expect(resolved('describe', 'getName')).toBe(true);
  88. expect(resolved('describe', 'setActive')).toBe(true);
  89. expect(resolved('make', 'builder')).toBe(true);
  90. });
  91. it('never overrides a hand-written accessor', async () => {
  92. write('model/Account.java', `package model;
  93. import lombok.Getter;
  94. @Getter
  95. public class Account {
  96. private int balance;
  97. private String owner;
  98. // explicit getter — Lombok skips it, so must we
  99. public int getBalance() { return balance < 0 ? 0 : balance; }
  100. }
  101. `);
  102. const { nodes } = await load();
  103. const getBalance = nodes.filter((n) => n.name === 'getBalance');
  104. expect(getBalance.length).toBe(1); // exactly one, not duplicated
  105. expect(isLombok(getBalance[0])).toBe(false); // the hand-written one survives
  106. // the un-shadowed field still gets its synthesized getter
  107. expect(isLombok(nodes.find((n) => n.name === 'getOwner'))).toBe(true);
  108. });
  109. it('field-level @Getter/@Setter and final-field rules', async () => {
  110. write('model/Box.java', `package model;
  111. import lombok.Getter;
  112. import lombok.Setter;
  113. public class Box {
  114. @Getter @Setter private String label;
  115. @Getter private final long id; // final → getter only, no setter
  116. private int hidden; // no annotation → nothing
  117. }
  118. `);
  119. const { nodes } = await load();
  120. expect(isLombok(nodes.find((n) => n.name === 'getLabel'))).toBe(true);
  121. expect(isLombok(nodes.find((n) => n.name === 'setLabel'))).toBe(true);
  122. expect(isLombok(nodes.find((n) => n.name === 'getId'))).toBe(true);
  123. expect(nodes.some((n) => n.name === 'setId')).toBe(false); // final → no setter
  124. expect(nodes.some((n) => n.name === 'getHidden')).toBe(false); // un-annotated → nothing
  125. });
  126. it('produces no synthesized members for a plain Java class (clean control)', async () => {
  127. write('model/Plain.java', `package model;
  128. public class Plain {
  129. private int value;
  130. public int getValue() { return value; }
  131. public void setValue(int v) { this.value = v; }
  132. }
  133. `);
  134. const { nodes } = await load();
  135. expect(nodes.some((n) => isLombok(n))).toBe(false);
  136. });
  137. });