Search in sources :

Example 1 with ASTForStatement

use of net.sourceforge.pmd.lang.java.ast.ASTForStatement in project pmd by pmd.

the class StatementAndBraceFinder method visit.

// ----------------------------------------------------------------------------
// BRANCH OUT
@Override
public Object visit(ASTStatement node, Object data) {
    if (!(data instanceof Structure)) {
        return data;
    }
    Structure dataFlow = (Structure) data;
    if (node.jjtGetParent() instanceof ASTForStatement) {
        this.addForExpressionNode(node, dataFlow);
        dataFlow.pushOnStack(NodeType.FOR_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
        tryToLog(NodeType.FOR_BEFORE_FIRST_STATEMENT, node);
    } else if (node.jjtGetParent() instanceof ASTDoStatement) {
        dataFlow.pushOnStack(NodeType.DO_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
        dataFlow.createNewNode(node.jjtGetParent());
        tryToLog(NodeType.DO_BEFORE_FIRST_STATEMENT, node);
    }
    super.visit(node, data);
    if (node.jjtGetParent() instanceof ASTIfStatement) {
        ASTIfStatement st = (ASTIfStatement) node.jjtGetParent();
        if (!st.hasElse()) {
            dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast());
            tryToLog(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, node);
        } else if (st.hasElse() && !st.jjtGetChild(1).equals(node)) {
            dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast());
            tryToLog(NodeType.ELSE_LAST_STATEMENT, node);
        } else {
            dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT, dataFlow.getLast());
            tryToLog(NodeType.IF_LAST_STATEMENT, node);
        }
    } else if (node.jjtGetParent() instanceof ASTWhileStatement) {
        dataFlow.pushOnStack(NodeType.WHILE_LAST_STATEMENT, dataFlow.getLast());
        tryToLog(NodeType.WHILE_LAST_STATEMENT, node);
    } else if (node.jjtGetParent() instanceof ASTForStatement) {
        dataFlow.pushOnStack(NodeType.FOR_END, dataFlow.getLast());
        tryToLog(NodeType.FOR_END, node);
    } else if (node.jjtGetParent() instanceof ASTLabeledStatement) {
        dataFlow.pushOnStack(NodeType.LABEL_LAST_STATEMENT, dataFlow.getLast());
        tryToLog(NodeType.LABEL_LAST_STATEMENT, node);
    }
    return data;
}
Also used : ASTWhileStatement(net.sourceforge.pmd.lang.java.ast.ASTWhileStatement) ASTDoStatement(net.sourceforge.pmd.lang.java.ast.ASTDoStatement) ASTLabeledStatement(net.sourceforge.pmd.lang.java.ast.ASTLabeledStatement) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) Structure(net.sourceforge.pmd.lang.dfa.Structure) ASTForStatement(net.sourceforge.pmd.lang.java.ast.ASTForStatement)

Example 2 with ASTForStatement

use of net.sourceforge.pmd.lang.java.ast.ASTForStatement in project pmd by pmd.

the class StatementAndBraceFinder method visit.

@Override
public Object visit(ASTExpression node, Object data) {
    if (!(data instanceof Structure)) {
        return data;
    }
    Structure dataFlow = (Structure) data;
    String loggerTag = "parent";
    Node parent = node.jjtGetParent();
    // TODO what about throw stmts?
    if (parent instanceof ASTIfStatement) {
        // START IF
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.IF_EXPR, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.IF_EXPR, node);
    } else if (parent instanceof ASTWhileStatement) {
        // START WHILE
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.WHILE_EXPR, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.WHILE_EXPR, node);
    } else if (parent instanceof ASTSwitchStatement) {
        // START SWITCH
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.SWITCH_START, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.SWITCH_START, node);
    } else if (parent instanceof ASTForStatement) {
        // FOR EXPR
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.FOR_EXPR, node);
    } else if (parent instanceof ASTDoStatement) {
        // DO EXPR
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.DO_EXPR, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.DO_EXPR, node);
    } else if (parent instanceof ASTAssertStatement) {
        dataFlow.createNewNode(node);
        dataFlow.pushOnStack(NodeType.ASSERT_STATEMENT, dataFlow.getLast());
        tryToLog(loggerTag, NodeType.ASSERT_STATEMENT, node);
    }
    return super.visit(node, data);
}
Also used : ASTWhileStatement(net.sourceforge.pmd.lang.java.ast.ASTWhileStatement) ASTDoStatement(net.sourceforge.pmd.lang.java.ast.ASTDoStatement) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTSwitchStatement(net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement) ASTAssertStatement(net.sourceforge.pmd.lang.java.ast.ASTAssertStatement) Structure(net.sourceforge.pmd.lang.dfa.Structure) ASTForStatement(net.sourceforge.pmd.lang.java.ast.ASTForStatement)

Example 3 with ASTForStatement

use of net.sourceforge.pmd.lang.java.ast.ASTForStatement in project pmd by pmd.

the class StatementAndBraceFinder method addForExpressionNode.

/*
     * The method handles the special "for" loop. It creates always an
     * expression node even if the loop looks like for(;;).
     */
private void addForExpressionNode(Node node, Structure dataFlow) {
    ASTForStatement parent = (ASTForStatement) node.jjtGetParent();
    boolean hasExpressionChild = false;
    boolean hasForInitNode = false;
    boolean hasForUpdateNode = false;
    for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
        if (parent.jjtGetChild(i) instanceof ASTExpression) {
            hasExpressionChild = true;
        } else if (parent.jjtGetChild(i) instanceof ASTForUpdate) {
            hasForUpdateNode = true;
        } else if (parent.jjtGetChild(i) instanceof ASTForInit) {
            hasForInitNode = true;
        }
    }
    if (!hasExpressionChild) {
        if (node instanceof ASTForInit) {
            dataFlow.createNewNode(node);
            dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
            tryToLog(NodeType.FOR_EXPR, node);
        } else if (node instanceof ASTForUpdate) {
            if (!hasForInitNode) {
                dataFlow.createNewNode(node);
                dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
                tryToLog(NodeType.FOR_EXPR, node);
            }
        } else if (node instanceof ASTStatement) {
            if (!hasForInitNode && !hasForUpdateNode) {
                dataFlow.createNewNode(node);
                dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
                tryToLog(NodeType.FOR_EXPR, node);
            }
        }
    }
}
Also used : ASTForInit(net.sourceforge.pmd.lang.java.ast.ASTForInit) ASTForStatement(net.sourceforge.pmd.lang.java.ast.ASTForStatement) ASTForUpdate(net.sourceforge.pmd.lang.java.ast.ASTForUpdate) ASTStatement(net.sourceforge.pmd.lang.java.ast.ASTStatement) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Aggregations

ASTForStatement (net.sourceforge.pmd.lang.java.ast.ASTForStatement)3 Structure (net.sourceforge.pmd.lang.dfa.Structure)2 ASTDoStatement (net.sourceforge.pmd.lang.java.ast.ASTDoStatement)2 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)2 ASTWhileStatement (net.sourceforge.pmd.lang.java.ast.ASTWhileStatement)2 Node (net.sourceforge.pmd.lang.ast.Node)1 ASTAssertStatement (net.sourceforge.pmd.lang.java.ast.ASTAssertStatement)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)1 ASTForUpdate (net.sourceforge.pmd.lang.java.ast.ASTForUpdate)1 ASTLabeledStatement (net.sourceforge.pmd.lang.java.ast.ASTLabeledStatement)1 ASTStatement (net.sourceforge.pmd.lang.java.ast.ASTStatement)1 ASTSwitchStatement (net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement)1 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1