1
2
3 package net.sourceforge.pmd.lang.java.ast;
4
5 import net.sourceforge.pmd.Rule;
6
7 public class ASTLocalVariableDeclaration extends AbstractJavaAccessNode implements Dimensionable, CanSuppressWarnings {
8
9 public ASTLocalVariableDeclaration(int id) {
10 super(id);
11 }
12
13 public ASTLocalVariableDeclaration(JavaParser p, int id) {
14 super(p, id);
15 }
16
17
18
19
20 @Override
21 public Object jjtAccept(JavaParserVisitor visitor, Object data) {
22 return visitor.visit(this, data);
23 }
24
25 public boolean hasSuppressWarningsAnnotationFor(Rule rule) {
26 for (int i = 0; i < jjtGetNumChildren(); i++) {
27 if (jjtGetChild(i) instanceof ASTAnnotation) {
28 ASTAnnotation a = (ASTAnnotation) jjtGetChild(i);
29 if (a.suppresses(rule)) {
30 return true;
31 }
32 }
33 }
34 return false;
35 }
36
37 public boolean isArray() {
38 return checkType() + checkDecl() > 0;
39 }
40
41 public int getArrayDepth() {
42 return checkType() + checkDecl();
43 }
44
45 public ASTType getTypeNode() {
46 for (int i = 0; i < jjtGetNumChildren(); i++) {
47 if (jjtGetChild(i) instanceof ASTType) {
48 return (ASTType) jjtGetChild(i);
49 }
50 }
51 throw new IllegalStateException("ASTType not found");
52 }
53
54 private int checkType() {
55 return getTypeNode().getArrayDepth();
56 }
57
58 private ASTVariableDeclaratorId getDecl() {
59 return (ASTVariableDeclaratorId) jjtGetChild(jjtGetNumChildren()-1).jjtGetChild(0);
60 }
61
62 private int checkDecl() {
63 return getDecl().getArrayDepth();
64 }
65
66
67
68
69
70
71
72 public String getVariableName() {
73 ASTVariableDeclaratorId decl = getFirstDescendantOfType(ASTVariableDeclaratorId.class);
74 if (decl != null) {
75 return decl.getImage();
76 }
77 return null;
78 }
79 }