Search in sources :

Example 51 with Block

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

the class ReduceVariableScopeRefactoring method copy.

private Block copy(Statement stmtToCopy, Name varName) {
    if (stmtToCopy != null && !(stmtToCopy instanceof Block)) {
        final Block b = this.ctx.getAST().newBlock();
        final Assignment a = asExpression(stmtToCopy, Assignment.class);
        if (a != null) {
            final VariableDeclarationFragment vdf = getVariableDeclarationFragment(a, varName);
            statements(b).add(this.ctx.getAST().newVariableDeclarationStatement(vdf));
        } else {
            throw new NotImplementedException(stmtToCopy);
        }
        return b;
    }
    // We should never come here if we had a Block statement, see the replace() method
    throw new NotImplementedException(stmtToCopy);
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) NotImplementedException(org.autorefactor.util.NotImplementedException) Block(org.eclipse.jdt.core.dom.Block)

Example 52 with Block

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

the class RemoveEmptyStatementRefactoring method visit.

@Override
public boolean visit(EmptyStatement node) {
    ASTNode parent = node.getParent();
    if (parent instanceof Block) {
        this.ctx.getRefactorings().remove(node);
        return DO_NOT_VISIT_SUBTREE;
    }
    parent = getParentIgnoring(node, Block.class);
    if (parent instanceof IfStatement) {
        final IfStatement is = (IfStatement) parent;
        if (isPassive(is.getExpression())) {
            final List<Statement> thenStmts = asList(is.getThenStatement());
            final List<Statement> elseStmts = asList(is.getElseStatement());
            final boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
            final boolean elseIsEmptyStmt = elseStmts.size() == 1 && is(elseStmts.get(0), EmptyStatement.class);
            if (thenIsEmptyStmt && elseIsEmptyStmt) {
                this.ctx.getRefactorings().remove(parent);
                return DO_NOT_VISIT_SUBTREE;
            } else if (thenIsEmptyStmt && is.getElseStatement() == null) {
                this.ctx.getRefactorings().remove(is);
                return DO_NOT_VISIT_SUBTREE;
            } else if (elseIsEmptyStmt) {
                this.ctx.getRefactorings().remove(is.getElseStatement());
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    } else if (parent instanceof EnhancedForStatement) {
        final EnhancedForStatement efs = (EnhancedForStatement) parent;
        if (isPassive(efs.getExpression()) && efs.getExpression().resolveTypeBinding().isArray()) {
            return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
        }
    } else if (parent instanceof ForStatement) {
        final ForStatement fs = (ForStatement) parent;
        if (arePassive(fs.initializers()) && isPassive(fs.getExpression())) {
            return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
        }
    } else if (parent instanceof WhileStatement) {
        final WhileStatement ws = (WhileStatement) parent;
        if (isPassive(ws.getExpression()) && !Boolean.TRUE.equals(ws.getExpression().resolveConstantExpressionValue())) {
            return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
        }
    } else if (parent instanceof DoStatement) {
        final DoStatement ds = (DoStatement) parent;
        if (isPassive(ds.getExpression()) && !Boolean.TRUE.equals(ds.getExpression().resolveConstantExpressionValue())) {
            return maybeRemoveEmptyStmtBody(node, ds, ds.getBody());
        }
    }
    return VISIT_SUBTREE;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 53 with Block

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

the class ASTBuilder method catch0.

/**
 * Builds a new {@link CatchClause} instance.
 *
 * @param exceptionTypeName the exception type name
 * @param caughtExceptionName the local name for the caught exception
 * @param stmts the statements to add to the catch clause
 * @return a new catch clause
 */
public CatchClause catch0(String exceptionTypeName, String caughtExceptionName, Statement... stmts) {
    final CatchClause cc = ast.newCatchClause();
    final SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setType(simpleType(exceptionTypeName));
    svd.setName(ast.newSimpleName(caughtExceptionName));
    cc.setException(svd);
    final Block block = ast.newBlock();
    addAll(statements(block), stmts);
    cc.setBody(block);
    return cc;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Block(org.eclipse.jdt.core.dom.Block) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Example 54 with Block

use of org.eclipse.jdt.core.dom.Block in project eclipse-pmd by acanda.

the class UseUtilityClassQuickFix method addPrivateConstructor.

@SuppressWarnings("unchecked")
private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) {
    final AST ast = typeDeclaration.getAST();
    final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class);
    constructor.setConstructor(true);
    final Modifier modifier = (Modifier) ast.createInstance(Modifier.class);
    modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD);
    constructor.modifiers().add(modifier);
    constructor.setName(ASTUtil.copy(typeDeclaration.getName()));
    final Block body = (Block) ast.createInstance(Block.class);
    constructor.setBody(body);
    final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
    final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class", ASTNode.EMPTY_STATEMENT);
    statementRewrite.insertFirst(comment, null);
    final int position = findConstructorPosition(typeDeclaration);
    final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
    bodyDeclarationRewrite.insertAt(constructor, position, null);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 55 with Block

use of org.eclipse.jdt.core.dom.Block in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getExchangeInnerAndOuterIfConditionsProposals.

private static boolean getExchangeInnerAndOuterIfConditionsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    boolean result = false;
    //
    if (!(node instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) node;
    if (ifStatement.getElseStatement() != null) {
        return false;
    }
    // case when current IfStatement is sole child of another IfStatement
    {
        IfStatement outerIf = null;
        if (ifStatement.getParent() instanceof IfStatement) {
            outerIf = (IfStatement) ifStatement.getParent();
        } else if (ifStatement.getParent() instanceof Block) {
            Block block = (Block) ifStatement.getParent();
            if (block.getParent() instanceof IfStatement && block.statements().size() == 1) {
                outerIf = (IfStatement) block.getParent();
            }
        }
        if (outerIf != null && outerIf.getElseStatement() == null) {
            if (resultingCollections == null) {
                return true;
            }
            //
            AST ast = node.getAST();
            ASTRewrite rewrite = ASTRewrite.create(ast);
            // prepare conditions
            Expression outerCondition = (Expression) rewrite.createCopyTarget(outerIf.getExpression());
            Expression innerCondition = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
            // exchange conditions
            rewrite.replace(outerIf.getExpression(), innerCondition, null);
            rewrite.replace(ifStatement.getExpression(), outerCondition, null);
            // add correction proposal
            String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeInnerAndOuterIfConditions_description;
            //				Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_INNER_AND_OUTER_IF_CONDITIONS);
            resultingCollections.add(proposal);
            result = true;
        }
    }
    // case when current IfStatement has another IfStatement as sole child
    {
        IfStatement innerIf = null;
        if (ifStatement.getThenStatement() instanceof IfStatement) {
            innerIf = (IfStatement) ifStatement.getThenStatement();
        } else if (ifStatement.getThenStatement() instanceof Block) {
            Block block = (Block) ifStatement.getThenStatement();
            if (block.statements().size() == 1 && block.statements().get(0) instanceof IfStatement) {
                innerIf = (IfStatement) block.statements().get(0);
            }
        }
        if (innerIf != null && innerIf.getElseStatement() == null) {
            if (resultingCollections == null) {
                return true;
            }
            //
            AST ast = node.getAST();
            ASTRewrite rewrite = ASTRewrite.create(ast);
            // prepare conditions
            Expression innerCondition = (Expression) rewrite.createCopyTarget(innerIf.getExpression());
            Expression outerCondition = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
            // exchange conditions
            rewrite.replace(innerIf.getExpression(), outerCondition, null);
            rewrite.replace(ifStatement.getExpression(), innerCondition, null);
            // add correction proposal
            String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeInnerAndOuterIfConditions_description;
            //				Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_INNER_AND_OUTER_IF_CONDITIONS);
            resultingCollections.add(proposal);
            result = true;
        }
    }
    return result;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

Block (org.eclipse.jdt.core.dom.Block)105 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)45 AST (org.eclipse.jdt.core.dom.AST)44 Statement (org.eclipse.jdt.core.dom.Statement)42 Expression (org.eclipse.jdt.core.dom.Expression)39 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)38 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)37 ForStatement (org.eclipse.jdt.core.dom.ForStatement)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)34 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)30 IfStatement (org.eclipse.jdt.core.dom.IfStatement)30 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)28 DoStatement (org.eclipse.jdt.core.dom.DoStatement)26 Type (org.eclipse.jdt.core.dom.Type)26 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)21 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)18