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

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

Introduction

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

Prototype

public VariableDeclarationExpression newVariableDeclarationExpression(VariableDeclarationFragment fragment) 

Source Link

Document

Creates a new unparented local variable declaration expression node owned by this AST, for the given variable declaration fragment.

Usage

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

License:Apache License

public static VariableDeclarationExpression newVariableDeclarationExpression(AST ast, IVariableBinding binding,
        Expression initializer) {
    VariableDeclarationExpression decl = ast
            .newVariableDeclarationExpression(newVariableDeclarationFragment(ast, binding, initializer));
    decl.setType(newType(ast, binding.getType()));
    ASTUtil.getModifiers(decl).addAll(newModifiers(ast, binding.getModifiers()));
    Types.addBinding(decl, binding.getType());
    return decl;/*from  ww w . j a  v a2 s .co  m*/
}

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   ww  w  .  java2 s  . c o  m*/
                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.jboss.forge.roaster.model.impl.expressions.DeclareExpressionImpl.java

License:Open Source License

@Override
public VariableDeclarationExpression materialize(AST ast) {
    if (isMaterialized()) {
        return var;
    }//from  www  .ja va 2  s .  co m
    VariableDeclarationFragment frag = ast.newVariableDeclarationFragment();
    frag.setName(ast.newSimpleName(name));
    var = ast.newVariableDeclarationExpression(frag);
    var.setType(JDTHelper.getType(type, ast));

    if (init != null) {
        frag.setInitializer(wireAndGetExpression(init, this, ast));
    }
    return var;
}

From source file:refactorer.Refactorer.java

License:Apache License

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

    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);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}