Example usage for org.eclipse.jdt.core.dom Block statements

List of usage examples for org.eclipse.jdt.core.dom Block statements

Introduction

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

Prototype

ASTNode.NodeList statements

To view the source code for org.eclipse.jdt.core.dom Block statements.

Click Source Link

Document

The list of statements (element type: Statement ).

Usage

From source file:edu.cmu.cs.crystal.cfg.eclipse.EclipseCFG.java

License:Open Source License

@Override
public void endVisit(Block node) {
    EclipseCFGNode bNode = nodeMap.get(node);
    makeListEdges(null, node.statements(), bNode);

    bNode.setName("{}");
}

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

License:Open Source License

/**
 * Example: { int x = 1; x++; }   or  {}
 *//*ww  w  .  java 2s.  co  m*/
public boolean visit(Block node) {
    List statements = node.statements();

    if (statements == null || statements.size() == 0) {
        // controlFlowNode.remove();
        return false;
    }

    List<ControlFlowNode> cfns = createCFNListFromASTNodeList(statements);
    ControlFlowNode cfn = cfns.get(0);
    controlFlowNode.moveEdges(ControlFlowNode.Direction.BACKWARDS, cfn);
    cfn = cfns.get(cfns.size() - 1);
    // Remove the block node from the graph
    cfn.addEdge(ControlFlowNode.Direction.FORWARDS, controlFlowNode);
    //controlFlowNode.moveEdges(ControlFlowNode.Direction.FORWARDS, cfn);
    //controlFlowNode.remove();

    evaluate(cfns);

    return false;
}

From source file:edu.illinois.jflow.core.transformations.code.ExtractClosureAnalyzer.java

License:Open Source License

private String canHandleBranches() {
    if (fReturnValue != null)
        return JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_branch_mismatch;

    ASTNode[] selectedNodes = getSelectedNodes();
    final ASTNode lastSelectedNode = selectedNodes[selectedNodes.length - 1];
    Statement body = getParentLoopBody(lastSelectedNode.getParent());
    if (!(body instanceof Block))
        return JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_branch_mismatch;

    if (body != lastSelectedNode) {
        Block block = (Block) body;
        List<Statement> statements = block.statements();
        ASTNode lastStatementInLoop = statements.get(statements.size() - 1);
        if (lastSelectedNode != lastStatementInLoop)
            return JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_branch_mismatch;
    }/*from  w  w w.  j a va 2  s .  c o  m*/

    final String continueMatchesLoopProblem[] = { null };
    for (int i = 0; i < selectedNodes.length; i++) {
        final ASTNode astNode = selectedNodes[i];
        astNode.accept(new ASTVisitor() {
            ArrayList<String> fLocalLoopLabels = new ArrayList<String>();

            @Override
            public boolean visit(BreakStatement node) {
                SimpleName label = node.getLabel();
                if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
                    continueMatchesLoopProblem[0] = Messages.format(
                            JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_branch_break_mismatch,
                            new Object[] { ("break " + label.getIdentifier()) }); //$NON-NLS-1$
                }
                return false;
            }

            @Override
            public boolean visit(LabeledStatement node) {
                SimpleName label = node.getLabel();
                if (label != null)
                    fLocalLoopLabels.add(label.getIdentifier());
                return true;
            }

            @Override
            public void endVisit(ContinueStatement node) {
                SimpleName label = node.getLabel();
                if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
                    if (fEnclosingLoopLabel == null
                            || !label.getIdentifier().equals(fEnclosingLoopLabel.getIdentifier())) {
                        continueMatchesLoopProblem[0] = Messages.format(
                                JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_branch_continue_mismatch,
                                new Object[] { "continue " + label.getIdentifier() }); //$NON-NLS-1$
                    }
                }
            }
        });
    }
    return continueMatchesLoopProblem[0];
}

From source file:edu.illinois.jflow.core.transformations.code.ExtractClosureAnalyzer.java

License:Open Source License

private void computeLastStatementSelected() {
    ASTNode[] nodes = getSelectedNodes();
    if (nodes.length == 0) {
        fIsLastStatementSelected = false;
    } else {//from ww  w . ja  v  a2s. c  o m
        Block body = null;
        if (fEnclosingBodyDeclaration instanceof MethodDeclaration) {
            body = ((MethodDeclaration) fEnclosingBodyDeclaration).getBody();
        } else if (fEnclosingBodyDeclaration instanceof Initializer) {
            body = ((Initializer) fEnclosingBodyDeclaration).getBody();
        }
        if (body != null) {
            List<Statement> statements = body.statements();
            fIsLastStatementSelected = nodes[nodes.length - 1] == statements.get(statements.size() - 1);
        }
    }
}

From source file:edu.uci.ics.sourcerer.tools.java.extractor.eclipse.ReferenceExtractorVisitor.java

License:Open Source License

@Override
public boolean visit(Block node) {
    fqnStack.peek(EnclosingMethod.class).incrementLevel();
    accept(node.statements());
    fqnStack.peek(EnclosingMethod.class).decrementLevel();
    return false;
}

From source file:edu.umd.cs.findbugs.plugin.eclipse.quickfix.CreateDoPrivilegedBlockResolution.java

License:Open Source License

private Block createRunMethodBody(ASTRewrite rewrite, ClassInstanceCreation classLoaderCreation) {
    AST ast = rewrite.getAST();/*from w  ww .ja  v a  2 s. c  o  m*/

    Block methodBody = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    List<Statement> statements = checkedList(methodBody.statements());

    statements.add(returnStatement);
    returnStatement.setExpression((ClassInstanceCreation) rewrite.createCopyTarget(classLoaderCreation));

    return methodBody;
}

From source file:edu.virginia.aid.visitors.ControlFlowGraphVisitor.java

License:Apache License

@Override
public boolean visit(Block node) {
    List<?> statements = node.statements();
    int size = statements.size();
    for (int i = 0; i < size - 1; i++) {
        Statement statement = (Statement) statements.get(i);
        // temporarily add edge for if-statement
        // addEdge doesn't really add if s is a return statement
        //   and its successor is not equal to last
        addEdge(statement, (Statement) statements.get(i + 1));
    }/* w ww.j a  va 2 s.  com*/

    // could be done faster (this loop is different from the one above)
    if (isInTryBlock()) {
        for (int i = 0; i < size; i++) {
            Statement statement = (Statement) statements.get(i);
            if (!(statement instanceof TryStatement)) {
                handleTryCatchFinally(statement);
            }
        }
    }

    return true;
}

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

License:Apache License

/**
 *   ?   ./*  ww  w . j a  va  2 s  .  c  o m*/
 * @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:gr.uop.intermittent.faults.intermittentfaultscodeparser.ParsingWithEclipseJDT.java

private static void blockIterate(final Block block, final CompilationUnit cu, final Set fieldNames,
        final int blockNum, final Map<Integer, Set> variableNames, final Set<String> parameterNames,
        final BlockStructure bs, final ClassStructure cs, final FileStructure fStr) {
    List<Statement> statements = block.statements();
    // System.out.println("Statements : " + statements.toString());

    Set variables = new HashSet();
    variableNames.put(blockNum, variables);

    for (Statement s : statements) {

        if (s.toString().startsWith("if") || s.toString().startsWith("while") || s.toString().startsWith("for")
                || s.toString().startsWith("try")) {
            s.accept(new ASTVisitor() {
                public boolean visit(Block node) {
                    if (node != null) {
                        //  System.out.println("Block " + node.toString());
                        int blockNumber = blockNum + 1;
                        BlockStructure bs2 = new BlockStructure();
                        bs2.setBlockNum(blockNum);
                        bs2.setBlockParent(bs);
                        bs2.setVariables(new ArrayList<VariableStructure>());
                        Info info = new Info();
                        info.setLine(cu.getLineNumber(node.getStartPosition()));
                        info.setClassName(cs.getClassName());
                        bs2.addBlockInfo(info);
                        bs.addChildBlockStructure(bs2);
                        blockIterate(node, cu, fieldNames, blockNumber, variableNames, parameterNames, bs2, cs,
                                fStr);/*from   w  w  w .  ja  v  a  2  s  .c  o m*/

                        for (int i = blockNumber; i < variableNames.size(); i++)
                            variableNames.remove(i);
                    }
                    return false;
                }
            });
        } else {
            s.accept(new ASTVisitor() {

                public boolean visit(VariableDeclarationStatement node) {
                    String name = node.fragments().get(0).toString().split("=")[0];
                    String type = node.getType().toString();
                    variableNames.get(blockNum).add(name);
                    System.out.println("Declaration of variable '" + name + "' at line"
                            + cu.getLineNumber(node.getStartPosition()) + cs.getClassName());
                    VariableStructure vs = new VariableStructure();
                    vs.setBlockParent(bs);
                    Info info = new Info();
                    info.setClassName(cs.getClassName());
                    info.setLine(cu.getLineNumber(node.getStartPosition()));
                    fStr.addInfoInLineDictionary(cu.getLineNumber(node.getStartPosition()), info);
                    vs.addVariableInfo(info);
                    vs.setVariableName(name);
                    vs.setVariableType(type);
                    bs.addVariableStructure(vs);
                    return false;
                }

                public boolean visit(SimpleName node) {
                    if (searchVariable(variableNames, node.getIdentifier()) != -1
                            || searchField(fieldNames, node.getIdentifier())
                            || searchParameter(parameterNames, node.getIdentifier())) {
                        System.out.println("Usage of variable/field/parameter '" + node.getIdentifier()
                                + "' at line " + cu.getLineNumber(node.getStartPosition()) + cs.getClassName());

                        Info info = SearchCodeStructure.searchFieldOrVariable(node.getIdentifier(), bs);
                        Info infoNew = new Info();
                        infoNew.setClassName(cs.getClassName());
                        infoNew.setFile(cs.getParent().getFileName());
                        infoNew.setLine(cu.getLineNumber(node.getStartPosition()));
                        fStr.addInfoInLineDictionary(cu.getLineNumber(node.getStartPosition()), infoNew);
                        if (info.getParent() instanceof FieldStructure) {
                            ((FieldStructure) info.getParent()).addFieldInfo(infoNew);
                        } else if (info.getParent() instanceof VariableStructure) {
                            ((VariableStructure) info.getParent()).addVariableInfo(infoNew);
                        } else if (info.getParent() instanceof ParameterStructure) {
                            ((ParameterStructure) info.getParent()).addParameterInfo(infoNew);
                        }
                    }
                    return false;
                }

                public boolean visit(MethodInvocation node) {
                    System.out.println("MethodInvocation: " + node.getName() + " at line "
                            + cu.getLineNumber(node.getStartPosition()) + " with arguments " + node.arguments()
                            + cs.getClassName());

                    Expression expression = node.getExpression();

                    ClassStructure classStructure = null;
                    Info info = new Info();
                    if (expression != null) {
                        String className = "";
                        info = SearchCodeStructure.searchFieldOrVariable(expression.toString(), bs);
                        if (info != null) {
                            if (info.getParent() instanceof FieldStructure) {
                                className = ((FieldStructure) info.getParent()).getFieldType();
                            } else if (info.getParent() instanceof VariableStructure) {
                                className = ((VariableStructure) info.getParent()).getVariableType();
                            } else if (info.getParent() instanceof ParameterStructure) {
                                className = ((ParameterStructure) info.getParent()).getParameterType();
                            }
                            classStructure = SearchCodeStructure.searchClass(className, myFS);
                        }
                        System.out.println("Expr: " + expression.toString());
                    } else {
                        classStructure = cs;
                    }

                    Info info2 = SearchCodeStructure.searchMethod(node.getName().getIdentifier(),
                            classStructure);

                    if (info2 == null) {
                        info2 = SearchCodeStructure.searchMethodInAllFiles(node.getName().getIdentifier(),
                                myFS);
                    }

                    Info infoNew = new Info();
                    if (info2 != null) {
                        infoNew.setClassName(cs.getClassName());
                        infoNew.setFile(cs.getParent().getFileName());
                        infoNew.setLine(cu.getLineNumber(node.getStartPosition()));
                        fStr.addInfoInLineDictionary(cu.getLineNumber(node.getStartPosition()), infoNew);
                        ((MethodStructure) info2.getParent()).addMethodInfo(infoNew);
                    }

                    for (Object param : node.arguments()) {
                        info = SearchCodeStructure.searchFieldOrVariable(param.toString(), bs);
                        Info infoNew2 = new Info();
                        infoNew2.setClassName(cs.getClassName());
                        infoNew2.setFile(cs.getParent().getFileName());
                        infoNew2.setLine(cu.getLineNumber(node.getStartPosition()));
                        fStr.addInfoInLineDictionary(cu.getLineNumber(node.getStartPosition()), infoNew2);
                        if (info != null) {
                            if (info.getParent() instanceof FieldStructure) {
                                ((FieldStructure) info.getParent()).addFieldInfo(infoNew2);
                                DependancyInfo dependancyOn = new DependancyInfo();
                                dependancyOn.setDependencyInfoNode(info);
                                dependancyOn.setIsField(true);
                                dependancyOn.setName(param.toString());
                                infoNew.addDependancyInfo(dependancyOn);
                            } else if (info.getParent() instanceof VariableStructure) {
                                ((VariableStructure) info.getParent()).addVariableInfo(infoNew2);
                                DependancyInfo dependancyOn = new DependancyInfo();
                                dependancyOn.setDependencyInfoNode(info);
                                dependancyOn.setIsVariable(true);
                                dependancyOn.setName(param.toString());
                                infoNew.addDependancyInfo(dependancyOn);
                            } else if (info.getParent() instanceof ParameterStructure) {
                                ((ParameterStructure) info.getParent()).addParameterInfo(infoNew2);
                                DependancyInfo dependancyOn = new DependancyInfo();
                                dependancyOn.setDependencyInfoNode(info);
                                dependancyOn.setIsParam(true);
                                dependancyOn.setName(param.toString());
                                infoNew.addDependancyInfo(dependancyOn);
                            }
                        }
                    }

                    return false;
                }

                public boolean visit(Block node) {
                    if (node != null) {
                        //  System.out.println("Block " + node.toString());
                        int blockNumber = blockNum + 1;
                        BlockStructure bs2 = new BlockStructure();
                        bs2.setBlockNum(blockNum);
                        bs2.setBlockParent(bs);
                        bs2.setVariables(new ArrayList<VariableStructure>());
                        Info info = new Info();
                        info.setLine(cu.getLineNumber(node.getStartPosition()));
                        info.setClassName(cs.getClassName());
                        bs2.addBlockInfo(info);
                        bs.addChildBlockStructure(bs2);
                        blockIterate(node, cu, fieldNames, blockNumber, variableNames, parameterNames, bs2, cs,
                                fStr);

                        for (int i = blockNumber; i < variableNames.size(); i++)
                            variableNames.remove(i);
                    }
                    return false;
                }
            });
        }
    }
}

From source file:info.okoshi.visitor.AccessorsGenerateVisitor.java

License:Open Source License

/**
 * Generate getter method.<br>/*from   w w  w  .j  ava 2s .  c o  m*/
 *
 * @param node
 *          {@link FieldDeclaration} object
 * @param ast
 *          {@link AST} object
 * @param name
 *          name of field variable
 * @return {@link MethodDeclaration} object
 */
@SuppressWarnings("unchecked")
private MethodDeclaration generateGetter(FieldDeclaration node, AST ast, String name) {
    MethodDeclaration getter = ast.newMethodDeclaration();
    if (node.getJavadoc() != null) {
        getter.setJavadoc(createGetterJavadoc(node, ast));
    }
    getter.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));

    Type type = (Type) ASTNode.copySubtree(ast, node.getType());
    getter.setReturnType2(type);
    getter.setName(ast.newSimpleName(getGetterPrefix(type) + StringUtils.capitalize(name)));

    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName(name));
    block.statements().add(returnStatement);
    getter.setBody(block);
    return getter;
}