1
2
3 package net.sourceforge.pmd.ast;
4
5 public class ASTFieldDeclaration extends AccessNode implements Dimensionable {
6
7 public ASTFieldDeclaration(int id) {
8 super(id);
9 }
10
11 public ASTFieldDeclaration(JavaParser p, int id) {
12 super(p, id);
13 }
14
15 /***
16 * Accept the visitor. *
17 */
18 public Object jjtAccept(JavaParserVisitor visitor, Object data) {
19 return visitor.visit(this, data);
20 }
21
22 public boolean isArray() {
23 return checkType() + checkDecl() > 0;
24 }
25
26 public int getArrayDepth() {
27 if (!isArray()) {
28 return 0;
29 }
30 return checkType() + checkDecl();
31 }
32
33 private int checkType() {
34 if (jjtGetNumChildren() == 0 || !(jjtGetChild(0) instanceof ASTType)) {
35 return 0;
36 }
37 return ((ASTType) jjtGetChild(0)).getArrayDepth();
38 }
39
40 private int checkDecl() {
41 if (jjtGetNumChildren() < 2 || !(jjtGetChild(1) instanceof ASTVariableDeclarator)) {
42 return 0;
43 }
44 return ((ASTVariableDeclaratorId) (jjtGetChild(1).jjtGetChild(0))).getArrayDepth();
45 }
46
47 public void dump(String prefix) {
48 String out = collectDumpedModifiers(prefix);
49 if (isArray()) {
50 out += "(array";
51 for (int i = 0; i < getArrayDepth(); i++) {
52 out += "[";
53 }
54 out += ")";
55 }
56 System.out.println(out);
57 dumpChildren(prefix);
58 }
59
60 /***
61 * Gets the variable name of this field.
62 * This method searches the first VariableDeclartorId node and returns it's image or <code>null</code> if the child node is not found.
63 * @return
64 */
65 public String getVariableName() {
66 ASTVariableDeclaratorId decl = (ASTVariableDeclaratorId) getFirstChildOfType(ASTVariableDeclaratorId.class);
67 if (decl!=null) {
68 return decl.getImage();
69 }
70 return null;
71 }
72 }