|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.rules; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.AbstractRule; |
|
7 |
| import net.sourceforge.pmd.Rule; |
|
8 |
| import net.sourceforge.pmd.ast.ASTAllocationExpression; |
|
9 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceType; |
|
10 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
|
11 |
| import net.sourceforge.pmd.ast.ASTPrimarySuffix; |
|
12 |
| import net.sourceforge.pmd.ast.SimpleNode; |
|
13 |
| |
|
14 |
| import java.util.HashSet; |
|
15 |
| import java.util.Set; |
|
16 |
| |
|
17 |
| public class UnnecessaryConversionTemporary extends AbstractRule implements Rule { |
|
18 |
| |
|
19 |
| private boolean inPrimaryExpressionContext; |
|
20 |
| private boolean usingPrimitiveWrapperAllocation; |
|
21 |
| private Set primitiveWrappers = new HashSet(); |
|
22 |
| |
|
23 |
36
| public UnnecessaryConversionTemporary() {
|
|
24 |
36
| primitiveWrappers.add("Integer");
|
|
25 |
36
| primitiveWrappers.add("Boolean");
|
|
26 |
36
| primitiveWrappers.add("Double");
|
|
27 |
36
| primitiveWrappers.add("Long");
|
|
28 |
36
| primitiveWrappers.add("Short");
|
|
29 |
36
| primitiveWrappers.add("Byte");
|
|
30 |
36
| primitiveWrappers.add("Float");
|
|
31 |
| } |
|
32 |
| |
|
33 |
12
| public Object visit(ASTPrimaryExpression node, Object data) {
|
|
34 |
12
| if (node.jjtGetNumChildren() == 0 || (node.jjtGetChild(0)).jjtGetNumChildren() == 0 || !(node.jjtGetChild(0).jjtGetChild(0) instanceof ASTAllocationExpression)) {
|
|
35 |
6
| return super.visit(node, data);
|
|
36 |
| } |
|
37 |
| |
|
38 |
6
| inPrimaryExpressionContext = true;
|
|
39 |
6
| super.visit(node, data);
|
|
40 |
6
| inPrimaryExpressionContext = false;
|
|
41 |
6
| usingPrimitiveWrapperAllocation = false;
|
|
42 |
6
| return data;
|
|
43 |
| } |
|
44 |
| |
|
45 |
6
| public Object visit(ASTAllocationExpression node, Object data) {
|
|
46 |
6
| if (!inPrimaryExpressionContext || !(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
|
|
47 |
0
| return super.visit(node, data);
|
|
48 |
| } |
|
49 |
6
| if (!primitiveWrappers.contains(((SimpleNode) node.jjtGetChild(0)).getImage())) {
|
|
50 |
0
| return super.visit(node, data);
|
|
51 |
| } |
|
52 |
6
| usingPrimitiveWrapperAllocation = true;
|
|
53 |
6
| return super.visit(node, data);
|
|
54 |
| } |
|
55 |
| |
|
56 |
12
| public Object visit(ASTPrimarySuffix node, Object data) {
|
|
57 |
12
| if (!inPrimaryExpressionContext || !usingPrimitiveWrapperAllocation) {
|
|
58 |
0
| return super.visit(node, data);
|
|
59 |
| } |
|
60 |
12
| if (node.getImage() != null && node.getImage().equals("toString")) {
|
|
61 |
6
| addViolation(data, node);
|
|
62 |
| } |
|
63 |
12
| return super.visit(node, data);
|
|
64 |
| } |
|
65 |
| |
|
66 |
| } |