Search in sources :

Example 1 with ASTElseClause

use of net.sourceforge.pmd.lang.plsql.ast.ASTElseClause in project pmd by pmd.

the class StatementAndBraceFinder method visit.

@Override
public Object visit(ASTIfStatement node, Object data) {
    if (!(data instanceof Structure)) {
        return data;
    }
    Structure dataFlow = (Structure) data;
    LOGGER.finest("ElsifClause) super.visit line");
    super.visit(node, data);
    /*
         * PLSQL AST now has explicit ELSIF and ELSE clauses All of the
         * ELSE_END_STATEMENTS in an IF clause should point to the outer last
         * clause because we have to convert a single PL/SQL IF/ELSIF/ELSE
         * satement into the equivalent set of nested Java if/else {if/else
         * {if/else}} statements
         */
    List<ASTElsifClause> elsifs = node.findChildrenOfType(ASTElsifClause.class);
    ASTElseClause elseClause = node.getFirstChildOfType(ASTElseClause.class);
    // statement
    if (null == elseClause && elsifs.isEmpty()) {
        dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast());
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("pushOnStack (ASTIfClause - no ELSIFs) IF_LAST_STATEMENT_WITHOUT_ELSE: line " + node.getBeginLine() + ", column " + node.getBeginColumn());
        }
    } else {
        if (!elsifs.isEmpty()) {
            ASTElsifClause lastElsifClause = elsifs.get(elsifs.size() - 1);
            for (ASTElsifClause elsifClause : elsifs) {
                /*
                     * If last ELSIF clause is not followed by a ELSE clause
                     * then the last ELSIF is equivalent to an if statement
                     * without an else Otherwise, it is equivalent to an if/else
                     * statement
                     */
                if (lastElsifClause.equals(elsifClause) && elseClause == null) {
                    dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast());
                    if (LOGGER.isLoggable(Level.FINEST)) {
                        LOGGER.finest("pushOnStack (ASTIfClause - with ELSIFs) IF_LAST_STATEMENT_WITHOUT_ELSE: line " + node.getBeginLine() + ", column " + node.getBeginColumn());
                    }
                }
                {
                    dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast());
                    if (LOGGER.isLoggable(Level.FINEST)) {
                        LOGGER.finest("pushOnStack (ASTIfClause - with ELSIFs) ELSE_LAST_STATEMENT : line " + node.getBeginLine() + ", column " + node.getBeginColumn());
                    }
                }
            }
        }
        if (null != elseClause) {
            // Output one terminating else
            dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast());
            if (LOGGER.isLoggable(Level.FINEST)) {
                LOGGER.finest("pushOnStack (ASTIfClause - with ELSE) ELSE_LAST_STATEMENT : line " + node.getBeginLine() + ", column " + node.getBeginColumn());
            }
        }
    }
    return data;
}
Also used : ASTElsifClause(net.sourceforge.pmd.lang.plsql.ast.ASTElsifClause) ASTElseClause(net.sourceforge.pmd.lang.plsql.ast.ASTElseClause) Structure(net.sourceforge.pmd.lang.dfa.Structure)

Example 2 with ASTElseClause

use of net.sourceforge.pmd.lang.plsql.ast.ASTElseClause in project pmd by pmd.

the class StatementAndBraceFinder method visit.

// ----------------------------------------------------------------------------
// BRANCH OUT
@Override
public Object visit(ASTStatement node, Object data) {
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.finest("entry ASTStatement: line " + node.getBeginLine() + ", column " + node.getBeginColumn() + " -> " + node.getClass().getCanonicalName());
    }
    if (!(data instanceof Structure)) {
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("immediate return ASTStatement: line " + node.getBeginLine() + ", column " + node.getBeginColumn());
        }
        return data;
    }
    Structure dataFlow = (Structure) data;
    if (node.jjtGetParent() instanceof ASTForStatement) {
        ASTForStatement st = (ASTForStatement) node.jjtGetParent();
        if (node.equals(st.getFirstChildOfType(ASTStatement.class))) {
            addForExpressionNode(node, dataFlow);
            dataFlow.pushOnStack(NodeType.FOR_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
            if (LOGGER.isLoggable(Level.FINEST)) {
                LOGGER.finest("pushOnStack FOR_BEFORE_FIRST_STATEMENT: line " + node.getBeginLine() + ", column " + node.getBeginColumn());
            }
        }
    } else if (node.jjtGetParent() instanceof ASTLoopStatement) {
        ASTLoopStatement st = (ASTLoopStatement) node.jjtGetParent();
        if (node.equals(st.getFirstChildOfType(ASTStatement.class))) {
            dataFlow.pushOnStack(NodeType.DO_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
            dataFlow.createNewNode(node.jjtGetParent());
            if (LOGGER.isLoggable(Level.FINEST)) {
                LOGGER.finest("pushOnStack DO_BEFORE_FIRST_STATEMENT: line " + node.getBeginLine() + ", column " + node.getBeginColumn());
            }
        }
    }
    super.visit(node, data);
    if (node.jjtGetParent() instanceof ASTElseClause) {
        List<ASTStatement> allStatements = node.jjtGetParent().findChildrenOfType(ASTStatement.class);
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("ElseClause has " + allStatements.size() + " Statements ");
        }
    /*
             * //Restrict to the last Statement of the Else Clause if (node ==
             * allStatements.get(allStatements.size()-1) ) { if
             * (node.jjtGetParent().jjtGetParent() instanceof ASTCaseStatement)
             * { dataFlow.pushOnStack(NodeType.SWITCH_LAST_DEFAULT_STATEMENT,
             * dataFlow.getLast()); LOGGER.finest(
             * "pushOnStack (Else-Below Case) SWITCH_LAST_DEFAULT_STATEMENT: line "
             * + node.getBeginLine() +", column " + node.getBeginColumn()); }
             * /*SRT else // if (node == node.jjtGetParent() instanceof
             * ASTElseClause) { {
             * dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT,
             * dataFlow.getLast()); LOGGER.finest(
             * "pushOnStack (Else-Below If) ELSE_LAST_STATEMENT: line " +
             * node.getBeginLine() +", column " + node.getBeginColumn()); }
             */
    // }
    } else if (node.jjtGetParent() instanceof ASTWhileStatement) {
        ASTWhileStatement statement = (ASTWhileStatement) node.jjtGetParent();
        List<ASTStatement> children = statement.findChildrenOfType(ASTStatement.class);
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("(LastChildren): size " + children.size());
        }
        ASTStatement lastChild = children.get(children.size() - 1);
        // the FOR Statment
        if (node.equals(lastChild)) {
            dataFlow.pushOnStack(NodeType.WHILE_LAST_STATEMENT, dataFlow.getLast());
            if (LOGGER.isLoggable(Level.FINEST)) {
                LOGGER.finest("pushOnStack WHILE_LAST_STATEMENT: line " + node.getBeginLine() + ", column " + node.getBeginColumn());
            }
        }
    } else if (node.jjtGetParent() instanceof ASTForStatement) {
        ASTForStatement statement = (ASTForStatement) node.jjtGetParent();
        List<ASTStatement> children = statement.findChildrenOfType(ASTStatement.class);
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("(LastChildren): size " + children.size());
        }
        ASTStatement lastChild = children.get(children.size() - 1);
        // the FOR Statment
        if (node.equals(lastChild)) {
            dataFlow.pushOnStack(NodeType.FOR_END, dataFlow.getLast());
            if (LOGGER.isLoggable(Level.FINEST)) {
                LOGGER.finest("pushOnStack (LastChildStatemnt) FOR_END: line " + node.getBeginLine() + ", column " + node.getBeginColumn());
            }
        }
    } else if (node.jjtGetParent() instanceof ASTLabelledStatement) {
        dataFlow.pushOnStack(NodeType.LABEL_LAST_STATEMENT, dataFlow.getLast());
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("pushOnStack LABEL_LAST_STATEMENT: line " + node.getBeginLine() + ", column " + node.getBeginColumn());
        }
    }
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.finest("exit ASTStatement: line " + node.getBeginLine() + ", column " + node.getBeginColumn() + " -> " + node.getClass().getCanonicalName() + " ->-> " + node.jjtGetParent().getClass().getCanonicalName());
    }
    return data;
}
Also used : ASTWhileStatement(net.sourceforge.pmd.lang.plsql.ast.ASTWhileStatement) ASTLabelledStatement(net.sourceforge.pmd.lang.plsql.ast.ASTLabelledStatement) ASTElseClause(net.sourceforge.pmd.lang.plsql.ast.ASTElseClause) ASTLoopStatement(net.sourceforge.pmd.lang.plsql.ast.ASTLoopStatement) List(java.util.List) Structure(net.sourceforge.pmd.lang.dfa.Structure) ASTForStatement(net.sourceforge.pmd.lang.plsql.ast.ASTForStatement) ASTStatement(net.sourceforge.pmd.lang.plsql.ast.ASTStatement)

Aggregations

Structure (net.sourceforge.pmd.lang.dfa.Structure)2 ASTElseClause (net.sourceforge.pmd.lang.plsql.ast.ASTElseClause)2 List (java.util.List)1 ASTElsifClause (net.sourceforge.pmd.lang.plsql.ast.ASTElsifClause)1 ASTForStatement (net.sourceforge.pmd.lang.plsql.ast.ASTForStatement)1 ASTLabelledStatement (net.sourceforge.pmd.lang.plsql.ast.ASTLabelledStatement)1 ASTLoopStatement (net.sourceforge.pmd.lang.plsql.ast.ASTLoopStatement)1 ASTStatement (net.sourceforge.pmd.lang.plsql.ast.ASTStatement)1 ASTWhileStatement (net.sourceforge.pmd.lang.plsql.ast.ASTWhileStatement)1