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 public final class UseCollectionsSingletonListRule
26 extends AbstractJavaRulechainRule {
27
28
29
30
31 private static final InvocationMatcher AS_LIST =
32 InvocationMatcher.parse("java.util.Arrays#asList(_*)");
33
34 public UseCollectionsSingletonListRule() {
35 super(ASTMethodCall.class);
36 }
37
38 @Override
39 public Object visit(final ASTMethodCall call, final Object data) {
40 if (UseCollectionsSingletonListRule.shouldUseSingletonList(call)) {
41 this.asCtx(data).addViolation(call);
42 }
43 return data;
44 }
45
46 private static boolean shouldUseSingletonList(final ASTMethodCall call) {
47 boolean result = false;
48 if (UseCollectionsSingletonListRule.AS_LIST.matchesCall(call)) {
49 final ASTArgumentList args = call.getArguments();
50 result = args.size() == 1
51 && !args.get(0).getTypeMirror().isArray();
52 }
53 return result;
54 }
55 }