|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.rules.optimization; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
|
7 |
| import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration; |
|
8 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
|
9 |
| |
|
10 |
| public class LocalVariableCouldBeFinal extends AbstractOptimizationRule { |
|
11 |
| |
|
12 |
9
| public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
|
13 |
9
| if (node.isInterface()) {
|
|
14 |
0
| return data;
|
|
15 |
| } |
|
16 |
9
| return super.visit(node, data);
|
|
17 |
| } |
|
18 |
| |
|
19 |
12
| public Object visit(ASTLocalVariableDeclaration node, Object data) {
|
|
20 |
12
| if (node.isFinal()) {
|
|
21 |
1
| return data;
|
|
22 |
| } |
|
23 |
11
| final String varName = getVarName(node);
|
|
24 |
| |
|
25 |
11
| ASTMethodDeclaration md = (ASTMethodDeclaration) node.getFirstParentOfType(ASTMethodDeclaration.class);
|
|
26 |
11
| if (md != null) {
|
|
27 |
11
| if (!isVarWritterInMethod(varName, md)) {
|
|
28 |
4
| addViolation(data, node);
|
|
29 |
| } |
|
30 |
| } |
|
31 |
11
| return data;
|
|
32 |
| } |
|
33 |
| |
|
34 |
| } |