Search in sources :

Example 1 with ASTSwitchStatement

use of net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement 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 2 with ASTSwitchStatement

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

the class InsufficientStringBufferDeclarationRule method getFirstParentBlock.

/**
 * Locate the block that the given node is in, if any
 *
 * @param node
 *            The node we're looking for a parent of
 * @return Node - The node that corresponds to any block that may be a
 *         parent of this object
 */
private Node getFirstParentBlock(Node node) {
    Node parentNode = node.jjtGetParent();
    Node lastNode = node;
    while (parentNode != null && !BLOCK_PARENTS.contains(parentNode.getClass())) {
        lastNode = parentNode;
        parentNode = parentNode.jjtGetParent();
    }
    if (parentNode instanceof ASTIfStatement) {
        parentNode = lastNode;
    } else if (parentNode instanceof ASTSwitchStatement) {
        parentNode = getSwitchParent(parentNode, lastNode);
    }
    return parentNode;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTSwitchStatement(net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement)

Example 3 with ASTSwitchStatement

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

the class ConsecutiveLiteralAppendsRule method getFirstParentBlock.

/**
 * Get the first parent. Keep track of the last node though. For If
 * statements it's the only way we can differentiate between if's and else's
 * For switches it's the only way we can differentiate between switches
 *
 * @param node
 *            The node to check
 * @return The first parent block
 */
private Node getFirstParentBlock(Node node) {
    Node parentNode = node.jjtGetParent();
    Node lastNode = node;
    while (parentNode != null && !BLOCK_PARENTS.contains(parentNode.getClass())) {
        lastNode = parentNode;
        parentNode = parentNode.jjtGetParent();
    }
    if (parentNode instanceof ASTIfStatement) {
        parentNode = lastNode;
    } else if (parentNode instanceof ASTSwitchStatement) {
        parentNode = getSwitchParent(parentNode, lastNode);
    }
    return parentNode;
}
Also used : TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTSwitchStatement(net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement)

Aggregations

Node (net.sourceforge.pmd.lang.ast.Node)3 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)3 ASTSwitchStatement (net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement)3 Structure (net.sourceforge.pmd.lang.dfa.Structure)1 ASTAssertStatement (net.sourceforge.pmd.lang.java.ast.ASTAssertStatement)1 ASTDoStatement (net.sourceforge.pmd.lang.java.ast.ASTDoStatement)1 ASTForStatement (net.sourceforge.pmd.lang.java.ast.ASTForStatement)1 ASTWhileStatement (net.sourceforge.pmd.lang.java.ast.ASTWhileStatement)1 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)1