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

pmd / pmd / 277

27 Nov 2025 01:37PM UTC coverage: 78.778% (+0.03%) from 78.749%
277

push

github

adangel
[java] UseArraysAsList: skip when if-statements (#6228)

18419 of 24233 branches covered (76.01%)

Branch coverage included in aggregate %.

40090 of 50038 relevant lines covered (80.12%)

0.81 hits per line

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

0.0
/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/internal/JavaDesignerBindings.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.internal;
6

7
import java.util.ArrayList;
8
import java.util.Collection;
9
import java.util.Collections;
10
import java.util.List;
11
import java.util.Set;
12
import java.util.stream.Collectors;
13

14
import org.checkerframework.checker.nullness.qual.NonNull;
15

16
import net.sourceforge.pmd.lang.ast.Node;
17
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
18
import net.sourceforge.pmd.lang.java.ast.ASTAssignableExpr.ASTNamedReferenceExpr;
19
import net.sourceforge.pmd.lang.java.ast.ASTAssignmentExpression;
20
import net.sourceforge.pmd.lang.java.ast.ASTClassType;
21
import net.sourceforge.pmd.lang.java.ast.ASTCompactConstructorDeclaration;
22
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
23
import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess;
24
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
25
import net.sourceforge.pmd.lang.java.ast.ASTInfixExpression;
26
import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression;
27
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
28
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
29
import net.sourceforge.pmd.lang.java.ast.ASTMethodReference;
30
import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType;
31
import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
32
import net.sourceforge.pmd.lang.java.ast.ASTUnaryExpression;
33
import net.sourceforge.pmd.lang.java.ast.ASTVariableAccess;
34
import net.sourceforge.pmd.lang.java.ast.ASTVariableId;
35
import net.sourceforge.pmd.lang.java.ast.InvocationNode;
36
import net.sourceforge.pmd.lang.java.ast.JModifier;
37
import net.sourceforge.pmd.lang.java.ast.JavaNode;
38
import net.sourceforge.pmd.lang.java.ast.JavaVisitorBase;
39
import net.sourceforge.pmd.lang.java.ast.ModifierOwner;
40
import net.sourceforge.pmd.lang.java.ast.TypeNode;
41
import net.sourceforge.pmd.lang.java.symbols.JVariableSymbol;
42
import net.sourceforge.pmd.lang.java.types.JTypeMirror;
43
import net.sourceforge.pmd.lang.rule.xpath.Attribute;
44
import net.sourceforge.pmd.util.designerbindings.DesignerBindings.DefaultDesignerBindings;
45
import net.sourceforge.pmd.util.designerbindings.RelatedNodesSelector;
46

47
public final class JavaDesignerBindings extends DefaultDesignerBindings {
48

49
    public static final JavaDesignerBindings INSTANCE = new JavaDesignerBindings();
×
50

51
    private JavaDesignerBindings() {
52

53
    }
54

55
    @Override
56
    public Attribute getMainAttribute(Node node) {
57
        if (node instanceof JavaNode) {
×
58
            Attribute attr = node.acceptVisitor(MainAttrVisitor.INSTANCE, null);
×
59
            if (attr != null) {
×
60
                return attr;
×
61
            }
62
        }
63

64
        return super.getMainAttribute(node);
×
65
    }
66

67
    @Override
68
    public TreeIconId getIcon(Node node) {
69
        if (node instanceof ASTFieldDeclaration) {
×
70
            return TreeIconId.FIELD;
×
71
        } else if (node instanceof ASTTypeDeclaration) {
×
72
            return TreeIconId.CLASS;
×
73
        } else if (node instanceof ASTMethodDeclaration) {
×
74
            return TreeIconId.METHOD;
×
75
        } else if (node instanceof ASTConstructorDeclaration
×
76
            || node instanceof ASTCompactConstructorDeclaration) {
77
            return TreeIconId.CONSTRUCTOR;
×
78
        } else if (node instanceof ASTVariableId) {
×
79
            return TreeIconId.VARIABLE;
×
80
        }
81
        return super.getIcon(node);
×
82
    }
83

84
    @Override
85
    public Collection<AdditionalInfo> getAdditionalInfo(Node node) {
86
        List<AdditionalInfo> info = new ArrayList<>(super.getAdditionalInfo(node));
×
87
        if (node instanceof ASTLambdaExpression) {
×
88
            ASTLambdaExpression lambda = (ASTLambdaExpression) node;
×
89
            info.add(new AdditionalInfo("Function type: " + lambda.getFunctionalMethod()));
×
90
        }
91
        if (node instanceof ASTMethodReference) {
×
92
            ASTMethodReference lambda = (ASTMethodReference) node;
×
93
            info.add(new AdditionalInfo("Function type: " + lambda.getFunctionalMethod()));
×
94
            info.add(new AdditionalInfo("CTDecl: " + lambda.getReferencedMethod()));
×
95
        }
96
        if (node instanceof InvocationNode) {
×
97
            InvocationNode invoc = (InvocationNode) node;
×
98
            info.add(new AdditionalInfo("Function: " + invoc.getMethodType()));
×
99
            info.add(new AdditionalInfo("VarargsCall: " + invoc.getOverloadSelectionInfo().isVarargsCall()));
×
100
            info.add(new AdditionalInfo("Unchecked: " + invoc.getOverloadSelectionInfo().needsUncheckedConversion()));
×
101
            info.add(new AdditionalInfo("Failed: " + invoc.getOverloadSelectionInfo().isFailed()));
×
102
        }
103
        if (node instanceof TypeNode) {
×
104
            JTypeMirror typeMirror = ((TypeNode) node).getTypeMirror();
×
105
            info.add(new AdditionalInfo("Type: " + typeMirror));
×
106
        }
107
        if (node instanceof ModifierOwner) {
×
108
            String effective = formatModifierSet(((ModifierOwner) node).getModifiers().getEffectiveModifiers());
×
109
            String explicit = formatModifierSet(((ModifierOwner) node).getModifiers().getExplicitModifiers());
×
110
            info.add(new AdditionalInfo("pmd-java:modifiers(): " + effective));
×
111
            info.add(new AdditionalInfo("pmd-java:explicitModifiers(): " + explicit));
×
112
        }
113
        return info;
×
114
    }
115

116
    private @NonNull String formatModifierSet(Set<JModifier> modifierSet) {
117
        return modifierSet.stream().map(JModifier::toString).collect(Collectors.joining(", ", "(", ")"));
×
118
    }
119

120
    @Override
121
    public RelatedNodesSelector getRelatedNodesSelector() {
122
        return n -> {
×
123
            if (n instanceof ASTNamedReferenceExpr) {
×
124
                JVariableSymbol sym = ((ASTNamedReferenceExpr) n).getReferencedSym();
×
125
                if (sym != null && sym.tryGetNode() != null) {
×
126
                    return Collections.unmodifiableList(sym.tryGetNode().getLocalUsages());
×
127
                }
128
            }
129

130
            return Collections.emptyList();
×
131

132
        };
133
    }
134

135
    private static final class MainAttrVisitor extends JavaVisitorBase<Void, Attribute> {
136

137
        private static final MainAttrVisitor INSTANCE = new MainAttrVisitor();
×
138

139
        @Override
140
        public Attribute visitJavaNode(JavaNode node, Void data) {
141
            return null; // don't recurse
×
142
        }
143

144
        @Override
145
        public Attribute visit(ASTInfixExpression node, Void data) {
146
            return new Attribute(node, "Operator", node.getOperator().toString());
×
147
        }
148

149
        @Override
150
        public Attribute visitTypeDecl(ASTTypeDeclaration node, Void data) {
151
            return new Attribute(node, "SimpleName", node.getSimpleName());
×
152
        }
153

154
        @Override
155
        public Attribute visit(ASTAnnotation node, Void data) {
156
            return new Attribute(node, "SimpleName", node.getSimpleName());
×
157
        }
158

159
        @Override
160
        public Attribute visit(ASTClassType node, Void data) {
161
            return new Attribute(node, "SimpleName", node.getSimpleName());
×
162
        }
163

164
        @Override
165
        public Attribute visit(ASTPrimitiveType node, Void data) {
166
            return new Attribute(node, "Kind", node.getKind().getSimpleName());
×
167
        }
168

169
        @Override
170
        public Attribute visit(ASTMethodCall node, Void data) {
171
            return new Attribute(node, "MethodName", node.getMethodName());
×
172
        }
173

174
        @Override
175
        public Attribute visit(ASTMethodReference node, Void data) {
176
            return new Attribute(node, "MethodName", node.getMethodName());
×
177
        }
178

179
        @Override
180
        public Attribute visit(ASTFieldAccess node, Void data) {
181
            return new Attribute(node, "Name", node.getName());
×
182
        }
183

184
        @Override
185
        public Attribute visit(ASTVariableAccess node, Void data) {
186
            return new Attribute(node, "Name", node.getName());
×
187
        }
188

189

190
        @Override
191
        public Attribute visit(ASTMethodDeclaration node, Void data) {
192
            return new Attribute(node, "Name", node.getName());
×
193
        }
194

195
        @Override
196
        public Attribute visit(ASTVariableId node, Void data) {
197
            return new Attribute(node, "Name", node.getName());
×
198
        }
199

200
        @Override
201
        public Attribute visit(ASTAssignmentExpression node, Void data) {
202
            return new Attribute(node, "Operator", node.getOperator().getToken());
×
203
        }
204

205
        @Override
206
        public Attribute visit(ASTUnaryExpression node, Void data) {
207
            return new Attribute(node, "Operator", node.getOperator().getToken());
×
208
        }
209
    }
210
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc