Example usage for org.eclipse.jdt.core.dom ExpressionStatement EXPRESSION_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom ExpressionStatement EXPRESSION_PROPERTY

Introduction

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

Prototype

ChildPropertyDescriptor EXPRESSION_PROPERTY

To view the source code for org.eclipse.jdt.core.dom ExpressionStatement EXPRESSION_PROPERTY.

Click Source Link

Document

The "expression" structural property of this node type (child type: Expression ).

Usage

From source file:org.eclipse.wb.internal.core.model.variable.FieldInitializerVariableSupport.java

License:Open Source License

/**
 * @return the related {@link Statement}'s, that should be moved with component.
 *///w  w w  . java  2s.c om
private List<Statement> getRelatedStatements() {
    List<Statement> statements = Lists.newArrayList();
    // add association statement (when we move component, association may not exist)
    if (m_javaInfo.getAssociation() != null) {
        Statement associationStatement = m_javaInfo.getAssociation().getStatement();
        if (associationStatement != null) {
            statements.add(associationStatement);
        }
    }
    // add related statements
    for (ASTNode relatedNode : m_javaInfo.getRelatedNodes()) {
        // add Statement with MethodInvocation
        {
            MethodInvocation invocation = m_javaInfo.getMethodInvocation(relatedNode);
            if (invocation != null
                    && invocation.getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY) {
                statements.add((Statement) invocation.getParent());
                continue;
            }
        }
        // add Statement with Assignment
        {
            ASTNode fieldAccess = AstNodeUtils.getFieldAssignment(relatedNode);
            if (fieldAccess != null) {
                Assignment fieldAssignment = (Assignment) fieldAccess.getParent();
                if (fieldAssignment.getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY) {
                    statements.add((Statement) fieldAssignment.getParent());
                    continue;
                }
            }
        }
    }
    return statements;
}

From source file:org.eclipse.wb.internal.core.model.variable.LocalReuseVariableSupport.java

License:Open Source License

@Override
public void setType(String newTypeName) throws Exception {
    // if old declaration is with our initializer, split it on declaration and assignment
    if (m_declaration.getName() == m_variable) {
        splitVariable();/* ww w  .  ja  v  a  2s  .  c  o m*/
    }
    // check that existing assignment is part of ExpressionStatement
    Assignment oldAssignment;
    ExpressionStatement oldStatement;
    {
        Assert.isTrue(m_variable.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY
                && m_variable.getParent().getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY,
                "Variable should be part of Assignment in ExpressionStatement, for "
                        + AstNodeUtils.getEnclosingStatement(m_variable));
        oldAssignment = (Assignment) m_variable.getParent();
        oldStatement = (ExpressionStatement) AstNodeUtils.getEnclosingStatement(oldAssignment);
    }
    // convert Assignment into VariableDeclarationStatement with required Type
    {
        AstEditor editor = m_javaInfo.getEditor();
        AST ast = m_variable.getAST();
        // initial values
        int position = oldStatement.getStartPosition();
        String source = "";
        // add type
        Type newType;
        {
            newType = editor.getParser().parseQualifiedType(position, newTypeName);
            source += newTypeName + " ";
        }
        // add variable
        SimpleName newVariable;
        {
            // prepare identifier
            String identifier;
            {
                identifier = AstNodeUtils.getVariableName(m_variable);
                identifier = editor.getUniqueVariableName(position, identifier, null);
                replaceComponentReferences(identifier);
            }
            // prepare variable
            newVariable = editor.getParser().parseVariable(position + source.length(), identifier, null,
                    AstNodeUtils.getTypeBinding(newType), false, Modifier.NONE);
            source += identifier;
        }
        // add " = "
        {
            source += " = ";
        }
        // add initializer
        Expression newInitializer;
        {
            newInitializer = oldAssignment.getRightHandSide();
            oldAssignment.setRightHandSide(ast.newSimpleName("__bar__"));
            String initializerSource = editor.getSource(newInitializer);
            AstNodeUtils.moveNode(newInitializer, position + source.length());
            source += initializerSource;
        }
        // create fragment
        VariableDeclarationFragment newFragment;
        {
            newFragment = ast.newVariableDeclarationFragment();
            newFragment.setName(newVariable);
            newFragment.setInitializer(newInitializer);
            AstNodeUtils.setSourceRange(newFragment, newVariable, newInitializer);
        }
        // create statement
        VariableDeclarationStatement newStatement;
        {
            newStatement = ast.newVariableDeclarationStatement(newFragment);
            newStatement.setType(newType);
            AstNodeUtils.setSourceRange(newStatement, newType, newFragment, 1);
            source += ";";
        }
        // replace old statement with new statement
        {
            List<Statement> statements = DomGenerics.statements((Block) oldStatement.getParent());
            int index = statements.indexOf(oldStatement);
            // replace without "oldStatement", so its parts are not moved
            statements.remove(index);
            editor.replaceSubstring(oldStatement, source);
            // now we can add "newStatement", we created it already with correct positions
            statements.add(index, newStatement);
            // finalize source modifications
            editor.resolveImports(newStatement);
        }
        // now our JavaInfo uses LocalUniqueVariableSupport
        m_javaInfo.setVariableSupport(new LocalUniqueVariableSupport(m_javaInfo, newVariable));
    }
}

From source file:org.moe.natjgen.InitializerEditor.java

License:Apache License

@SuppressWarnings("unchecked")
public boolean hasNatJRegister() {
    Block block = (Block) getRewrite().get(decl, Initializer.BODY_PROPERTY);

    if (block == null) {
        return false;
    }/*from  w w  w.j  a  v  a2  s  . c  o  m*/

    ListRewrite block_stmts = getRewrite().getListRewrite(block, Block.STATEMENTS_PROPERTY);
    for (Statement stmt : (List<Statement>) block_stmts.getRewrittenList()) {
        if (stmt instanceof ExpressionStatement) {
            Expression expr = (Expression) getRewrite().get(stmt, ExpressionStatement.EXPRESSION_PROPERTY);
            if (expr instanceof MethodInvocation) {
                Expression cls = (Expression) getRewrite().get(expr, MethodInvocation.EXPRESSION_PROPERTY);
                if (!(cls instanceof SimpleName)) {
                    continue;
                }

                String identifier = (String) getRewrite().get(cls, SimpleName.IDENTIFIER_PROPERTY);
                if (!identifier.equals(Constants.NatJ) && !identifier.equals(Constants.NatJFQ)) {
                    continue;
                }

                SimpleName method = (SimpleName) getRewrite().get(expr, MethodInvocation.NAME_PROPERTY);
                String methodId = (String) getRewrite().get(method, SimpleName.IDENTIFIER_PROPERTY);
                if (methodId.equals(Constants.NatJRegisterMethod)) {
                    return true;
                }
            }
        }
    }
    return false;
}