Example usage for org.eclipse.jdt.core.dom Statement getNodeType

List of usage examples for org.eclipse.jdt.core.dom Statement getNodeType

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Statement getNodeType.

Prototype

public final int getNodeType() 

Source Link

Document

Returns an integer value identifying the type of this concrete AST node.

Usage

From source file:com.ashigeru.eclipse.util.jdt.internal.ui.handlers.InsertAssertionHandler.java

License:Apache License

private TextEdit createEdit(CompilationUnit ast, MethodDeclaration method, IDocument target) {
    assert ast != null;
    assert method != null;
    assert target != null;
    List<String> objectParams = new ArrayList<String>();
    for (Object o : method.parameters()) {
        SingleVariableDeclaration var = (SingleVariableDeclaration) o;
        if (var.getType().getNodeType() != ASTNode.PRIMITIVE_TYPE) {
            objectParams.add(var.getName().getIdentifier());
        }/*  w  w  w . ja  v a 2  s.c  o  m*/
    }
    if (objectParams.isEmpty()) {
        return null;
    }
    AST factory = ast.getAST();
    ast.recordModifications();
    List<Statement> toInsert = new ArrayList<Statement>();
    for (String name : objectParams) {
        AssertStatement assertion = createAssertion(factory, name);
        toInsert.add(assertion);
    }

    Block body = method.getBody();
    @SuppressWarnings("unchecked")
    List<Statement> statements = body.statements();

    int offset = 0;
    if (statements.isEmpty() == false) {
        Statement first = statements.get(0);
        int type = first.getNodeType();
        if (type == ASTNode.CONSTRUCTOR_INVOCATION || type == ASTNode.SUPER_CONSTRUCTOR_INVOCATION) {
            offset++;
        }
    }

    statements.addAll(offset, toInsert);

    return ast.rewrite(target, null);
}

From source file:com.google.googlejavaformat.java.JavaInputAstVisitor.java

License:Apache License

/**
 * Helper method for {@link DoStatement}s, {@link EnhancedForStatement}s, {@link ForStatement}s,
 * {@link IfStatement}s, and WhileStatements.
 *///from  ww w.ja v  a 2  s. co  m
private void visitStatement(Statement node, CollapseEmptyOrNot collapseEmptyOrNot,
        AllowLeadingBlankLine allowLeadingBlank, AllowTrailingBlankLine allowTrailingBlank) {
    sync(node);
    switch (node.getNodeType()) {
    case ASTNode.BLOCK:
        builder.space();
        visitBlock((Block) node, collapseEmptyOrNot, allowLeadingBlank, allowTrailingBlank);
        break;
    default:
        // TODO(jdd): Fix.
        builder.open(plusTwo);
        builder.breakOp(" ");
        node.accept(this);
        builder.close();
    }
}

From source file:edu.buffalo.cse.green.relationships.RelationshipGenerator.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.Block)
 *///from ww w.j  av a  2  s  .co  m
public final boolean visit(Block node) {
    if (doVisitBlocks()) {
        for (Statement stmt : (AbstractList<Statement>) node.statements()) {
            if (stmt.getNodeType() == VARIABLE_DECLARATION_STATEMENT) {
                VariableDeclarationStatement vds = (VariableDeclarationStatement) stmt;
                List<VariableDeclarationFragment> vdfs = (AbstractList<VariableDeclarationFragment>) vds
                        .fragments();

                for (VariableDeclarationFragment vdf : vdfs) {
                    getLocalDeclarations().add(vdf.getName().getIdentifier());
                }
            }
        }

        process(node);
    }

    return true;
}

From source file:edu.buffalo.cse.green.relationships.RelationshipRecognizer.java

License:Open Source License

/**
 * Processes calls to the add() method if they are called on a parameterized
 * variable./*ww  w.  j  a va  2s .  c  om*/
 * 
 * @param features - The features of the relationship.
 * @param variable - The name of the variable.
 * @param node - The block node to search.
 */
protected void processAddInvocations(List<ASTNode> features, Name variable, ASTNode node) {
    if (!(variable instanceof SimpleName)) {
        return;
    }

    Block block = null;

    while (node != null) {
        if (node.getNodeType() == BLOCK) {
            block = (Block) node;
            break;
        }

        node = node.getParent();
    }

    for (Statement stmt : (AbstractList<Statement>) (List) block.statements()) {
        if (stmt.getNodeType() == EXPRESSION_STATEMENT) {
            ExpressionStatement eStmt = (ExpressionStatement) stmt;
            Expression e = eStmt.getExpression();

            if (e.getNodeType() == METHOD_INVOCATION) {
                MethodInvocation m = (MethodInvocation) e;

                if (m.getExpression() instanceof SimpleName) {
                    SimpleName name = (SimpleName) m.getExpression();
                    SimpleName var = (SimpleName) variable;
                    if (name.getIdentifier().equals(var.getIdentifier())) {
                        // QUESTION: is this related to '*' cardinality for association/composition?
                        if (m.getName().getIdentifier().equals("add")) {
                            features.add(stmt);
                        }
                    }
                }
            }
        }
    }
}

From source file:edu.buffalo.cse.green.relationships.RelationshipRemover.java

License:Open Source License

/**
 * Handles removal of add invocations involved in a relationship.
 * // ww  w  . ja  va  2 s.c  om
 * @param block - The block to process.
 */
protected void processAddInvocations(Block block) {
    List<Statement> stmts = (AbstractList<Statement>) (List) block.statements();
    List<Statement> toRemove = new ArrayList<Statement>();

    for (int x = 1; x < _relationship.getFeatures().size(); x++) {
        stmts.removeAll(toRemove);
        toRemove.clear();

        for (Statement statement : stmts) {
            if (statement.getNodeType() == EXPRESSION_STATEMENT) {
                ExpressionStatement e = (ExpressionStatement) statement;

                if (new ASTMatcher().match(e, _relationship.getFeatures().get(x))) {
                    toRemove.add(e);
                    break;
                }
            }
        }
    }

    stmts.removeAll(toRemove);
}

From source file:edu.cmu.cs.crystal.internal.ControlFlowVisitor.java

License:Open Source License

public boolean visit(SwitchStatement node) {
    Expression expression = node.getExpression();
    List statements = node.statements();
    if (expression == null)
        throw new CrystalRuntimeException("Switch statement without an expression?");
    if (statements == null || statements.size() == 0)
        throw new CrystalRuntimeException("Switch statements without any statements?");

    ControlFlowNode expressionCFN = controlFlowNode.newControlFlowNode(expression);

    controlFlowNode.moveEdges(ControlFlowNode.Direction.BACKWARDS, expressionCFN);

    Iterator i = statements.iterator();
    List<ControlFlowNode> statementCFNs = new LinkedList<ControlFlowNode>();
    ControlFlowNode cfn = null, previous = null;
    Statement astNode;
    boolean haveSeenDefault = false;
    while (i.hasNext()) {
        astNode = (Statement) i.next();
        cfn = controlFlowNode.newControlFlowNode(astNode);
        statementCFNs.add(cfn);/*from ww w  . jav a  2s.  c o  m*/
        if (previous != null)
            previous.addEdge(Direction.FORWARDS, cfn);
        if (astNode.getNodeType() == ASTNode.SWITCH_CASE) {
            expressionCFN.addEdge(Direction.FORWARDS, cfn);
            SwitchCase sc = (SwitchCase) astNode;
            if (sc.isDefault()) {
                if (haveSeenDefault)
                    throw new CrystalRuntimeException("cannot have more than one default in a switch");
                haveSeenDefault = true;
            }
        }
        previous = cfn;
    }
    if (cfn == null)
        throw new CrystalRuntimeException("no statements in switch");
    // if we never saw a default, then add an edge to the switch statement
    if (!haveSeenDefault)
        expressionCFN.addEdge(Direction.FORWARDS, controlFlowNode);
    cfn.addEdge(ControlFlowNode.Direction.FORWARDS, controlFlowNode);

    expressionCFN.evaluate();
    if (statementCFNs != null)
        evaluate(statementCFNs);

    return false;
}

From source file:egovframework.mgt.fit.library.parser.visitor.ClassParsingVisitor.java

License:Apache License

/**
 *   ?   .//from  w  w  w  . j a  va 2  s. c om
 * @param node  
 * @return  ?
 */
private StatementList<AbstractStatement> getStatements(Block node) {
    StatementList<AbstractStatement> statements = new StatementList<AbstractStatement>();
    for (Object obj : node.statements()) {
        if (obj instanceof Statement) {
            Statement s = (Statement) obj;
            if (obj instanceof Block) {
                statements.addStatement(getStatements((Block) obj));
            } else {
                StatementLine sl = new StatementLine();
                sl.setStatementType(s.getNodeType());
                sl.setStatement(s.toString());
                statements.addStatement(sl);
            }
        }
    }
    return statements;
}

From source file:org.autorefactor.cfg.CFGBuilder.java

License:Open Source License

private LivenessState buildCFG(List<Statement> stmts, final LivenessState startState, ThrowerBlocks throwers) {
    LivenessState liveState = startState;
    for (Statement stmt : stmts) {
        switch (stmt.getNodeType()) {
        case ASSERT_STATEMENT:
            liveState = buildCFG((AssertStatement) stmt, liveState, throwers);
            break;
        case BLOCK:
            liveState = buildCFG((Block) stmt, liveState, throwers);
            break;
        case BREAK_STATEMENT:
            liveState = buildCFG((BreakStatement) stmt, liveState, throwers);
            break;
        case CONSTRUCTOR_INVOCATION:
            liveState = buildCFG(stmt, liveState, throwers);
            break;
        case CONTINUE_STATEMENT:
            liveState = buildCFG((ContinueStatement) stmt, liveState, throwers);
            break;
        case DO_STATEMENT:
            liveState = buildCFG((DoStatement) stmt, liveState, throwers);
            break;
        case EMPTY_STATEMENT:
            liveState = buildCFG((EmptyStatement) stmt, liveState, throwers);
            break;
        case ENHANCED_FOR_STATEMENT:
            liveState = buildCFG((EnhancedForStatement) stmt, liveState, throwers);
            break;
        case EXPRESSION_STATEMENT:
            liveState = buildCFG((ExpressionStatement) stmt, liveState, throwers);
            break;
        case FOR_STATEMENT:
            liveState = buildCFG((ForStatement) stmt, liveState, throwers);
            break;
        case IF_STATEMENT:
            liveState = buildCFG((IfStatement) stmt, liveState, throwers);
            break;
        case LABELED_STATEMENT:
            liveState = buildCFG((LabeledStatement) stmt, liveState, throwers);
            break;
        case RETURN_STATEMENT:
            liveState = buildCFG((ReturnStatement) stmt, liveState, throwers);
            break;
        case SUPER_CONSTRUCTOR_INVOCATION:
            liveState = buildCFG(stmt, liveState, throwers);
            break;
        case SWITCH_CASE:
            // Here, use startState.liveBasicBlock to build an edge
            // from the switch condition to the case statement
            liveState = buildCFG((SwitchCase) stmt, startState.liveBasicBlock, liveState, throwers);
            break;
        case SWITCH_STATEMENT:
            liveState = buildCFG((SwitchStatement) stmt, liveState, throwers);
            break;
        case SYNCHRONIZED_STATEMENT:
            liveState = buildCFG((SynchronizedStatement) stmt, liveState, throwers);
            break;
        case THROW_STATEMENT:
            liveState = buildCFG((ThrowStatement) stmt, liveState, throwers);
            break;
        case TRY_STATEMENT:
            liveState = buildCFG((TryStatement) stmt, liveState, throwers);
            // break;case TYPE_DECLARATION_STATEMENT:
            // buildCFG((TypeDeclarationStatement) stmt, liveState, throwers);
            break;
        case VARIABLE_DECLARATION_STATEMENT:
            liveState = buildCFG((VariableDeclarationStatement) stmt, liveState, throwers);
            break;
        case WHILE_STATEMENT:
            liveState = buildCFG((WhileStatement) stmt, liveState, throwers);
            break;
        default:/*w w  w.  ja  v a2 s  .  c o  m*/
            throw new NotImplementedException(stmt);
        }
    }
    return liveState;
}

From source file:org.autorefactor.refactoring.rules.DeadCodeEliminationRefactoring.java

License:Open Source License

private boolean lastStmtIsThrowOrReturn(Statement stmt) {
    final List<Statement> stmts = asList(stmt);
    if (stmts.isEmpty()) {
        return false;
    }/*from  w  ww .  jav  a 2s. c o  m*/

    final Statement lastStmt = stmts.get(stmts.size() - 1);
    switch (lastStmt.getNodeType()) {
    case RETURN_STATEMENT:
    case THROW_STATEMENT:
        return true;

    case IF_STATEMENT:
        final IfStatement ifStmt = (IfStatement) lastStmt;
        final Statement thenStmt = ifStmt.getThenStatement();
        final Statement elseStmt = ifStmt.getElseStatement();
        return lastStmtIsThrowOrReturn(thenStmt) && (elseStmt == null || lastStmtIsThrowOrReturn(elseStmt));

    default:
        return false;
    }
}

From source file:org.autorefactor.refactoring.rules.IfRatherThanWhileAndFallsThroughRefactoring.java

License:Open Source License

/**
 * Return true if the statement falls through.
 *
 * @param stmt the statement//ww w.j a  v  a 2 s. co m
 * @return true if the statement falls through.
 */
private boolean isEndingWithExit(final Statement stmt) {
    final List<Statement> stmts = asList(stmt);
    if (stmts.isEmpty()) {
        return false;
    }

    final Statement lastStmt = stmts.get(stmts.size() - 1);
    switch (lastStmt.getNodeType()) {
    case RETURN_STATEMENT:
    case THROW_STATEMENT:
        return true;

    case BREAK_STATEMENT:
        final BreakStatement breakStmt = (BreakStatement) lastStmt;
        return breakStmt.getLabel() == null;

    case IF_STATEMENT:
        final IfStatement ifStmt = (IfStatement) lastStmt;
        final Statement thenStmt = ifStmt.getThenStatement();
        final Statement elseStmt = ifStmt.getElseStatement();
        return isEndingWithExit(thenStmt) && isEndingWithExit(elseStmt);

    default:
        return false;
    }
}