/* Generated By:JJTree: Do not edit this line. ASTVariableDeclaratorId.java */

package net.sourceforge.pmd.ast;

import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;

import java.util.List;

public class ASTVariableDeclaratorId extends SimpleJavaTypeNode {

    public ASTVariableDeclaratorId(int id) {
        super(id);
    }

    public ASTVariableDeclaratorId(JavaParser p, int id) {
        super(p, id);
    }

    /**
     * Accept the visitor. *
     */
    public Object jjtAccept(JavaParserVisitor visitor, Object data) {
        return visitor.visit(this, data);
    }

    private int arrayDepth;
    private VariableNameDeclaration nameDeclaration;

    public VariableNameDeclaration getNameDeclaration() {
        return nameDeclaration;
    }

    public void setNameDeclaration(VariableNameDeclaration decl) {
        nameDeclaration = decl;
    }

    public List<NameOccurrence> getUsages() {
        return getScope().getVariableDeclarations().get(nameDeclaration);
    }

    public void bumpArrayDepth() {
        arrayDepth++;
    }

    public int getArrayDepth() {
        return arrayDepth;
    }

    public boolean isArray() {
        return arrayDepth > 0;
    }

    public boolean isExceptionBlockParameter() {
        return jjtGetParent().jjtGetParent() instanceof ASTTryStatement;
    }

    public SimpleNode getTypeNameNode() {
        if (jjtGetParent() instanceof ASTFormalParameter) {
            return findTypeNameNode(jjtGetParent());
        } else if (jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration || jjtGetParent().jjtGetParent() instanceof ASTFieldDeclaration) {
            return findTypeNameNode(jjtGetParent().jjtGetParent());
        }
        throw new RuntimeException("Don't know how to get the type for anything other than ASTLocalVariableDeclaration/ASTFormalParameter/ASTFieldDeclaration");
    }

    public ASTType getTypeNode() {
        if (jjtGetParent() instanceof ASTFormalParameter) {
            return ((ASTFormalParameter) jjtGetParent()).getTypeNode();
        } else if (jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration || jjtGetParent().jjtGetParent() instanceof ASTFieldDeclaration) {
            SimpleNode n = (SimpleNode) jjtGetParent().jjtGetParent();
            return n.getFirstChildOfType(ASTType.class);
        }
        throw new RuntimeException("Don't know how to get the type for anything other than ASTLocalVariableDeclaration/ASTFormalParameter/ASTFieldDeclaration");
    }

    private SimpleNode findTypeNameNode(Node node) {
        int i = 0;
        while (node.jjtGetChild(i) instanceof ASTAnnotation) {
            // skip annotations
            i++;
        }
        ASTType typeNode = (ASTType) node.jjtGetChild(i);
        return (SimpleNode) typeNode.jjtGetChild(0);
    }
}
