• 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

92.04
/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/ast/AstClassSym.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.ast;
6

7
import static net.sourceforge.pmd.util.CollectionUtil.setOf;
8

9
import java.lang.reflect.Modifier;
10
import java.util.ArrayList;
11
import java.util.Collections;
12
import java.util.List;
13
import java.util.Objects;
14

15
import org.checkerframework.checker.nullness.qual.NonNull;
16
import org.checkerframework.checker.nullness.qual.Nullable;
17
import org.pcollections.HashTreePSet;
18
import org.pcollections.PSet;
19

20
import net.sourceforge.pmd.lang.ast.NodeStream;
21
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
22
import net.sourceforge.pmd.lang.java.ast.ASTAssignableExpr.ASTNamedReferenceExpr;
23
import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration;
24
import net.sourceforge.pmd.lang.java.ast.ASTClassDeclaration;
25
import net.sourceforge.pmd.lang.java.ast.ASTClassType;
26
import net.sourceforge.pmd.lang.java.ast.ASTConstructorCall;
27
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
28
import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant;
29
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
30
import net.sourceforge.pmd.lang.java.ast.ASTMemberValue;
31
import net.sourceforge.pmd.lang.java.ast.ASTMemberValuePair;
32
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
33
import net.sourceforge.pmd.lang.java.ast.ASTPermitsList;
34
import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType;
35
import net.sourceforge.pmd.lang.java.ast.ASTRecordComponent;
36
import net.sourceforge.pmd.lang.java.ast.ASTRecordComponentList;
37
import net.sourceforge.pmd.lang.java.ast.ASTType;
38
import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
39
import net.sourceforge.pmd.lang.java.ast.ASTVariableId;
40
import net.sourceforge.pmd.lang.java.ast.ASTVoidType;
41
import net.sourceforge.pmd.lang.java.ast.Annotatable;
42
import net.sourceforge.pmd.lang.java.ast.InternalApiBridge;
43
import net.sourceforge.pmd.lang.java.ast.JModifier;
44
import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils;
45
import net.sourceforge.pmd.lang.java.internal.JavaAstProcessor;
46
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
47
import net.sourceforge.pmd.lang.java.symbols.JConstructorSymbol;
48
import net.sourceforge.pmd.lang.java.symbols.JElementSymbol;
49
import net.sourceforge.pmd.lang.java.symbols.JExecutableSymbol;
50
import net.sourceforge.pmd.lang.java.symbols.JFieldSymbol;
51
import net.sourceforge.pmd.lang.java.symbols.JMethodSymbol;
52
import net.sourceforge.pmd.lang.java.symbols.JRecordComponentSymbol;
53
import net.sourceforge.pmd.lang.java.symbols.JTypeDeclSymbol;
54
import net.sourceforge.pmd.lang.java.symbols.JTypeParameterOwnerSymbol;
55
import net.sourceforge.pmd.lang.java.symbols.internal.ImplicitMemberSymbols;
56
import net.sourceforge.pmd.lang.java.types.JClassType;
57
import net.sourceforge.pmd.lang.java.types.JTypeMirror;
58
import net.sourceforge.pmd.lang.java.types.Substitution;
59
import net.sourceforge.pmd.lang.java.types.TypeOps;
60
import net.sourceforge.pmd.lang.java.types.TypeSystem;
61
import net.sourceforge.pmd.util.CollectionUtil;
62

63

64
final class AstClassSym
1 ✔
65
    extends AbstractAstTParamOwner<ASTTypeDeclaration>
66
    implements JClassSymbol {
67

68
    private static final String LOMBOK_BUILDER = "lombok.Builder";
69

70
    private final @Nullable JTypeParameterOwnerSymbol enclosing;
71
    private final List<JClassSymbol> declaredClasses;
72
    private final List<JMethodSymbol> declaredMethods;
73
    private final List<JConstructorSymbol> declaredCtors;
74
    private final List<JFieldSymbol> declaredFields;
75
    private final List<JFieldSymbol> enumConstants; // subset of declaredFields
76
    private final List<JRecordComponentSymbol> recordComponents;
77
    private final PSet<String> annotAttributes;
78

79
    private List<JClassSymbol> permittedSubclasses;
80

81
    AstClassSym(ASTTypeDeclaration node,
82
                AstSymFactory factory,
83
                @Nullable JTypeParameterOwnerSymbol enclosing) {
84
        super(node, factory);
1 ✔
85
        this.enclosing = enclosing;
1 ✔
86

87
        // evaluate everything strictly
88
        // this populates symbols on the relevant AST nodes
89

90
        final List<JClassSymbol> myClasses = new ArrayList<>();
1 ✔
91
        final List<JMethodSymbol> myMethods = new ArrayList<>();
1 ✔
92
        final List<JConstructorSymbol> myCtors = new ArrayList<>();
1 ✔
93
        final List<JFieldSymbol> myFields = new ArrayList<>();
1 ✔
94
        final List<JFieldSymbol> enumConstants;
95
        final List<JRecordComponentSymbol> recordComponents;
96

97
        if (isRecord()) {
1 ✔
98
            ASTRecordComponentList components = Objects.requireNonNull(node.getRecordComponents(),
1 ✔
99
                                                                       "Null component list for " + node);
100
            recordComponents = mapComponentsToMutableList(factory, components, myFields);
1 ✔
101

102
            JConstructorSymbol canonicalRecordCtor = ImplicitMemberSymbols.recordConstructor(this, recordComponents, components.isVarargs());
1 ✔
103
            myCtors.add(canonicalRecordCtor);
1 ✔
104
            InternalApiBridge.setSymbol(components, canonicalRecordCtor);
1 ✔
105

106
        } else {
1 ✔
107
            recordComponents = Collections.emptyList();
1 ✔
108
        }
109

110
        if (isEnum()) {
1 ✔
111
            enumConstants = new ArrayList<>();
1 ✔
112
            node.getEnumConstants()
1 ✔
113
                .forEach(constant -> {
1 ✔
114
                    AstFieldSym fieldSym = new AstFieldSym(constant.getVarId(), factory, this);
1 ✔
115
                    enumConstants.add(fieldSym);
1 ✔
116
                    myFields.add(fieldSym);
1 ✔
117
                });
1 ✔
118
        } else {
119
            enumConstants = null;
1 ✔
120
        }
121

122
        for (ASTBodyDeclaration dnode : node.getDeclarations()) {
1 ✔
123

124
            if (dnode instanceof ASTTypeDeclaration) {
1 ✔
125
                myClasses.add(new AstClassSym((ASTTypeDeclaration) dnode, factory, this));
1 ✔
126
            } else if (dnode instanceof ASTMethodDeclaration) {
1 ✔
127
                if (!recordComponents.isEmpty() && ((ASTMethodDeclaration) dnode).getArity() == 0) {
1 ✔
128
                    // filter out record component, so that the accessor is not generated
129
                    recordComponents.removeIf(f -> f.nameEquals(((ASTMethodDeclaration) dnode).getName()));
1 ✔
130
                }
131
                myMethods.add(new AstMethodSym((ASTMethodDeclaration) dnode, factory, this));
1 ✔
132
            } else if (dnode instanceof ASTConstructorDeclaration) {
1 ✔
133
                myCtors.add(new AstCtorSym((ASTConstructorDeclaration) dnode, factory, this));
1 ✔
134
            } else if (dnode instanceof ASTFieldDeclaration) {
1 ✔
135
                for (ASTVariableId varId : ((ASTFieldDeclaration) dnode).getVarIds()) {
1 ✔
136
                    myFields.add(new AstFieldSym(varId, factory, this));
1 ✔
137
                }
1 ✔
138
            }
139
        }
1 ✔
140
        
141

142
        if (!recordComponents.isEmpty()) {
1 ✔
143
            // then the recordsComponents contains all record components
144
            // for which we must synthesize an accessor (explicitly declared
145
            // accessors have been filtered out)
146
            for (JRecordComponentSymbol component : recordComponents) {
1 ✔
147
                myMethods.add(ImplicitMemberSymbols.recordAccessor(this, component));
1 ✔
148
            }
1 ✔
149
        }
150

151
        if (myCtors.isEmpty() && isClass() && !isAnonymousClass()) {
1 ✔
152
            myCtors.add(ImplicitMemberSymbols.defaultCtor(this));
1 ✔
153
        }
154

155
        if (this.isEnum()) {
1 ✔
156
            myMethods.add(ImplicitMemberSymbols.enumValues(this));
1 ✔
157
            myMethods.add(ImplicitMemberSymbols.enumValueOf(this));
1 ✔
158
        }
159

160

161
        this.declaredClasses = myClasses;
1 ✔
162
        this.declaredMethods = myMethods;
1 ✔
163
        this.declaredCtors = myCtors;
1 ✔
164
        this.declaredFields = myFields;
1 ✔
165
        this.enumConstants = CollectionUtil.makeUnmodifiableAndNonNull(enumConstants);
1 ✔
166
        this.recordComponents = CollectionUtil.makeUnmodifiableAndNonNull(recordComponents);
1 ✔
167
        this.annotAttributes = isAnnotation()
1 ✔
168
                               ? getDeclaredMethods().stream().filter(JMethodSymbol::isAnnotationAttribute).map(JElementSymbol::getSimpleName).collect(CollectionUtil.toPersistentSet())
1 ✔
169
                               : HashTreePSet.empty();
1 ✔
170
    }
1 ✔
171

172
    /**
173
     * @return builder classes added by this call, in declaration order
174
     */
175
    public List<JClassSymbol> processLombok(JavaAstProcessor processor) {
176
        final String LOMBOK_SLF4J = "lombok.extern.slf4j.Slf4j";
1 ✔
177
        final String LOMBOK_GETTER = "lombok.Getter";
1 ✔
178
        final String LOMBOK_DATA = "lombok.Data";
1 ✔
179
        final String LOMBOK_VALUE = "lombok.Value";
1 ✔
180

181
        if (node.isAnnotationPresent(LOMBOK_SLF4J)) {
1 ✔
182
            declaredFields.add(ImplicitMemberSymbols.lombokSlf4jLoggerField(this, processor));
1 ✔
183
        }
184

185
        declaredFields.stream()
1 ✔
186
                .filter(f -> !f.isStatic())
1 ✔
187
                .map(f -> f.tryGetNode())
1 ✔
188
                .filter(Objects::nonNull)
1 ✔
189
                .filter(varId -> !varId.isRecordComponent())
1 ✔
190
                .forEach(varId -> {
1 ✔
191
                    ASTAnnotation fieldGetter = varId.getAnnotation(LOMBOK_GETTER);
1 ✔
192
                    int accessModifier = -1;
1 ✔
193

194
                    if (fieldGetter != null) {
1 ✔
195
                        accessModifier = getLombokAccessModifier(fieldGetter);
1 ✔
196
                    } else {
197
                        ASTAnnotation classGetter = node.getAnnotation(LOMBOK_GETTER);
1 ✔
198
                        if (classGetter != null) {
1 ✔
199
                            accessModifier = getLombokAccessModifier(classGetter);
1 ✔
200
                        } else if (JavaAstUtils.hasAnyAnnotation(node, setOf(LOMBOK_DATA, LOMBOK_VALUE))) {
1 ✔
201
                            accessModifier = Modifier.PUBLIC;
1 ✔
202
                        }
203
                    }
204

205
                    if (accessModifier != -1) {
1 ✔
206
                        JMethodSymbol getter = ImplicitMemberSymbols.lombokGetter(this,
1 ✔
207
                                (JFieldSymbol) varId.getSymbol(), accessModifier);
1 ✔
208
                        String getterName = getter.getSimpleName();
1 ✔
209
                        if (declaredMethods.stream().noneMatch(m -> m.getSimpleName().equals(getterName) && m.getArity() == 0)) {
1 !
210
                            declaredMethods.add(getter);
1 ✔
211
                        }
212
                    }
213
                });
1 ✔
214

215
        return addLombokBuilders();
1 ✔
216
    }
217

218
    /**
219
     * Lombok {@code @Builder} adds a static nested class named {@code <TypeName>Builder},
220
     * or the {@code builderClassName} attribute when that is set ({@code *} is the type name).
221
     * On a method, that name is the return type's simple name; otherwise it is the enclosing type.
222
     * An existing member class of that name is left in place.
223
     *
224
     * @return the synthetic builder classes added by this call, in declaration order
225
     */
226
    private List<JClassSymbol> addLombokBuilders() {
227
        List<JClassSymbol> added = new ArrayList<>();
1 ✔
228
        if (isInterface() || isAnonymousClass() || node.getSimpleName().isEmpty()) {
1 ✔
229
            return added;
1 ✔
230
        }
231
        addLombokBuilder(node, added);
1 ✔
232
        node.getOperations().forEach(op -> addLombokBuilder(op, added));
1 ✔
233
        return added;
1 ✔
234
    }
235

236
    private void addLombokBuilder(Annotatable annotated, List<JClassSymbol> added) {
237
        ASTAnnotation builder = annotated.getAnnotation(LOMBOK_BUILDER);
1 ✔
238
        if (builder == null) {
1 ✔
239
            return;
1 ✔
240
        }
241
        String builderName = lombokBuilderClassName(builder, lombokBuilderTypeName(annotated));
1 ✔
242
        if (declaredClasses.stream().anyMatch(c -> c.nameEquals(builderName))) {
1 ✔
243
            return;
1 ✔
244
        }
245
        JClassSymbol generated = ImplicitMemberSymbols.lombokBuilderClass(this, builderName);
1 ✔
246
        declaredClasses.add(generated);
1 ✔
247
        added.add(generated);
1 ✔
248
    }
1 ✔
249

250
    /**
251
     * Name substituted for {@code *} in {@code builderClassName}. Lombok uses the
252
     * enclosing type, except on methods, where it uses the return type
253
     * ({@code static Foo create()} in {@code Bar} generates {@code Bar$FooBuilder}).
254
     */
255
    private String lombokBuilderTypeName(Annotatable annotated) {
256
        if (annotated instanceof ASTMethodDeclaration) {
1 ✔
257
            String returnTypeName = simpleNameForBuilder(((ASTMethodDeclaration) annotated).getResultTypeNode());
1 ✔
258
            if (returnTypeName != null) {
1 !
259
                return returnTypeName;
1 ✔
260
            }
261
        }
262
        return node.getSimpleName();
1 ✔
263
    }
264

265
    /**
266
     * Simple name Lombok uses for a method return type. Primitives are capitalized
267
     * ({@code int} becomes {@code Int}), matching {@code returnTypeToBuilderClassName}.
268
     * Arrays and other shapes return null so the caller falls back to the enclosing type.
269
     */
270
    private static @Nullable String simpleNameForBuilder(ASTType resultType) {
271
        if (resultType instanceof ASTClassType) {
1 !
272
            return ((ASTClassType) resultType).getSimpleName();
1 ✔
273
        }
NEW
274
        if (resultType instanceof ASTPrimitiveType) {
×
NEW
275
            String name = ((ASTPrimitiveType) resultType).getKind().getSimpleName();
×
NEW
276
            return Character.toTitleCase(name.charAt(0)) + name.substring(1);
×
277
        }
NEW
278
        if (resultType instanceof ASTVoidType) {
×
NEW
279
            return "Void";
×
280
        }
NEW
281
        return null;
×
282
    }
283

284
    private static String lombokBuilderClassName(ASTAnnotation annotation, String typeName) {
285
        ASTMemberValue configured = annotation.getAttribute("builderClassName");
1 ✔
286
        Object value = configured == null ? null : configured.getConstValue();
1 ✔
287
        if (!(value instanceof String) || ((String) value).isEmpty()) {
1 !
288
            return typeName + "Builder";
1 ✔
289
        }
290
        return ((String) value).replace("*", typeName);
1 ✔
291
    }
292

293
    private int getLombokAccessModifier(ASTAnnotation annot) {
294
        return annot.getFlatValue(ASTMemberValuePair.VALUE_ATTR)
1 ✔
295
                .filter(v -> v instanceof ASTNamedReferenceExpr)
1 ✔
296
                .toStream()
1 ✔
297
                .map(ASTNamedReferenceExpr.class::cast)
1 ✔
298
                .map(ASTNamedReferenceExpr::getName)
1 ✔
299
                .findFirst()
1 ✔
300
                .map(accessLevel -> {
1 ✔
301
                    switch (accessLevel) {
1 !
302
                    case "PROTECTED":
303
                        return Modifier.PROTECTED;
×
304
                    case "PACKAGE":
305
                        return 0;
×
306
                    case "PRIVATE":
307
                        return Modifier.PRIVATE;
1 ✔
308
                    case "NONE":
309
                        return -1; // special value for NONE, we'll use -1 as a flag or handle it
1 ✔
310
                    case "PUBLIC":
311
                    default:
312
                        return Modifier.PUBLIC;
1 ✔
313
                    }
314
                })
315
                .orElse(Modifier.PUBLIC); // lombok generates by default public getters
1 ✔
316
    }
317

318

319
    private List<JRecordComponentSymbol> mapComponentsToMutableList(AstSymFactory factory,
320
                                                          ASTRecordComponentList components,
321
                                                          List<JFieldSymbol> fieldSyms) {
322
        List<JRecordComponentSymbol> list = new ArrayList<>();
1 ✔
323
        for (ASTRecordComponent comp : components) {
1 ✔
324
            list.add(new AstRecordComponentSym(comp, factory, this));
1 ✔
325
            fieldSyms.add(new AstFieldSym(comp.getVarId(), factory, this));
1 ✔
326
        }
1 ✔
327
        return list;
1 ✔
328
    }
329

330
    @Override
331
    public @NonNull String getSimpleName() {
332
        return node.getSimpleName();
1 ✔
333
    }
334

335

336
    @Override
337
    public @NonNull String getBinaryName() {
338
        return node.getBinaryName();
1 ✔
339
    }
340

341
    @Override
342
    public @Nullable String getCanonicalName() {
343
        return node.getCanonicalName();
1 ✔
344
    }
345

346
    @Override
347
    public boolean isUnresolved() {
348
        return false;
1 ✔
349
    }
350

351
    @Override
352
    public @Nullable JClassSymbol getEnclosingClass() {
353
        if (enclosing instanceof JClassSymbol) {
1 ✔
354
            return (JClassSymbol) enclosing;
1 ✔
355
        } else if (enclosing instanceof JExecutableSymbol) {
1 ✔
356
            return enclosing.getEnclosingClass();
1 ✔
357
        }
358
        assert enclosing == null;
1 !
359
        return null;
1 ✔
360
    }
361

362
    @Override
363
    public @Nullable JExecutableSymbol getEnclosingMethod() {
364
        return enclosing instanceof JExecutableSymbol ? (JExecutableSymbol) enclosing : null;
1 ✔
365
    }
366

367
    @Override
368
    public List<JClassSymbol> getDeclaredClasses() {
369
        return Collections.unmodifiableList(declaredClasses);
1 ✔
370
    }
371

372
    @Override
373
    public List<JMethodSymbol> getDeclaredMethods() {
374
        return Collections.unmodifiableList(declaredMethods);
1 ✔
375
    }
376

377
    @Override
378
    public List<JConstructorSymbol> getConstructors() {
379
        return Collections.unmodifiableList(declaredCtors);
1 ✔
380
    }
381

382
    @Override
383
    public List<JFieldSymbol> getDeclaredFields() {
384
        return Collections.unmodifiableList(declaredFields);
1 ✔
385
    }
386

387
    @Override
388
    public @NonNull List<JFieldSymbol> getEnumConstants() {
389
        return enumConstants;
1 ✔
390
    }
391

392
    @Override
393
    public @NonNull List<JRecordComponentSymbol> getRecordComponents() {
394
        return recordComponents;
1 ✔
395
    }
396

397

398
    @Override
399
    public List<JClassSymbol> getPermittedSubtypes() {
400
        // permitted subclasses are populated lazily because they require
401
        // symbol and type resolution to determine which types are sealed.
402
        if (permittedSubclasses == null) {
1 ✔
403
            ASTPermitsList permits = node.getPermitsClause();
1 ✔
404
            if (permits != null) {
1 ✔
405
                this.permittedSubclasses = permits.toList().stream().map(it -> {
1 ✔
406
                    JTypeDeclSymbol symbol = it.getTypeMirror().getSymbol();
1 ✔
407
                    if (symbol instanceof JClassSymbol) {
1 !
408
                        return (JClassSymbol) symbol;
1 ✔
409
                    } else {
410
                        return null;
×
411
                    }
412
                }).filter(Objects::nonNull).collect(CollectionUtil.toUnmodifiableList());
1 ✔
413
            } else if (isSealed()) {
1 ✔
414
                // sealed with no permits clause: infer permitted
415
                this.permittedSubclasses = inferPermittedSubclasses();
1 ✔
416
            } else {
417
                this.permittedSubclasses = Collections.emptyList();
1 ✔
418
            }
419
        }
420
        return permittedSubclasses;
1 ✔
421
    }
422

423
    private List<JClassSymbol> inferPermittedSubclasses() {
424
        /*
425
         *  If the declaration of a sealed class C lacks a permits clause,
426
         * then the permitted direct subclasses of C are as follows:
427
         *
428
         *  1. If C is not an enum class, then its permitted direct subclasses
429
         *     are those classes declared in the same compilation unit as C (§7.3)
430
         *     which have a canonical name (§6.7) and whose direct superclass is C.
431
         *
432
         *     That is, the permitted direct subclasses are inferred as the classes
433
         *     in the same compilation unit that specify C as their direct superclass.
434
         *     The requirement for a canonical name means that no local classes or
435
         *     anonymous classes will be considered.
436
         *
437
         *     It is a compile-time error if the declaration of a sealed class C lacks
438
         *     a permits clause and C has no permitted direct subclasses.
439
         *
440
         *  2. If C is an enum class, then its permitted direct subclasses, if any,
441
         *     are specified in §8.9.
442
         */
443
        if (!isEnum()) {
1 !
444
            boolean isInterface = isInterface();
1 ✔
445
            List<JClassSymbol> list = node
1 ✔
446
                .getRoot().descendants(ASTTypeDeclaration.class).crossFindBoundaries()
1 ✔
447
                .filter(it -> it.getCanonicalName() != null)
1 ✔
448
                .filter(it -> {
1 ✔
449
                    if (isInterface) {
1 ✔
450
                        return it.getSuperInterfaceTypeNodes().any(ty -> Objects.equals(ty.getTypeMirror().getSymbol(), this));
1 ✔
451
                    }
452
                    return NodeStream.of(it.getSuperClassTypeNode()).any(ty -> Objects.equals(ty.getTypeMirror().getSymbol(), this));
1 ✔
453
                }).toList(ASTTypeDeclaration::getSymbol);
1 ✔
454
            return Collections.unmodifiableList(list);
1 ✔
455
        }
456
        return Collections.emptyList();
×
457
    }
458

459
    @Override
460
    public boolean isSealed() {
461
        return node.hasModifiers(JModifier.SEALED);
1 ✔
462
    }
463

464
    @Override
465
    public @Nullable JClassType getSuperclassType(Substitution substitution) {
466
        TypeSystem ts = getTypeSystem();
1 ✔
467

468
        if (node.isEnum()) {
1 ✔
469

470
            return factory.enumSuperclass(this);
1 ✔
471

472
        } else if (node instanceof ASTClassDeclaration) {
1 ✔
473

474
            ASTClassType superClass = node.getSuperClassTypeNode();
1 ✔
475
            return superClass == null
1 ✔
476
                   ? ts.OBJECT
1 ✔
477
                   // this cast relies on the fact that the superclass is not a type variable
478
                   : (JClassType) TypeOps.subst(superClass.getTypeMirror(), substitution);
1 ✔
479

480
        } else if (isAnonymousClass()) {
1 ✔
481

482
            if (node.getParent() instanceof ASTEnumConstant) {
1 ✔
483

484
                return node.getEnclosingType().getTypeMirror().subst(substitution);
1 ✔
485

486
            } else if (node.getParent() instanceof ASTConstructorCall) {
1 !
487

488
                @NonNull JTypeMirror sym = ((ASTConstructorCall) node.getParent()).getTypeMirror();
1 ✔
489

490
                return sym instanceof JClassType && !sym.isInterface()
1 !
491
                       ? (JClassType) sym
1 ✔
492
                       : factory.types().OBJECT;
1 ✔
493
            }
494

495
        } else if (isRecord()) {
1 ✔
496

497
            return factory.recordSuperclass();
1 ✔
498

499
        } else if (isAnnotation()) {
1 ✔
500

501
            return ts.OBJECT;
1 ✔
502

503
        }
504

505
        return null;
1 ✔
506
    }
507

508
    @Override
509
    public @Nullable JClassSymbol getSuperclass() {
510
        // notice this relies on the fact that the extends clause
511
        // (or the type node of the constructor call, for an anonymous class),
512
        // was disambiguated early
513

514
        // We special case anonymous classes so as not to trigger overload resolution
515
        if (isAnonymousClass() && node.getParent() instanceof ASTConstructorCall) {
1 ✔
516

517
            @NonNull JTypeMirror sym = ((ASTConstructorCall) node.getParent()).getTypeNode().getTypeMirror();
1 ✔
518

519
            return sym instanceof JClassType && !sym.isInterface()
1 !
520
                   ? ((JClassType) sym).getSymbol()
1 ✔
521
                   : factory.types().OBJECT.getSymbol();
1 ✔
522

523
        }
524

525
        JClassType sup = getSuperclassType(Substitution.EMPTY);
1 ✔
526
        return sup == null ? null : sup.getSymbol();
1 !
527
    }
528

529
    @Override
530
    public List<JClassSymbol> getSuperInterfaces() {
531
        List<JClassSymbol> itfs = CollectionUtil.mapNotNull(
1 ✔
532
            node.getSuperInterfaceTypeNodes(),
1 ✔
533
            n -> {
534
                // we play safe here, but the symbol is either a JClassSymbol
535
                // or a JTypeParameterSymbol, with the latter case being a
536
                // compile-time error
537
                JTypeDeclSymbol sym = n.getTypeMirror().getSymbol();
1 ✔
538
                return sym instanceof JClassSymbol ? (JClassSymbol) sym : null;
1 !
539
            }
540
        );
541
        if (isAnnotation()) {
1 ✔
542
            itfs = CollectionUtil.concatView(Collections.singletonList(factory.annotationSym()), itfs);
1 ✔
543
        }
544
        return itfs;
1 ✔
545
    }
546

547
    @Override
548
    public List<JClassType> getSuperInterfaceTypes(Substitution subst) {
549
        List<JClassType> itfs = CollectionUtil.map(node.getSuperInterfaceTypeNodes(), n -> (JClassType) TypeOps.subst(n.getTypeMirror(), subst));
1 ✔
550
        if (isAnnotation()) {
1 ✔
551
            itfs = CollectionUtil.concatView(Collections.singletonList(factory.annotationType()), itfs);
1 ✔
552
        }
553
        return itfs;
1 ✔
554
    }
555

556
    @Override
557
    public @Nullable JTypeDeclSymbol getArrayComponent() {
558
        return null;
×
559
    }
560

561
    @Override
562
    public boolean isArray() {
563
        return false;
1 ✔
564
    }
565

566
    @Override
567
    public boolean isPrimitive() {
568
        return false;
1 ✔
569
    }
570

571
    @Override
572
    public boolean isInterface() {
573
        return node.isInterface();
1 ✔
574
    }
575

576
    @Override
577
    public boolean isEnum() {
578
        return node.isEnum();
1 ✔
579
    }
580

581
    @Override
582
    public boolean isRecord() {
583
        return node.isRecord();
1 ✔
584
    }
585

586
    @Override
587
    public boolean isAnnotation() {
588
        return node.isAnnotation();
1 ✔
589
    }
590

591
    @Override
592
    public boolean isLocalClass() {
593
        return node.isLocal();
1 ✔
594
    }
595

596
    @Override
597
    public boolean isAnonymousClass() {
598
        return node.isAnonymous();
1 ✔
599
    }
600

601
    @Override
602
    public PSet<String> getAnnotationAttributeNames() {
603
        return annotAttributes;
×
604
    }
605
}
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