Example usage for org.eclipse.jdt.core.dom EnhancedForStatement setExpression

List of usage examples for org.eclipse.jdt.core.dom EnhancedForStatement setExpression

Introduction

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

Prototype

public void setExpression(Expression expression) 

Source Link

Document

Sets the expression of this enhanced for statement.

Usage

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.EnhancedForStatement node) {
    EnhancedForStatement element = (EnhancedForStatement) this.binding.get(node);
    this.initializeNode(element, node);

    if (this.binding.get(node.getExpression()) != null)
        element.setExpression((Expression) this.binding.get(node.getExpression()));
    if (this.binding.get(node.getBody()) != null)
        element.setBody((Statement) this.binding.get(node.getBody()));
    if (this.binding.get(node.getParameter()) != null)
        element.setParameter((SingleVariableDeclaration) this.binding.get(node.getParameter()));
}

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

License:Open Source License

private void replace(VariableAccess varDecl, VariableAccess varAccess) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final AST ast = b.getAST();
    final ASTNode scope = varAccess.getScope();
    final Name varName = varAccess.getVariableName();
    final Type varType = getType(varDecl.getVariableName().getParent());
    if (scope instanceof Block) {
        final List<Statement> stmts = statements((Block) scope);
        for (int i = 0; i < stmts.size(); i++) {
            final Statement stmt = stmts.get(i);
            final Expression parentExpr = getAncestor(varName, Expression.class); // FIXME i=0
            final Statement parentStmt = getAncestor(parentExpr, Statement.class); // FIXME i=0
            if (stmt.equals(parentStmt)) {
                final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
                final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
                vds.setType(varType);//from  www . ja  v  a 2s.c  om
                this.ctx.getRefactorings().replace(stmt, vds);
                break;
            }
        }
    } else if (scope instanceof EnhancedForStatement) {
        final EnhancedForStatement efs = (EnhancedForStatement) scope;
        final EnhancedForStatement newEfs = b.copy(efs);
        newEfs.setParameter(b.copy(efs.getParameter()));
        newEfs.setExpression(b.copy(efs.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(efs.getBody(), parentStmt)) {
            newEfs.setBody(copy(efs.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(efs, newEfs);
    } else if (scope instanceof ForStatement) {
        final ForStatement fs = (ForStatement) scope;
        final ForStatement newFs = b.copy(fs);
        final List<Expression> initializers = initializers(newFs);
        if (initializers.size() == 1) {
            final Expression init = initializers.remove(0);
            final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName);
            final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf);
            vde.setType(varType);
            initializers.add(vde);
            this.ctx.getRefactorings().replace(fs, newFs);
            // TODO JNR
            // if (equalNotNull(fs.getBody(), parentStmt)) {
            // newFs.setBody(copy(fs.getBody()));
            // }
        } else {
            throw new NotImplementedException(scope, "for more than one initializer in for loop.");
        }
    } else if (scope instanceof WhileStatement) {
        final WhileStatement ws = (WhileStatement) scope;
        final WhileStatement newWs = ast.newWhileStatement();
        newWs.setExpression(b.copy(ws.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(ws.getBody(), parentStmt)) {
            newWs.setBody(copy(ws.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(ws, newWs);
    } else if (scope instanceof IfStatement) {
        final IfStatement is = (IfStatement) scope;
        final IfStatement newIs = ast.newIfStatement();
        newIs.setExpression(b.copy(is.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(is.getThenStatement(), parentStmt)) {
            newIs.setThenStatement(copy(is.getThenStatement(), varName));
            if (is.getElseStatement() != null) {
                newIs.setElseStatement(b.copy(is.getElseStatement()));
            }
            this.ctx.getRefactorings().replace(is, newIs);
        } else if (equalNotNull(is.getElseStatement(), parentStmt)) {
            if (is.getThenStatement() != null) {
                newIs.setThenStatement(b.copy(is.getThenStatement()));
            }
            newIs.setElseStatement(copy(is.getElseStatement(), varName));
            this.ctx.getRefactorings().replace(is, newIs);
        } else {
            throw new IllegalStateException(is,
                    "Parent statement should be inside the then or else statement of this if statement: " + is);
        }
    } else {
        throw new NotImplementedException(scope);
    }
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public Statement convert(ForeachStatement statement) {
    switch (this.ast.apiLevel) {
    case AST.JLS2_INTERNAL:
        return createFakeEmptyStatement(statement);
    default:/* w w w .  jav  a 2  s.  c  om*/
        EnhancedForStatement enhancedForStatement = new EnhancedForStatement(this.ast);
        enhancedForStatement.setParameter(convertToSingleVariableDeclaration(statement.elementVariable));
        org.eclipse.jdt.internal.compiler.ast.Expression collection = statement.collection;
        if (collection == null)
            return null;
        enhancedForStatement.setExpression(convert(collection));
        final Statement action = convert(statement.action);
        if (action == null)
            return null;
        enhancedForStatement.setBody(action);
        int start = statement.sourceStart;
        int end = statement.sourceEnd;
        enhancedForStatement.setSourceRange(start, end - start + 1);
        return enhancedForStatement;
    }
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(final org.eclipse.jdt.core.dom.EnhancedForStatement node) {
    EnhancedForStatement element = (EnhancedForStatement) this.binding.get(node);
    initializeNode(element, node);/*  w w  w .  jav  a2 s .  com*/

    if (this.binding.get(node.getExpression()) != null) {
        element.setExpression(JDTVisitorUtils.completeExpression(this.binding.get(node.getExpression()), this));
    }
    if (this.binding.get(node.getBody()) != null) {
        element.setBody((Statement) this.binding.get(node.getBody()));
    }
    if (this.binding.get(node.getParameter()) != null) {
        SingleVariableDeclaration param = (SingleVariableDeclaration) this.binding.get(node.getParameter());
        param.setEnhancedForStatement(element);
        element.setParameter(param);
    }
}

From source file:org.whole.lang.java.util.JDTTransformerVisitor.java

License:Open Source License

public boolean visit(EnhancedForStatement node) {
    org.whole.lang.java.model.EnhancedForStatement forStm = lf
            .create(JavaEntityDescriptorEnum.EnhancedForStatement);

    org.whole.lang.java.model.Parameters params = this.params;
    this.params = null;

    acceptChild(node.getParameter());/*w w w .  ja  v a2 s  .  c  om*/
    forStm.setParameter(varDecl);

    acceptChild(node.getExpression());
    forStm.setExpression(exp);

    acceptChild(node.getBody());
    forStm.setBody(stm);

    this.params = params;

    stm = forStm;
    return false;
}