Lombok generates getters/setters, builder(), equals/hashCode/toString, and the @Slf4j log field at compile time, so they never appear in the source AST. Static extraction missed them entirely, so a bean.getName() / User.builder() / log.info() call resolved to nothing and call-chain analysis broke silently — the agent would conclude the method didn't exist. Add a synthesizeMembers hook on LanguageExtractor, called at the end of class extraction (class still on the scope stack, real members already extracted), and a Java implementation that synthesizes the mechanical members for @Getter, @Setter, @Data, @Value, @Builder/@SuperBuilder, @ToString, @EqualsAndHashCode, and the @Log* family. Each node is anchored on the field/class name-token leaf (so it pulls in no spurious value-reference scope), marked with a `lombok` decorator and a docstring naming the generating annotation, and never overrides a member the source already declares. Methods and fields are deduped separately since they're distinct namespaces in Java (a boolean field `isRunning` and its generated getter `isRunning()` coexist). Deliberately not synthesized: constructors (new X() already links via instantiates, and overloaded @NoArgs/@AllArgs/@RequiredArgs ctors would collide on a synthetic node id), fluent builder setters, and @Accessors(fluent=true). Validated on eladmin (274 Java files, Lombok-heavy): 100% accessor precision (878/878 map to a real field), 722 previously-broken calls now resolve; spring-petclinic (no Lombok) control synthesizes nothing. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
3e1547bbe1
commit
826810f128
@@ -1,6 +1,6 @@
|
||||
import type { Node as SyntaxNode } from 'web-tree-sitter';
|
||||
import { getNodeText, getChildByField } from '../tree-sitter-helpers';
|
||||
import type { LanguageExtractor } from '../tree-sitter-types';
|
||||
import type { ExtractorContext, LanguageExtractor } from '../tree-sitter-types';
|
||||
|
||||
/**
|
||||
* Tree-sitter-java node types for a method's `type` (return) field that can
|
||||
@@ -15,18 +15,16 @@ const JAVA_NON_CLASS_RETURN_NODES = new Set([
|
||||
]);
|
||||
|
||||
/**
|
||||
* A Java method's declared return type, normalized to the bare class name a
|
||||
* chained `Foo.getInstance().bar()` could be called on (the #645/#608 mechanism).
|
||||
* Reads the `type` field: primitives/void/arrays yield undefined (no class to
|
||||
* chain on), `List<Foo>` is unwrapped to its base type `List`, and a dotted
|
||||
* package/outer-class qualifier (`java.util.List`) is stripped to the simple
|
||||
* name. Constructors have no `type` field → undefined.
|
||||
* Normalize a Java type node to the bare class name a chained
|
||||
* `foo.getThing().bar()` could be called on (the #645/#608 mechanism):
|
||||
* primitives/void/arrays yield undefined (no class to chain on), `List<Foo>`
|
||||
* is unwrapped to its base `List`, and a dotted package/outer-class qualifier
|
||||
* (`java.util.List`) is stripped to the simple name.
|
||||
*/
|
||||
function extractJavaReturnType(node: SyntaxNode, source: string): string | undefined {
|
||||
const typeNode = getChildByField(node, 'type');
|
||||
function normalizeJavaType(typeNode: SyntaxNode | null, source: string): string | undefined {
|
||||
if (!typeNode) return undefined;
|
||||
if (JAVA_NON_CLASS_RETURN_NODES.has(typeNode.type)) return undefined;
|
||||
// An array return (`Foo[]`) isn't a receiver you call instance methods on.
|
||||
// An array (`Foo[]`) isn't a receiver you call instance methods on.
|
||||
if (typeNode.type === 'array_type') return undefined;
|
||||
// Strip type arguments (`List<Foo>` → `List`) — the chain resolves on the base.
|
||||
const raw = getNodeText(typeNode, source).trim().replace(/<[^>]*>/g, '');
|
||||
@@ -36,6 +34,222 @@ function extractJavaReturnType(node: SyntaxNode, source: string): string | undef
|
||||
return last;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Java method's declared return type. Reads the `type` field; constructors
|
||||
* (no `type` field) → undefined.
|
||||
*/
|
||||
function extractJavaReturnType(node: SyntaxNode, source: string): string | undefined {
|
||||
return normalizeJavaType(getChildByField(node, 'type'), source);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Lombok-generated member synthesis (#912)
|
||||
// ---------------------------------------------------------------------------
|
||||
// Lombok generates methods at compile time, so they never appear in the source
|
||||
// AST and static extraction misses them — `bean.getX()`, `bean.setX()`,
|
||||
// `Bean.builder()`, and `log.info(...)` calls then resolve to nothing and call
|
||||
// chains break silently. We synthesize the mechanical, well-documented ones.
|
||||
|
||||
/** Lombok logging annotations — all generate a field named `log` by default. */
|
||||
const LOMBOK_LOG_ANNOTATIONS = new Set([
|
||||
'Slf4j', 'Log4j', 'Log4j2', 'Log', 'CommonsLog', 'JBossLog', 'Flogger', 'XSlf4j', 'CustomLog',
|
||||
]);
|
||||
|
||||
/** Simple names of every annotation in a node's `modifiers` child (`@lombok.Getter` → `Getter`). */
|
||||
function lombokAnnotationNames(node: SyntaxNode): Set<string> {
|
||||
const names = new Set<string>();
|
||||
const modifiers = node.namedChildren.find((c: SyntaxNode) => c.type === 'modifiers');
|
||||
if (!modifiers) return names;
|
||||
for (const child of modifiers.namedChildren) {
|
||||
if (child.type === 'marker_annotation' || child.type === 'annotation') {
|
||||
const nameNode = getChildByField(child, 'name');
|
||||
const simple = nameNode ? nameNode.text.trim().split('.').pop() : undefined;
|
||||
if (simple) names.add(simple);
|
||||
}
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
/** Text of a declaration's `modifiers` child (keyword modifiers are anonymous, so match on text). */
|
||||
function modifierTextOf(node: SyntaxNode): string {
|
||||
const modifiers = node.namedChildren.find((c: SyntaxNode) => c.type === 'modifiers');
|
||||
return modifiers ? modifiers.text : '';
|
||||
}
|
||||
|
||||
function capitalizeJava(name: string): string {
|
||||
return name ? name.charAt(0).toUpperCase() + name.slice(1) : name;
|
||||
}
|
||||
|
||||
/** Lombok getter name: `getX`, or `isX` for a primitive boolean (keeping an existing `isFoo` field name). */
|
||||
function lombokGetterName(fieldName: string, isBooleanPrimitive: boolean): string {
|
||||
if (isBooleanPrimitive) {
|
||||
return /^is[A-Z]/.test(fieldName) ? fieldName : 'is' + capitalizeJava(fieldName);
|
||||
}
|
||||
return 'get' + capitalizeJava(fieldName);
|
||||
}
|
||||
|
||||
/** Lombok setter name: `setX` (a primitive boolean field `isFoo` sets via `setFoo`). */
|
||||
function lombokSetterName(fieldName: string, isBooleanPrimitive: boolean): string {
|
||||
const base = isBooleanPrimitive && /^is[A-Z]/.test(fieldName) ? fieldName.slice(2) : fieldName;
|
||||
return 'set' + capitalizeJava(base);
|
||||
}
|
||||
|
||||
/**
|
||||
* Synthesize the members Lombok generates at compile time. Covers the common,
|
||||
* mechanical annotations:
|
||||
*
|
||||
* @Getter / @Setter (class- or field-level) → getX()/isX(), setX()
|
||||
* @Data → getters + setters (non-final)
|
||||
* + equals/hashCode/toString
|
||||
* @Value → getters + equals/hashCode/toString (immutable, no setters)
|
||||
* @Builder / @SuperBuilder → static builder()
|
||||
* @ToString / @EqualsAndHashCode → those methods
|
||||
* @Slf4j and the other @Log* annotations → the `log` field
|
||||
*
|
||||
* Each node is anchored on the field's (or class's) name token — a leaf, so it
|
||||
* pulls in no spurious value-reference scope — carries a `lombok` decorator and
|
||||
* a docstring naming the generating annotation, so it reads as generated rather
|
||||
* than hand-written. Deliberately NOT synthesized: constructors (`new X()`
|
||||
* already links to the class via `instantiates`, and overloaded
|
||||
* @NoArgs/@AllArgs/@RequiredArgs ctors share a name → would collide on a
|
||||
* synthetic node id), the fluent builder setters, and `@Accessors(fluent=true)`
|
||||
* naming. A member the source already declares is never overridden.
|
||||
*/
|
||||
function synthesizeLombokMembers(classNode: SyntaxNode, ctx: ExtractorContext): void {
|
||||
const classAnns = lombokAnnotationNames(classNode);
|
||||
const classGetter = classAnns.has('Getter');
|
||||
const classSetter = classAnns.has('Setter');
|
||||
const isData = classAnns.has('Data');
|
||||
const isValue = classAnns.has('Value');
|
||||
const hasBuilder = classAnns.has('Builder') || classAnns.has('SuperBuilder');
|
||||
const hasToString = isData || isValue || classAnns.has('ToString');
|
||||
const hasEquals = isData || isValue || classAnns.has('EqualsAndHashCode');
|
||||
const logAnn = [...classAnns].find((a) => LOMBOK_LOG_ANNOTATIONS.has(a));
|
||||
|
||||
const body = getChildByField(classNode, 'body');
|
||||
if (!body) return;
|
||||
const fields = body.namedChildren.filter((c: SyntaxNode) => c.type === 'field_declaration');
|
||||
|
||||
// Leave immediately when nothing Lombok is present, so a non-Lombok class
|
||||
// pays nothing beyond one scan of its direct field declarations (and an
|
||||
// annotated class skips even that — this hook runs for every Java class).
|
||||
const classHasLombok =
|
||||
classGetter || classSetter || isData || isValue || hasBuilder || hasToString || hasEquals || !!logAnn;
|
||||
if (!classHasLombok && !fields.some((f: SyntaxNode) => lombokAnnotationNames(f).size > 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Members already declared directly in this class. Lombok never overrides an
|
||||
// explicit member, so we skip a name the source already has. Methods and
|
||||
// fields are tracked separately: they're distinct namespaces in Java (a
|
||||
// boolean field `isRunning` and its generated getter `isRunning()` coexist),
|
||||
// and the node id is keyed by kind so they never actually collide.
|
||||
const classId = ctx.nodeStack[ctx.nodeStack.length - 1];
|
||||
const classRec = ctx.nodes.find((n) => n.id === classId);
|
||||
const classQN = classRec?.qualifiedName;
|
||||
const takenMethods = new Set<string>();
|
||||
const takenFields = new Set<string>();
|
||||
if (classQN) {
|
||||
for (const n of ctx.nodes) {
|
||||
if (n.filePath === ctx.filePath && n.qualifiedName === `${classQN}::${n.name}`) {
|
||||
if (n.kind === 'method' || n.kind === 'function') takenMethods.add(n.name);
|
||||
else if (n.kind === 'field' || n.kind === 'variable' || n.kind === 'constant' || n.kind === 'property') {
|
||||
takenFields.add(n.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const classNameNode = getChildByField(classNode, 'name') ?? classNode;
|
||||
const className = classRec?.name ?? getNodeText(classNameNode, ctx.source).trim();
|
||||
|
||||
const emitMethod = (
|
||||
name: string,
|
||||
anchor: SyntaxNode,
|
||||
signature: string,
|
||||
fromAnnotation: string,
|
||||
extra: { returnType?: string; isStatic?: boolean } = {}
|
||||
): void => {
|
||||
if (!name || takenMethods.has(name)) return;
|
||||
takenMethods.add(name);
|
||||
ctx.createNode('method', name, anchor, {
|
||||
visibility: 'public',
|
||||
signature,
|
||||
docstring: `Lombok-generated (${fromAnnotation})`,
|
||||
decorators: ['lombok'],
|
||||
isStatic: extra.isStatic,
|
||||
returnType: extra.returnType,
|
||||
});
|
||||
};
|
||||
|
||||
// Per-field getters/setters.
|
||||
for (const fd of fields) {
|
||||
const mods = modifierTextOf(fd);
|
||||
if (/\bstatic\b/.test(mods)) continue; // Lombok skips static fields.
|
||||
const isFinal = /\bfinal\b/.test(mods);
|
||||
const fieldAnns = lombokAnnotationNames(fd);
|
||||
const fieldGetter = fieldAnns.has('Getter');
|
||||
const fieldSetter = fieldAnns.has('Setter');
|
||||
|
||||
const wantGetter = classGetter || isData || isValue || fieldGetter;
|
||||
const wantSetter = (classSetter || isData || fieldSetter) && !isFinal;
|
||||
if (!wantGetter && !wantSetter) continue;
|
||||
|
||||
const typeNode = getChildByField(fd, 'type');
|
||||
const typeText = typeNode ? getNodeText(typeNode, ctx.source).trim() : 'Object';
|
||||
const isBooleanPrimitive = typeNode?.type === 'boolean_type';
|
||||
const returnType = normalizeJavaType(typeNode, ctx.source);
|
||||
|
||||
for (const vd of fd.namedChildren) {
|
||||
if (vd.type !== 'variable_declarator') continue;
|
||||
const nameNode = getChildByField(vd, 'name');
|
||||
if (!nameNode) continue;
|
||||
const fieldName = getNodeText(nameNode, ctx.source).trim();
|
||||
if (!fieldName) continue;
|
||||
|
||||
if (wantGetter) {
|
||||
const g = lombokGetterName(fieldName, isBooleanPrimitive);
|
||||
emitMethod(g, nameNode, `${typeText} ${g}()`,
|
||||
fieldGetter ? '@Getter' : isData ? '@Data' : isValue ? '@Value' : '@Getter',
|
||||
{ returnType });
|
||||
}
|
||||
if (wantSetter) {
|
||||
const s = lombokSetterName(fieldName, isBooleanPrimitive);
|
||||
emitMethod(s, nameNode, `void ${s}(${typeText} ${fieldName})`,
|
||||
fieldSetter ? '@Setter' : isData ? '@Data' : '@Setter');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Class-level synthesized methods.
|
||||
if (hasBuilder) {
|
||||
emitMethod('builder', classNameNode, `static ${className}.${className}Builder builder()`,
|
||||
classAnns.has('SuperBuilder') ? '@SuperBuilder' : '@Builder',
|
||||
{ isStatic: true, returnType: `${className}Builder` });
|
||||
}
|
||||
if (hasToString) {
|
||||
emitMethod('toString', classNameNode, 'String toString()',
|
||||
isData ? '@Data' : isValue ? '@Value' : '@ToString');
|
||||
}
|
||||
if (hasEquals) {
|
||||
const from = isData ? '@Data' : isValue ? '@Value' : '@EqualsAndHashCode';
|
||||
emitMethod('equals', classNameNode, 'boolean equals(Object o)', from);
|
||||
emitMethod('hashCode', classNameNode, 'int hashCode()', from);
|
||||
}
|
||||
|
||||
// Logger field (@Slf4j and friends).
|
||||
if (logAnn && !takenFields.has('log')) {
|
||||
takenFields.add('log');
|
||||
ctx.createNode('field', 'log', classNameNode, {
|
||||
visibility: 'private',
|
||||
isStatic: true,
|
||||
signature: 'Logger log',
|
||||
docstring: `Lombok-generated (@${logAnn})`,
|
||||
decorators: ['lombok'],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const javaExtractor: LanguageExtractor = {
|
||||
functionTypes: [],
|
||||
classTypes: ['class_declaration'],
|
||||
@@ -58,6 +272,7 @@ export const javaExtractor: LanguageExtractor = {
|
||||
paramsField: 'parameters',
|
||||
returnField: 'type',
|
||||
getReturnType: extractJavaReturnType,
|
||||
synthesizeMembers: synthesizeLombokMembers,
|
||||
getSignature: (node, source) => {
|
||||
const params = getChildByField(node, 'parameters');
|
||||
const returnType = getChildByField(node, 'type');
|
||||
|
||||
@@ -174,6 +174,20 @@ export interface LanguageExtractor {
|
||||
*/
|
||||
visitNode?: (node: SyntaxNode, ctx: ExtractorContext) => boolean;
|
||||
|
||||
/**
|
||||
* Synthesize members that exist at compile time but not in the source AST,
|
||||
* called at the end of class extraction with the class still on the scope
|
||||
* stack (so `ctx.createNode` attaches containment + qualified names) and the
|
||||
* class's real members already extracted (so the hook can skip a member the
|
||||
* source explicitly declares). Used by Java for Lombok-generated accessors
|
||||
* (`@Getter`/`@Setter`/`@Data`/`@Value`/`@Builder` → `getX`/`setX`/`builder`/
|
||||
* `equals`/`hashCode`/`toString` + the `log` field), which are otherwise
|
||||
* invisible and break call-chain analysis (#912). The created nodes carry a
|
||||
* `lombok` decorator + a docstring naming the generating annotation, so an
|
||||
* agent can tell them apart from hand-written code.
|
||||
*/
|
||||
synthesizeMembers?: (classNode: SyntaxNode, ctx: ExtractorContext) => void;
|
||||
|
||||
/**
|
||||
* Classify a class_declaration node when the grammar reuses one node type
|
||||
* for multiple concepts (e.g. Swift uses class_declaration for classes, structs, and enums).
|
||||
|
||||
@@ -1530,6 +1530,14 @@ export class TreeSitterExtractor {
|
||||
this.visitNode(child);
|
||||
}
|
||||
}
|
||||
|
||||
// Synthesize compile-time-generated members (Lombok accessors, #912). Runs
|
||||
// after the body so the hook can dedup against hand-written members, and
|
||||
// while the class is still on the stack so containment/QNs attach.
|
||||
if (this.extractor.synthesizeMembers) {
|
||||
this.extractor.synthesizeMembers(node, this.makeExtractorContext());
|
||||
}
|
||||
|
||||
this.nodeStack.pop();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user