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:de.ovgu.cide.export.physical.ahead.ReturnExtractionHelper.java

License:Open Source License

/**
 * create a method call in the following form "throw ReturnHack.returnInt;"
 * and add it to the body as the last statement
 * //w ww . jav a 2s . co  m
 * 
 * @param method
 * @param oldBody
 */
private void addDummyReturnStatementHack(MethodDeclaration method, Block body) {
    AST ast = method.getAST();
    ThrowStatement throwStmt = ast.newThrowStatement();
    FieldAccess fa = ast.newFieldAccess();
    fa.setExpression(ast.newSimpleName("ReturnHack"));
    fa.setName(ast.newSimpleName("return" + getReturnTypeStr(method)));
    throwStmt.setExpression(fa);

    body.statements().add(throwStmt);
}

From source file:de.ovgu.cide.language.jdt.ASTID.java

License:Open Source License

public static String calculateId(ASTNode node) {
    if (node == null)
        return "/";
    String id = calculateId(node.getParent());

    if (node.getParent() instanceof Block) {
        Block block = (Block) node.getParent();
        int idx = block.statements().indexOf(node);
        id += "[" + idx + "]";
    }//ww  w .j  a  v a 2  s  .  c  om
    if (node.getParent() instanceof ArrayInitializer) {
        ArrayInitializer block = (ArrayInitializer) node.getParent();
        int idx = block.expressions().indexOf(node);
        id += "[" + idx + "]";
    }
    if (node.getLocationInParent() != null && node.getParent() != null) {
        id += ":" + node.getLocationInParent().getId();
    }
    if (node.getParent() instanceof TryStatement) {
        TryStatement trystmt = (TryStatement) node.getParent();
        if (trystmt.getBody() == node)
            id += "[body]";
    }
    // if (node.getLocationInParent() instanceof
    // ChildListPropertyDescriptor) {
    // List children = (List) node.getParent().getStructuralProperty(
    // node.getLocationInParent());
    // id += "[" + children.indexOf(node) + "]";
    // }

    id += "/" + clean(node.getClass().getSimpleName());

    if (node instanceof TypeDeclaration) {
        id += ":" + clean(((TypeDeclaration) node).getName().toString());
    }
    if (node instanceof FieldDeclaration) {
        for (Object fragment : ((FieldDeclaration) node).fragments()) {
            id += ":" + clean(((VariableDeclarationFragment) fragment).getName().toString());
        }
    }
    if (node instanceof Type) {
        ITypeBinding b = ((Type) node).resolveBinding();
        id += "::" + (b == null ? "null" : b.getQualifiedName());
    }
    if (node instanceof VariableDeclaration)
        id += ":" + clean(((VariableDeclaration) node).getName().toString());
    if (node instanceof Name)
        id += ":" + clean(((Name) node).getFullyQualifiedName());
    if (node instanceof ImportDeclaration)
        id += ":" + clean(((ImportDeclaration) node).getName().toString());
    if (node instanceof MethodDeclaration) {
        Type rt = ((MethodDeclaration) node).getReturnType2();
        id += ":" + clean(rt == null ? "void" : rt.toString());
        id += ":" + clean(((MethodDeclaration) node).getName().toString());
        id += ":" + clean(getParameterTypes(((MethodDeclaration) node).parameters()));
    }

    ChildListPropertyDescriptor[] enumLists = new ChildListPropertyDescriptor[] {
            SwitchStatement.STATEMENTS_PROPERTY, InfixExpression.EXTENDED_OPERANDS_PROPERTY,
            TryStatement.CATCH_CLAUSES_PROPERTY, MethodInvocation.ARGUMENTS_PROPERTY,
            ConstructorInvocation.ARGUMENTS_PROPERTY, ClassInstanceCreation.ARGUMENTS_PROPERTY,
            SuperConstructorInvocation.ARGUMENTS_PROPERTY, SuperMethodInvocation.ARGUMENTS_PROPERTY };
    for (ChildListPropertyDescriptor prop : enumLists) {
        if (node.getLocationInParent() == prop) {
            List<?> childList = (List<?>) node.getParent().getStructuralProperty(prop);
            int idx = childList.indexOf(node);
            id += "[" + idx + "]";
        }
    }

    if (node instanceof CompilationUnit) {
        // IJavaElement je = ((CompilationUnit) node).getJavaElement();
        CompilationUnit cu = ((CompilationUnit) node);
        id += "[";
        if (cu.getPackage() != null && cu.getPackage().getName() != null)
            id += clean(cu.getPackage().getName().getFullyQualifiedName() + ".");
        if (cu.types() != null && cu.types().size() >= 1
                && ((AbstractTypeDeclaration) cu.types().get(0)).getName() != null)
            id += ((AbstractTypeDeclaration) cu.types().get(0)).getName().getFullyQualifiedName();
        id += "]";
        // id += "[" + clean(je.getPath().toPortableString()) + "]";
    }
    return id;
}

From source file:de.ovgu.cide.language.jdt.ASTID.java

License:Open Source License

public static String calculateId_old(ASTNode node) {
    if (node == null)
        return "/";
    String id = calculateId(node.getParent());

    if (node.getParent() instanceof Block) {
        Block block = (Block) node.getParent();
        int idx = block.statements().indexOf(node);
        id += "[" + idx + "]";
    }// w  w w  . j  ava 2s .c  o m
    if (node.getLocationInParent() != null && node.getParent() != null) {
        id += ":" + node.getLocationInParent().getId();
    }
    if (node.getParent() instanceof TryStatement) {
        TryStatement trystmt = (TryStatement) node.getParent();
        if (trystmt.getBody() == node)
            id += "[body]";
    }
    // if (node.getLocationInParent() instanceof
    // ChildListPropertyDescriptor) {
    // List children = (List) node.getParent().getStructuralProperty(
    // node.getLocationInParent());
    // id += "[" + children.indexOf(node) + "]";
    // }

    id += "/" + clean(node.getClass().getSimpleName());

    if (node instanceof TypeDeclaration) {
        id += ":" + clean(((TypeDeclaration) node).getName().toString());
    }
    if (node instanceof FieldDeclaration) {
        for (Object fragment : ((FieldDeclaration) node).fragments()) {
            id += ":" + clean(((VariableDeclarationFragment) fragment).getName().toString());
        }
    }
    if (node instanceof Type) {
        ITypeBinding b = ((Type) node).resolveBinding();
        id += "::" + (b == null ? "null" : b.getQualifiedName());
    }
    if (node instanceof VariableDeclaration)
        id += ":" + clean(((VariableDeclaration) node).getName().toString());
    if (node instanceof Name)
        id += ":" + clean(((Name) node).getFullyQualifiedName());
    if (node instanceof ImportDeclaration)
        id += ":" + clean(((ImportDeclaration) node).getName().toString());
    if (node instanceof MethodDeclaration) {
        Type rt = ((MethodDeclaration) node).getReturnType2();
        id += ":" + clean(rt == null ? "void" : rt.toString());
        id += ":" + clean(((MethodDeclaration) node).getName().toString());
        id += ":" + clean(getParameterTypes(((MethodDeclaration) node).parameters()));
    }

    ChildListPropertyDescriptor[] enumLists = new ChildListPropertyDescriptor[] {
            SwitchStatement.STATEMENTS_PROPERTY, InfixExpression.EXTENDED_OPERANDS_PROPERTY,
            TryStatement.CATCH_CLAUSES_PROPERTY };
    for (ChildListPropertyDescriptor prop : enumLists) {
        if (node.getLocationInParent() == prop) {
            List<?> childList = (List<?>) node.getParent().getStructuralProperty(prop);
            int idx = childList.indexOf(node);
            id += "[" + idx + "]";
        }
    }

    if (node instanceof CompilationUnit) {
        // IJavaElement je = ((CompilationUnit) node).getJavaElement();
        CompilationUnit cu = ((CompilationUnit) node);
        id += "[";
        if (cu.getPackage() != null && cu.getPackage().getName() != null)
            id += clean(cu.getPackage().getName().getFullyQualifiedName() + ".");
        if (cu.types() != null && cu.types().size() >= 1
                && ((AbstractTypeDeclaration) cu.types().get(0)).getName() != null)
            id += ((AbstractTypeDeclaration) cu.types().get(0)).getName().getFullyQualifiedName();
        id += "]";
        // id += "[" + clean(je.getPath().toPortableString()) + "]";
    }
    return id;
}

From source file:edu.buffalo.cse.green.relationship.alternateassociation.AssociationGenerator.java

License:Open Source License

/**
 * @see edu.buffalo.cse.green.relationships.RelationshipGenerator#process(org.eclipse.jdt.core.dom.Block)
 *///  w w w.  j ava2 s  .co  m
protected boolean process(Block node) {
    if (!correctTypeToGenerate())
        return true;
    if (!inConstructor())
        return true;
    generateFields();

    List<Statement> statements = (AbstractList<Statement>) node.statements();
    // generate a generic variable name
    String baseName = getBaseVariableName();

    if (isGeneric()) {
        // create a unique variable name for the parameter
        String paramName = generateVariableName(VariableAffix.getAffixString(ParameterPrefix) + baseName
                + VariableAffix.getAffixString(ParameterSuffix));
        // retrieve the generated field name
        String fieldName = getFieldNames().get(0);

        Assignment ass = createAssignment(fieldName, name(paramName));

        // insert the parameter
        MethodDeclaration method = (MethodDeclaration) node.getParent();
        addParameterizedParameter(method, getTargetType(), paramName);

        // add the assignment to the block
        statements.add(getAST().newExpressionStatement(ass));

        // create the appropriate number of method invocations
        for (int x = 0; x > _cardinality; x--) {
            List<Expression> list = new ArrayList<Expression>();
            list.add(createInvocation(createTypeReference(getActualTargetType())));

            statements.add(createMethodInvocation(fieldName, "add", list));
        }

        // add the variable name(s) to the appropriate list(s)
        getLocalDeclarations().add(paramName);
    } else {
        for (int x = 0; x < _cardinality; x++) {
            // create a unique variable name for the parameter
            String paramName = generateVariableName(baseName);
            // retrieve the generated field name
            String fieldName = getFieldNames().get(x);

            // create the field assignment
            Assignment ass = createAssignment(fieldName, getAST().newSimpleName(paramName));

            // insert the parameter
            MethodDeclaration method = (MethodDeclaration) node.getParent();
            addNormalParameter(method, getTargetType(), paramName);

            // add the assignment to the block
            statements.add(getAST().newExpressionStatement(ass));

            // add the variable name(s) to the appropriate list(s)
            getLocalDeclarations().add(paramName);
        }
    }

    return true;
}

From source file:edu.buffalo.cse.green.relationship.composition.CompositionGenerator.java

License:Open Source License

/**
 * @see edu.buffalo.cse.green.relationships.RelationshipGenerator#process(org.eclipse.jdt.core.dom.Block)
 *//*from   ww  w .jav a2s.c o  m*/
protected boolean process(Block node) {
    if (!correctTypeToGenerate())
        return true;
    if (!inConstructor()) {
        return true;
    }
    generateFields();

    List<Statement> statements = (AbstractList<Statement>) node.statements();

    if (isGeneric()) {
        // retrieve the generated field name
        String fieldName = getFieldNames().get(0);
        Assignment ass = createAssignment(fieldName,
                createParameterizedInvocation(createTypeReference(getTargetType())));
        statements.add(statements.size(), getAST().newExpressionStatement(ass));

        // create the appropriate number of method invocations
        for (int x = 0; x > _cardinality; x--) {
            List<Expression> arguments = new ArrayList<Expression>();
            arguments.add(createInvocation(createTypeReference(getActualTargetType())));

            statements.add(createMethodInvocation(fieldName, "add", arguments));
        }
    } else {
        for (int x = 0; x < _cardinality; x++) {
            // retrieve the generated field name
            String fieldName = getFieldNames().get(x);

            Assignment ass = createAssignment(fieldName,
                    createInvocation(createTypeReference(getActualTargetType())));
            statements.add(statements.size(), getAST().newExpressionStatement(ass));
        }
    }

    return true;
}

From source file:edu.buffalo.cse.green.relationship.dependency.DependencyIGenerator.java

License:Open Source License

/**
 * @see edu.buffalo.cse.green.relationships.RelationshipGenerator#process(org.eclipse.jdt.core.dom.Block)
 *///w  w w  .ja  v  a  2s . c  o  m
protected boolean process(Block block) {
    if (_selectedMethods == null)
        return true;
    if (!correctTypeToGenerate())
        return true;
    IMethod method = (IMethod) getMethodDeclaration().resolveBinding().getJavaElement();
    if (!_selectedMethods.contains(method))
        return true;

    List<Statement> statements = (AbstractList<Statement>) (List) block.statements();

    for (int x = 0; x < _cardinality; x++) {
        ClassInstanceCreation cic = createInvocation(createTypeReference(getActualTargetType()));
        ExpressionStatement stmt = getAST().newExpressionStatement(cic);
        statements.add(stmt);
    }

    return true;
}

From source file:edu.buffalo.cse.green.relationship.dependency.DependencyLGenerator.java

License:Open Source License

/**
 * @see edu.buffalo.cse.green.relationships.RelationshipGenerator#process(org.eclipse.jdt.core.dom.Block)
 *//*from w ww  .java  2s  .com*/
protected boolean process(Block block) {
    if (_selectedMethods == null)
        return true;
    if (!correctTypeToGenerate())
        return true;
    IMethod method = (IMethod) getMethodDeclaration().resolveBinding().getJavaElement();
    if (!_selectedMethods.contains(method))
        return true;

    List<Statement> statements = (AbstractList<Statement>) (List) block.statements();

    for (int x = 0; x < _cardinality; x++) {
        String localName = generateVariableName(VariableAffix.getAffixString(LocalPrefix)
                + getBaseVariableName() + VariableAffix.getAffixString(LocalSuffix));
        VariableDeclarationStatement varDec = createNormalVariableDeclaration(getTargetType(), localName,
                createInvocation(createTypeReference(getActualTargetType())));
        statements.add(0, varDec);

        // add the variable name(s) to the appropriate list(s)
        getLocalDeclarations().add(localName);
    }

    return true;
}

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)
 *///w ww . ja v a 2 s  . c  o 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./*from  w  ww  . j a va2 s.  c  o m*/
 * 
 * @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.
 * //from w  w  w  .  ja va 2  s  . co m
 * @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);
}