Example usage for org.eclipse.jdt.core.dom AST newForStatement

List of usage examples for org.eclipse.jdt.core.dom AST newForStatement

Introduction

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

Prototype

public ForStatement newForStatement() 

Source Link

Document

Creates a new unparented for statement node owned by this AST.

Usage

From source file:com.google.devtools.j2objc.translate.ASTFactory.java

License:Apache License

public static ForStatement newForStatement(AST ast, VariableDeclarationExpression decl, Expression cond,
        Expression updater, Statement body) {
    ForStatement forLoop = ast.newForStatement();
    ASTUtil.getInitializers(forLoop).add(decl);
    forLoop.setExpression(cond);//  w w  w  .  j a va  2  s.c  om
    ASTUtil.getUpdaters(forLoop).add(updater);
    forLoop.setBody(body);
    return forLoop;
}

From source file:org.jboss.forge.roaster.model.impl.statements.ForStatementImpl.java

License:Open Source License

@Override
public org.eclipse.jdt.core.dom.ForStatement materialize(AST ast) {
    if (iter != null) {
        return iter;
    }//w ww .  j a  va 2 s  . co m

    iter = ast.newForStatement();

    for (DeclareExpression declaration : declarations) {
        VariableDeclarationExpression declare = (VariableDeclarationExpression) wireAndGetExpression(
                declaration, this, ast);
        if (iter.initializers().isEmpty()) {
            iter.initializers().add(declare);
        } else {
            VariableDeclarationExpression existing = (VariableDeclarationExpression) iter.initializers().get(0);
            VariableDeclarationFragment frag = (VariableDeclarationFragment) declare.fragments().get(0);
            frag.delete();
            existing.fragments().add(frag);
        }
    }

    if (condition != null) {
        iter.setExpression(wireAndGetExpression(condition, this, ast));
    }

    for (ExpressionSource<O, ForStatement<O, P>, ?> updater : updaters) {
        iter.updaters().add(wireAndGetExpression(updater, this, ast));
    }

    if (body != null) {
        iter.setBody(wireAndGetStatement(body, this, ast));
    }

    return iter;
}

From source file:refactorer.Refactorer.java

License:Apache License

@SuppressWarnings("unchecked")
protected void processTypeDeclaration(AbstractTypeDeclaration type) {
    if (type == null)
        return;//from  ww  w  . j a v  a 2s. c o  m

    List<BodyDeclaration> bodies = type.bodyDeclarations();
    for (BodyDeclaration body : bodies) {
        processBodyDeclaration(body);
    }

    if (type.getNodeType() == ASTNode.TYPE_DECLARATION) {
        TypeDeclaration typeDeclaration = (TypeDeclaration) type;

        if (typeDeclaration.getSuperclassType() != null) {
            ITypeBinding superclass = typeDeclaration.getSuperclassType().resolveBinding();
            if (superclass != null) {
                if ("javax.media.opengl.GLCanvas".equals(superclass.getQualifiedName())) {
                    addImportIfRequired("javax.media.opengl.awt.GLCanvas");
                } else if ("javax.media.opengl.GLJPanel".equals(superclass.getQualifiedName())) {
                    addImportIfRequired("javax.media.opengl.awt.GLJPanel");
                }
            }
        }

        for (Type interfaceType : (List<Type>) typeDeclaration.superInterfaceTypes()) {
            ITypeBinding binding = interfaceType.resolveBinding();
            if (binding != null) {
                String interfaceName = binding.getQualifiedName();
                if ("javax.media.opengl.GLEventListener".equals(interfaceName)) {
                    for (int i = bodies.size() - 1; i >= 0; i--) {
                        BodyDeclaration body = bodies.get(i);
                        if (body.getNodeType() == ASTNode.METHOD_DECLARATION) {
                            MethodDeclaration methodDeclaration = (MethodDeclaration) body;
                            if ("displayChanged".equals(methodDeclaration.getName().getFullyQualifiedName())) {
                                bodies.remove(i);
                            }
                        }
                    }

                    AST ast = typeDeclaration.getAST();
                    MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
                    methodDeclaration.setName(ast.newSimpleName("dispose"));
                    methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
                    methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
                    Block block = ast.newBlock();
                    methodDeclaration.setBody(block);
                    SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration();
                    parameter.setName(ast.newSimpleName("glDrawable"));
                    parameter.setType(ast.newSimpleType(ast.newName("GLAutoDrawable")));
                    methodDeclaration.parameters().add(parameter);
                    bodies.add(methodDeclaration);

                    if ("performance.VBORenderer.GLDisplay.MyGLEventListener"
                            .equals(type.resolveBinding().getQualifiedName())) {
                        ForStatement forStatement = ast.newForStatement();

                        VariableDeclarationFragment forInitializerFragment = ast
                                .newVariableDeclarationFragment();
                        forInitializerFragment.setName(ast.newSimpleName("i"));
                        forInitializerFragment.setInitializer(ast.newNumberLiteral("0"));
                        VariableDeclarationExpression forInitializer = ast
                                .newVariableDeclarationExpression(forInitializerFragment);
                        forInitializer.setType(ast.newPrimitiveType(PrimitiveType.INT));
                        forStatement.initializers().add(forInitializer);

                        InfixExpression forExpression = ast.newInfixExpression();
                        forExpression.setLeftOperand(ast.newSimpleName("i"));
                        forExpression.setOperator(InfixExpression.Operator.LESS);
                        MethodInvocation rightOperand = ast.newMethodInvocation();
                        rightOperand.setExpression(ast.newSimpleName("eventListeners"));
                        rightOperand.setName(ast.newSimpleName("size"));
                        forExpression.setRightOperand(rightOperand);
                        forStatement.setExpression(forExpression);

                        PostfixExpression forUpdater = ast.newPostfixExpression();
                        forUpdater.setOperand(ast.newSimpleName("i"));
                        forUpdater.setOperator(PostfixExpression.Operator.INCREMENT);
                        forStatement.updaters().add(forUpdater);

                        Block forBlock = ast.newBlock();
                        MethodInvocation mi1 = ast.newMethodInvocation();
                        mi1.setName(ast.newSimpleName("dispose"));
                        mi1.arguments().add(ast.newName("glDrawable"));
                        CastExpression castExpression = ast.newCastExpression();
                        castExpression.setType(ast.newSimpleType(ast.newName("GLEventListener")));
                        MethodInvocation mi2 = ast.newMethodInvocation();
                        mi2.setExpression(ast.newName("eventListeners"));
                        mi2.setName(ast.newSimpleName("get"));
                        mi2.arguments().add(ast.newName("i"));
                        castExpression.setExpression(mi2);
                        ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
                        parenthesizedExpression.setExpression(castExpression);
                        mi1.setExpression(parenthesizedExpression);
                        forBlock.statements().add(ast.newExpressionStatement(mi1));
                        forStatement.setBody(forBlock);

                        block.statements().add(forStatement);
                    }
                } else if ("com.sun.opengl.impl.packrect.BackingStoreManager".equals(interfaceName)) {
                    AST ast = typeDeclaration.getAST();
                    MethodDeclaration newMethodDeclaration = ast.newMethodDeclaration();
                    newMethodDeclaration.setName(ast.newSimpleName("canCompact"));
                    newMethodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
                    newMethodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.BOOLEAN));
                    Block block = ast.newBlock();
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(ast.newBooleanLiteral(false));
                    block.statements().add(returnStatement);
                    newMethodDeclaration.setBody(block);
                    bodies.add(newMethodDeclaration);

                    if ("gov.nasa.worldwind.util.TextureAtlas.AtlasBackingStore"
                            .equals(type.resolveBinding().getQualifiedName())) {
                        for (BodyDeclaration body : (List<BodyDeclaration>) typeDeclaration
                                .bodyDeclarations()) {
                            if (body.getNodeType() == ASTNode.METHOD_DECLARATION) {
                                MethodDeclaration methodDeclaration = (MethodDeclaration) body;
                                if ("additionFailed"
                                        .equals(methodDeclaration.getName().getFullyQualifiedName())) {
                                    methodDeclaration
                                            .setReturnType2(ast.newPrimitiveType(PrimitiveType.BOOLEAN));
                                    Block body2 = methodDeclaration.getBody();
                                    if (!body2.statements().isEmpty()) {
                                        Statement firstStatement = (Statement) body2.statements().get(0);
                                        if (firstStatement.getNodeType() == ASTNode.IF_STATEMENT) {
                                            IfStatement ifStatement = (IfStatement) firstStatement;
                                            Expression expression = ifStatement.getExpression();
                                            ifStatement.setExpression(ast.newBooleanLiteral(true));
                                            ReturnStatement newReturnStatement = ast.newReturnStatement();
                                            newReturnStatement.setExpression(expression);
                                            body2.statements().clear();
                                            body2.statements().add(newReturnStatement);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}