Search in sources :

Example 11 with VariableDeclarationExpression

use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final Modifier it) {
    boolean append = true;
    int _flagValue = it.getKeyword().toFlagValue();
    switch(_flagValue) {
        case Modifier.PUBLIC:
            if (((it.getParent() instanceof TypeDeclaration) || (it.getParent() instanceof MethodDeclaration))) {
                append = false;
            }
            break;
        case Modifier.PRIVATE:
            ASTNode _parent = it.getParent();
            if ((_parent instanceof FieldDeclaration)) {
                append = false;
            }
            break;
        case Modifier.FINAL:
            if (((it.getParent() instanceof VariableDeclarationExpression) || (it.getParent() instanceof VariableDeclarationStatement))) {
                append = false;
            }
            break;
        default:
            append = true;
            break;
    }
    if (append) {
        String valueToAppend = it.getKeyword().toString();
        int _flagValue_1 = it.getKeyword().toFlagValue();
        boolean _equals = (_flagValue_1 == 0);
        if (_equals) {
            valueToAppend = "package";
        }
        this.appendToBuffer(valueToAppend);
        this.appendSpaceToBuffer();
    }
    return false;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 12 with VariableDeclarationExpression

use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project AutoRefactor by JnRouvignac.

the class ReduceVariableScopeRefactoring method replace.

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);
            // FIXME i=0
            final Expression parentExpr = getAncestor(varName, Expression.class);
            // FIXME i=0
            final Statement parentStmt = getAncestor(parentExpr, Statement.class);
            if (stmt.equals(parentStmt)) {
                final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
                final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
                vds.setType(varType);
                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);
    }
}
Also used : IllegalStateException(org.autorefactor.util.IllegalStateException) AST(org.eclipse.jdt.core.dom.AST) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) NotImplementedException(org.autorefactor.util.NotImplementedException) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Type(org.eclipse.jdt.core.dom.Type) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 13 with VariableDeclarationExpression

use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project AutoRefactor by JnRouvignac.

the class ForLoopHelper method decomposeInitializer.

/**
 * Decomposes an initializer into a {@link Pair} with the name of the initialized variable
 * and the initializing expression.
 *
 * @param init
 *          the initializer to decompose
 * @return a {@link Pair} with the name of the initialized variable and the initializing
 *         expression, or {@code null} if the initializer could not be decomposed
 */
public static Pair<Name, Expression> decomposeInitializer(Expression init) {
    if (init instanceof VariableDeclarationExpression) {
        final VariableDeclarationExpression vde = (VariableDeclarationExpression) init;
        final List<VariableDeclarationFragment> fragments = fragments(vde);
        if (fragments.size() == 1) {
            final VariableDeclarationFragment fragment = fragments.get(0);
            return Pair.of((Name) fragment.getName(), fragment.getInitializer());
        }
    } else if (init instanceof Assignment) {
        final Assignment as = (Assignment) init;
        if (hasOperator(as, ASSIGN) && as.getLeftHandSide() instanceof Name) {
            return Pair.of((Name) as.getLeftHandSide(), as.getRightHandSide());
        }
    }
    return Pair.empty();
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 14 with VariableDeclarationExpression

use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project AutoRefactor by JnRouvignac.

the class ASTBuilder method declareExpr.

/**
 * Builds a new {@link VariableDeclarationExpression} instance.
 *
 * @param type
 *            the declared variable type
 * @param fragment
 *            the variable declaration fragment
 * @return a new variable declaration expression
 */
public VariableDeclarationExpression declareExpr(Type type, VariableDeclarationFragment fragment) {
    final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(fragment);
    vde.setType(type);
    return vde;
}
Also used : VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression)

Example 15 with VariableDeclarationExpression

use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project AutoRefactor by JnRouvignac.

the class ASTBuilder method declareExpr.

/**
 * Builds a new {@link VariableDeclarationExpression} instance.
 *
 * @param type
 *            the type of the variable being declared
 * @param varName
 *            the name of the variable being declared
 * @param initializer
 *            the variable initializer, can be null
 * @return a new variable declaration expression
 */
public VariableDeclarationExpression declareExpr(Type type, SimpleName varName, Expression initializer) {
    final VariableDeclarationFragment fragment = declareFragment(varName, initializer);
    final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(fragment);
    modifiers(vde).add(final0());
    vde.setType(type);
    return vde;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression)

Aggregations

VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)29 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)22 Expression (org.eclipse.jdt.core.dom.Expression)15 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)13 AST (org.eclipse.jdt.core.dom.AST)12 Assignment (org.eclipse.jdt.core.dom.Assignment)9 Block (org.eclipse.jdt.core.dom.Block)8 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 Type (org.eclipse.jdt.core.dom.Type)8 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)7 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)6 ForStatement (org.eclipse.jdt.core.dom.ForStatement)6 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)6 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)6 Statement (org.eclipse.jdt.core.dom.Statement)6