/
UnnecessaryVarargsArrayCreationRule.java
53 lines (43 loc) · 2.01 KB
/
UnnecessaryVarargsArrayCreationRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import java.util.List;
import net.sourceforge.pmd.lang.java.ast.ASTArgumentList;
import net.sourceforge.pmd.lang.java.ast.ASTArrayAllocation;
import net.sourceforge.pmd.lang.java.ast.InvocationNode;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.types.JTypeMirror;
import net.sourceforge.pmd.lang.java.types.OverloadSelectionResult;
public class UnnecessaryVarargsArrayCreationRule extends AbstractJavaRulechainRule {
// we visit array allocations because they are less frequent than
// method calls
public UnnecessaryVarargsArrayCreationRule() {
super(ASTArrayAllocation.class);
}
@Override
public Object visit(ASTArrayAllocation array, Object data) {
if (array.getArrayInitializer() == null) {
return null;
}
JavaNode parent = array.getParent();
if (parent instanceof ASTArgumentList && array.getIndexInParent() == parent.getNumChildren() - 1) {
// node is the last param in an arguments list
InvocationNode call = (InvocationNode) parent.getParent();
OverloadSelectionResult info = call.getOverloadSelectionInfo();
if (info.isFailed() || info.isVarargsCall()
|| !info.getMethodType().isVarargs()) {
return null;
}
List<JTypeMirror> formals = info.getMethodType().getFormalParameters();
JTypeMirror lastFormal = formals.get(formals.size() - 1);
if (array.getTypeMirror().equals(lastFormal)) {
// If type not equal, then it would not actually be equivalent to remove the array creation.
// That case may be caught by ConfusingArgumentToVarargsMethod
asCtx(data).addViolation(array);
}
}
return null;
}
}