• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / 964

12 Nov 2026 12:04PM UTC coverage: 80.243% (-0.005%) from 80.248%
964

push

github

web-flow
[java] Fix #6731: Support lombok.Builder (fix UnnecessaryFullyQualifiedName false positive) (#7198)

UnnecessaryFullyQualifiedName reported lombok.Builder when the generated
builder class was named Builder. Add that static nested class to the
enclosing type's symbol, as with other Lombok members, instead of
special-casing the rule. The default name is <TypeName>Builder; builderClassName
overrides it, and an existing nested class of that name is kept.

Fixes #6731

20766 of 26864 branches covered (77.3%)

Branch coverage included in aggregate %.

67 of 83 new or added lines in 5 files covered. (80.72%)

44277 of 54194 relevant lines covered (81.7%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.04
/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/ImplicitMemberSymbols.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.java.symbols.internal;
6

7
import static java.util.Collections.emptyList;
8
import static java.util.Collections.singletonList;
9

10
import java.lang.reflect.Modifier;
11
import java.util.List;
12
import java.util.function.BiFunction;
13
import java.util.function.Function;
14

15
import org.checkerframework.checker.nullness.qual.NonNull;
16
import org.checkerframework.checker.nullness.qual.Nullable;
17

18
import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType;
19
import net.sourceforge.pmd.lang.java.ast.ASTType;
20
import net.sourceforge.pmd.lang.java.ast.ASTVariableId;
21
import net.sourceforge.pmd.lang.java.internal.JavaAstProcessor;
22
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
23
import net.sourceforge.pmd.lang.java.symbols.JConstructorSymbol;
24
import net.sourceforge.pmd.lang.java.symbols.JExecutableSymbol;
25
import net.sourceforge.pmd.lang.java.symbols.JFieldSymbol;
26
import net.sourceforge.pmd.lang.java.symbols.JFormalParamSymbol;
27
import net.sourceforge.pmd.lang.java.symbols.JMethodSymbol;
28
import net.sourceforge.pmd.lang.java.symbols.JRecordComponentSymbol;
29
import net.sourceforge.pmd.lang.java.types.JClassType;
30
import net.sourceforge.pmd.lang.java.types.JPrimitiveType.PrimitiveTypeKind;
31
import net.sourceforge.pmd.lang.java.types.JTypeMirror;
32
import net.sourceforge.pmd.lang.java.types.JTypeVar;
33
import net.sourceforge.pmd.lang.java.types.Substitution;
34
import net.sourceforge.pmd.lang.java.types.TypeSystem;
35
import net.sourceforge.pmd.util.CollectionUtil;
36

37
/**
38
 * Members inserted by the compiler, eg default constructor, etc. They
39
 * would be absent from the source, but are reflected by the {@link Class}.
40
 */
41
public final class ImplicitMemberSymbols {
1 ✔
42

43
    private static final int VISIBILITY_MASK = Modifier.PRIVATE | Modifier.PUBLIC | Modifier.PROTECTED;
44
    /** This is the private access flag for varargs modifiers. */
45
    private static final int VARARGS_MOD = 0x00000080;
46

47
    private ImplicitMemberSymbols() {
48

49
    }
50

51
    public static JMethodSymbol enumValueOf(JClassSymbol enumSym) {
52
        assert enumSym.isEnum() : "Not an enum symbol " + enumSym;
1 !
53

54
        return new FakeMethodSym(
1 ✔
55
            enumSym,
56
            "valueOf",
57
            Modifier.PUBLIC | Modifier.STATIC,
58
            TypeSystem::declaration,
59
            singletonList(t -> new FakeFormalParamSym(t, "name", (ts, s) -> ts.declaration(ts.getClassSymbol(String.class))))
1 ✔
60
        );
61
    }
62

63
    public static JMethodSymbol enumValues(JClassSymbol enumSym) {
64
        assert enumSym.isEnum() : "Not an enum symbol " + enumSym;
1 !
65

66
        return new FakeMethodSym(
1 ✔
67
            enumSym,
68
            "values",
69
            Modifier.PUBLIC | Modifier.STATIC,
70
            (ts, c) -> ts.arrayType(ts.declaration(c)),
1 ✔
71
            emptyList()
1 ✔
72
        );
73
    }
74

75

76
    public static JConstructorSymbol defaultCtor(JClassSymbol sym) {
77
        assert sym != null;
1 !
78

79
        // Enum constructors have 2 additional implicit parameters, for the name and ordinal
80
        // Inner classes have 1 additional implicit param, for the outer instance
81
        // They are not reflected by the symbol
82

83
        int modifiers = sym.isEnum() ? Modifier.PRIVATE
1 ✔
84
                                     : sym.getModifiers() & VISIBILITY_MASK;
1 ✔
85

86
        return new FakeCtorSym(sym, modifiers, emptyList());
1 ✔
87
    }
88

89

90
    public static JMethodSymbol arrayClone(JClassSymbol arraySym) {
91
        assert arraySym.isArray() : "Not an array symbol " + arraySym;
1 !
92

93
        return new FakeMethodSym(
1 ✔
94
            arraySym,
95
            "clone",
96
            Modifier.PUBLIC | Modifier.FINAL,
97
            TypeSystem::declaration,
98
            emptyList()
1 ✔
99
        );
100
    }
101

102
    public static JConstructorSymbol arrayConstructor(JClassSymbol arraySym) {
103
        assert arraySym.isArray() : "Not an array symbol " + arraySym;
1 !
104

105
        return new FakeCtorSym(
1 ✔
106
            arraySym,
107
            Modifier.PUBLIC | Modifier.FINAL,
108
            singletonList(c -> new FakeFormalParamSym(c, "arg0", (ts, sym) -> ts.INT))
1 ✔
109
        );
110
    }
111

112
    /** Symbol for the canonical record constructor. */
113
    public static JConstructorSymbol recordConstructor(JClassSymbol recordSym,
114
                                                       List<JRecordComponentSymbol> recordComponents,
115
                                                       boolean isVarargs) {
116
        assert recordSym.isRecord() : "Not a record symbol " + recordSym;
1 !
117

118
        int modifiers = isVarargs ? Modifier.PUBLIC | VARARGS_MOD
1 ✔
119
                                  : Modifier.PUBLIC;
1 ✔
120

121
        return new FakeCtorSym(
1 ✔
122
            recordSym,
123
            modifiers,
124
            CollectionUtil.map(
1 ✔
125
                recordComponents,
126
                f -> c -> new FakeFormalParamSym(c, f.getSimpleName(), f.tryGetNode().getVarId(), (ts, sym) -> f.getTypeMirror(Substitution.EMPTY))
1 ✔
127
            )
128
        );
129
    }
130

131
    /**
132
     * Symbol for a record component accessor.
133
     * Only synthesized if it is not explicitly declared.
134
     */
135
    public static JMethodSymbol recordAccessor(JClassSymbol recordSym, JRecordComponentSymbol recordComponent) {
136
        // See https://cr.openjdk.java.net/~gbierman/jep359/jep359-20200115/specs/records-jls.html#jls-8.10.3
137

138
        assert recordSym.isRecord() : "Not a record symbol " + recordSym;
1 !
139

140
        return new FakeMethodSym(
1 ✔
141
            recordSym,
142
            recordComponent.getSimpleName(),
1 ✔
143
            Modifier.PUBLIC,
144
            (ts, encl) -> recordComponent.getTypeMirror(Substitution.EMPTY),
1 ✔
145
            emptyList()
1 ✔
146
        );
147
    }
148

149
    public static JFieldSymbol arrayLengthField(JClassSymbol arraySym) {
150
        assert arraySym.isArray() : "Not an array symbol " + arraySym;
1 !
151

152
        return new FakeFieldSym(
1 ✔
153
            arraySym,
154
            "length",
155
            Modifier.PUBLIC | Modifier.FINAL,
156
            (ts, s) -> ts.INT
1 ✔
157
        );
158
    }
159

160
    public static JFieldSymbol lombokSlf4jLoggerField(JClassSymbol classSym, JavaAstProcessor processor) {
161
        // https://javadoc.io/doc/org.projectlombok/lombok/1.16.18/lombok/extern/slf4j/Slf4j.html
162

163
        return new FakeFieldSym(
1 ✔
164
                classSym,
165
                "log",
166
                Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL,
167
                (ts, s) ->
168
                    ts.declaration(processor.findSymbolCannotFail("org.slf4j.Logger"))
1 ✔
169
        );
170
    }
171

172
    public static JMethodSymbol lombokGetter(JClassSymbol classSym, JFieldSymbol field, int accessModifier) {
173
        String fieldName = field.getSimpleName();
1 ✔
174
        boolean isBoolean = false;
1 ✔
175
        ASTVariableId varId = field.tryGetNode();
1 ✔
176
        if (varId != null) {
1 !
177
            ASTType typeNode = varId.getTypeNode();
1 ✔
178
            isBoolean = typeNode instanceof ASTPrimitiveType
1 ✔
179
                    && ((ASTPrimitiveType) typeNode).getKind() == PrimitiveTypeKind.BOOLEAN;
1 ✔
180
        }
181
        String name;
182
        if (isBoolean && fieldName.length() > 2 && fieldName.startsWith("is")
1 ✔
183
                && !Character.isLowerCase(fieldName.charAt(2))) {
1 ✔
184
            // lombok does not double the prefix: boolean isActive -> isActive()
185
            name = fieldName;
1 ✔
186
        } else {
187
            name = (isBoolean ? "is" : "get") + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
1 ✔
188
        }
189

190
        return new FakeMethodSym(
1 ✔
191
                classSym,
192
                name,
193
                accessModifier,
194
                (ts, s) -> field.getTypeMirror(Substitution.EMPTY),
1 ✔
195
                emptyList()
1 ✔
196
        );
197
    }
198

199
    /**
200
     * Static nested class generated by {@code lombok.Builder}.
201
     * See https://projectlombok.org/features/Builder
202
     */
203
    public static JClassSymbol lombokBuilderClass(JClassSymbol owner, String simpleName) {
204
        return new FakeClassSym(owner, simpleName);
1 ✔
205
    }
206

207
    private abstract static class FakeExecutableSymBase<T extends JExecutableSymbol> implements JExecutableSymbol {
208

209
        private final JClassSymbol owner;
210
        private final String name;
211
        private final int modifiers;
212
        private final List<JFormalParamSymbol> formals;
213

214

215
        FakeExecutableSymBase(JClassSymbol owner,
216
                              String name,
217
                              int modifiers,
218
                              List<Function<T, JFormalParamSymbol>> formals) {
1 ✔
219
            this.owner = owner;
1 ✔
220
            this.name = name;
1 ✔
221
            this.modifiers = modifiers;
1 ✔
222
            this.formals = CollectionUtil.map(formals, f -> f.apply((T) this));
1 ✔
223
        }
1 ✔
224

225
        @Override
226
        public TypeSystem getTypeSystem() {
227
            return owner.getTypeSystem();
1 ✔
228
        }
229

230
        @Override
231
        public List<JTypeMirror> getFormalParameterTypes(Substitution subst) {
232
            return CollectionUtil.map(formals, p -> p.getTypeMirror(subst));
1 ✔
233
        }
234

235
        @Override
236
        public List<JTypeMirror> getThrownExceptionTypes(Substitution subst) {
237
            return emptyList();
1 ✔
238
        }
239

240
        @Override
241
        public List<JTypeVar> getTypeParameters() {
242
            return emptyList();
1 ✔
243
        }
244

245
        @Override
246
        public String getSimpleName() {
247
            return name;
1 ✔
248
        }
249

250
        @Override
251
        public List<JFormalParamSymbol> getFormalParameters() {
252
            return formals;
1 ✔
253
        }
254

255
        @Override
256
        public boolean isVarargs() {
257
            return (modifiers & VARARGS_MOD) != 0;
1 ✔
258
        }
259

260
        @Override
261
        public int getArity() {
262
            return formals.size();
1 ✔
263
        }
264

265
        @Override
266
        public @Nullable JTypeMirror getAnnotatedReceiverType(Substitution subst) {
267
            if (!this.hasReceiver()) {
×
268
                return null;
×
269
            }
270
            return getTypeSystem().declaration(owner).subst(subst);
×
271
        }
272

273
        @Override
274
        public int getModifiers() {
275
            return modifiers;
1 ✔
276
        }
277

278
        @Override
279
        public @NonNull JClassSymbol getEnclosingClass() {
280
            return owner;
1 ✔
281
        }
282

283
        @Override
284
        public String toString() {
285
            return SymbolToStrings.FAKE.toString(this);
×
286
        }
287
    }
288

289
    private static final class FakeMethodSym extends FakeExecutableSymBase<JMethodSymbol> implements JMethodSymbol {
290

291
        private final BiFunction<? super TypeSystem, ? super JClassSymbol, ? extends JTypeMirror> returnType;
292

293
        FakeMethodSym(JClassSymbol owner,
294
                      String name,
295
                      int modifiers,
296
                      BiFunction<? super TypeSystem, ? super JClassSymbol, ? extends JTypeMirror> returnType,
297
                      List<Function<JMethodSymbol, JFormalParamSymbol>> formals) {
298
            super(owner, name, modifiers, formals);
1 ✔
299
            this.returnType = returnType;
1 ✔
300
        }
1 ✔
301

302
        @Override
303
        public boolean isBridge() {
304
            return false;
×
305
        }
306

307
        @Override
308
        public JTypeMirror getReturnType(Substitution subst) {
309
            return returnType.apply(getTypeSystem(), getEnclosingClass());
1 ✔
310
        }
311

312
        @Override
313
        public boolean equals(Object o) {
314
            return SymbolEquality.METHOD.equals(this, o);
1 ✔
315
        }
316

317
        @Override
318
        public int hashCode() {
319
            return SymbolEquality.METHOD.hash(this);
1 ✔
320
        }
321
    }
322

323
    private static final class FakeCtorSym extends FakeExecutableSymBase<JConstructorSymbol> implements JConstructorSymbol {
324

325
        FakeCtorSym(JClassSymbol owner,
326
                    int modifiers,
327
                    List<Function<JConstructorSymbol, JFormalParamSymbol>> formals) {
328
            super(owner, JConstructorSymbol.CTOR_NAME, modifiers, formals);
1 ✔
329
        }
1 ✔
330

331

332
        @Override
333
        public boolean equals(Object o) {
334
            return SymbolEquality.CONSTRUCTOR.equals(this, o);
1 ✔
335
        }
336

337
        @Override
338
        public int hashCode() {
339
            return SymbolEquality.CONSTRUCTOR.hash(this);
1 ✔
340
        }
341
    }
342

343
    private static final class FakeFormalParamSym implements JFormalParamSymbol {
344

345
        private final JExecutableSymbol owner;
346
        private final String name;
347
        private final ASTVariableId node;
348
        private final BiFunction<? super TypeSystem, ? super JFormalParamSymbol, ? extends JTypeMirror> type;
349

350
        private FakeFormalParamSym(JExecutableSymbol owner, String name, BiFunction<? super TypeSystem, ? super JFormalParamSymbol, ? extends JTypeMirror> type) {
351
            this(owner, name, null, type);
1 ✔
352
        }
1 ✔
353

354
        private FakeFormalParamSym(JExecutableSymbol owner, String name, @Nullable ASTVariableId node, BiFunction<? super TypeSystem, ? super JFormalParamSymbol, ? extends JTypeMirror> type) {
1 ✔
355
            this.owner = owner;
1 ✔
356
            this.name = name;
1 ✔
357
            this.node = node;
1 ✔
358
            this.type = type;
1 ✔
359
        }
1 ✔
360

361
        @Override
362
        public @Nullable ASTVariableId tryGetNode() {
363
            return node;
1 ✔
364
        }
365

366
        @Override
367
        public TypeSystem getTypeSystem() {
368
            return owner.getTypeSystem();
1 ✔
369
        }
370

371
        @Override
372
        public JTypeMirror getTypeMirror(Substitution subst) {
373
            return type.apply(getTypeSystem(), this).subst(subst);
1 ✔
374
        }
375

376
        @Override
377
        public JExecutableSymbol getDeclaringSymbol() {
378
            return owner;
1 ✔
379
        }
380

381
        @Override
382
        public boolean isFinal() {
383
            return false;
1 ✔
384
        }
385

386
        @Override
387
        public String getSimpleName() {
388
            return name;
1 ✔
389
        }
390

391
        @Override
392
        public String toString() {
393
            return SymbolToStrings.FAKE.toString(this);
×
394
        }
395

396
        @Override
397
        public boolean equals(Object o) {
398
            return SymbolEquality.FORMAL_PARAM.equals(this, o);
×
399
        }
400

401
        @Override
402
        public int hashCode() {
403
            return SymbolEquality.FORMAL_PARAM.hash(this);
1 ✔
404
        }
405
    }
406

407

408
    private static final class FakeFieldSym implements JFieldSymbol {
409

410
        private final JClassSymbol owner;
411
        private final String name;
412
        private final int modifiers;
413
        private final BiFunction<? super TypeSystem, ? super JClassSymbol, ? extends JTypeMirror> type;
414

415
        FakeFieldSym(JClassSymbol owner, String name, int modifiers, BiFunction<? super TypeSystem, ? super JClassSymbol, ? extends JTypeMirror> type) {
1 ✔
416
            this.owner = owner;
1 ✔
417
            this.name = name;
1 ✔
418
            this.modifiers = modifiers;
1 ✔
419
            this.type = type;
1 ✔
420
        }
1 ✔
421

422
        @Override
423
        public TypeSystem getTypeSystem() {
424
            return owner.getTypeSystem();
1 ✔
425
        }
426

427
        @Override
428
        public JTypeMirror getTypeMirror(Substitution subst) {
429
            return type.apply(getTypeSystem(), owner).subst(subst);
1 ✔
430
        }
431

432
        @Override
433
        public String getSimpleName() {
434
            return name;
1 ✔
435
        }
436

437
        @Override
438
        public int getModifiers() {
439
            return modifiers;
1 ✔
440
        }
441

442
        @Override
443
        public boolean isEnumConstant() {
444
            return false;
×
445
        }
446

447
        @Override
448
        public @NonNull JClassSymbol getEnclosingClass() {
449
            return owner;
1 ✔
450
        }
451

452
        @Override
453
        public String toString() {
454
            return SymbolToStrings.FAKE.toString(this);
1 ✔
455
        }
456

457
        @Override
458
        public boolean equals(Object o) {
459
            return SymbolEquality.FIELD.equals(this, o);
1 ✔
460
        }
461

462
        @Override
463
        public int hashCode() {
464
            return SymbolEquality.FIELD.hash(this);
1 ✔
465
        }
466
    }
467

468
    private static final class FakeClassSym extends EmptyClassSymbol {
469

470
        private final JClassSymbol owner;
471
        private final String simpleName;
472
        private final String binaryName;
473

474
        FakeClassSym(JClassSymbol owner, String simpleName) {
475
            super(owner::getTypeSystem);
1 ✔
476
            this.owner = owner;
1 ✔
477
            this.simpleName = simpleName;
1 ✔
478
            this.binaryName = owner.getBinaryName() + '$' + simpleName;
1 ✔
479
        }
1 ✔
480

481
        @Override
482
        public @NonNull String getSimpleName() {
483
            return simpleName;
1 ✔
484
        }
485

486
        @Override
487
        public @NonNull String getBinaryName() {
488
            return binaryName;
1 ✔
489
        }
490

491
        @Override
492
        public @Nullable String getCanonicalName() {
NEW
493
            String outer = owner.getCanonicalName();
×
NEW
494
            return outer == null ? null : outer + '.' + simpleName;
×
495
        }
496

497
        @Override
498
        public int getModifiers() {
499
            return Modifier.PUBLIC | Modifier.STATIC;
1 ✔
500
        }
501

502
        @Override
503
        public @NonNull String getPackageName() {
NEW
504
            return owner.getPackageName();
×
505
        }
506

507
        @Override
508
        public @Nullable JClassSymbol getEnclosingClass() {
509
            return owner;
1 ✔
510
        }
511

512
        @Override
513
        public @Nullable JClassSymbol getSuperclass() {
NEW
514
            return getTypeSystem().OBJECT.getSymbol();
×
515
        }
516

517
        @Override
518
        public @Nullable JClassType getSuperclassType(Substitution substitution) {
NEW
519
            return getTypeSystem().OBJECT;
×
520
        }
521

522
        @Override
523
        public boolean equals(Object o) {
NEW
524
            return SymbolEquality.CLASS.equals(this, o);
×
525
        }
526

527
        @Override
528
        public int hashCode() {
NEW
529
            return SymbolEquality.CLASS.hash(this);
×
530
        }
531

532
        @Override
533
        public String toString() {
NEW
534
            return SymbolToStrings.FAKE.toString(this);
×
535
        }
536
    }
537

538
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc