1
2
3
4
5 package com.qulice.pmd.rules;
6
7 import net.sourceforge.pmd.lang.java.ast.ASTArgumentList;
8 import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
9 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
10 import net.sourceforge.pmd.lang.java.types.InvocationMatcher;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public final class UseCollectionsSingletonListRule
27 extends AbstractJavaRulechainRule {
28
29
30
31
32 private static final InvocationMatcher AS_LIST =
33 InvocationMatcher.parse("java.util.Arrays#asList(_*)");
34
35
36
37
38 public UseCollectionsSingletonListRule() {
39 super(ASTMethodCall.class);
40 }
41
42 @Override
43 public Object visit(final ASTMethodCall call, final Object data) {
44 if (UseCollectionsSingletonListRule.shouldUseSingletonList(call)) {
45 this.asCtx(data).addViolation(call);
46 }
47 return data;
48 }
49
50 private static boolean shouldUseSingletonList(final ASTMethodCall call) {
51 boolean result = false;
52 if (UseCollectionsSingletonListRule.AS_LIST.matchesCall(call)) {
53 final ASTArgumentList args = call.getArguments();
54 result = args.size() == 1
55 && !args.get(0).getTypeMirror().isArray();
56 }
57 return result;
58 }
59 }