Search in sources :

Example 6 with EmptyStatement

use of org.eclipse.jdt.core.dom.EmptyStatement in project eclipse.jdt.ls by eclipse.

the class LocalCorrectionsSubProcessor method addRemoveIncludingConditionProposal.

private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<CUCorrectionProposal> proposals) {
    String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
    AST ast = toRemove.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION);
    if (replacement == null || replacement instanceof EmptyStatement || replacement instanceof Block && ((Block) replacement).statements().size() == 0) {
        if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) {
            rewrite.replace(toRemove, toRemove.getAST().newBlock(), null);
        } else {
            rewrite.remove(toRemove, null);
        }
    } else if (toRemove instanceof Expression && replacement instanceof Expression) {
        Expression moved = (Expression) rewrite.createMoveTarget(replacement);
        Expression toRemoveExpression = (Expression) toRemove;
        Expression replacementExpression = (Expression) replacement;
        ITypeBinding explicitCast = ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression);
        if (explicitCast != null) {
            CastExpression cast = ast.newCastExpression();
            if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
                ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                parenthesized.setExpression(moved);
                moved = parenthesized;
            }
            cast.setExpression(moved);
            ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
            ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(toRemove, imports);
            cast.setType(imports.addImport(explicitCast, ast, importRewriteContext, TypeLocation.CAST));
            moved = cast;
        }
        rewrite.replace(toRemove, moved, null);
    } else {
        ASTNode parent = toRemove.getParent();
        ASTNode moveTarget;
        if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) {
            ListRewrite listRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
            List<Statement> list = ((Block) replacement).statements();
            int lastIndex = list.size() - 1;
            moveTarget = listRewrite.createMoveTarget(list.get(0), list.get(lastIndex));
        } else {
            moveTarget = rewrite.createMoveTarget(replacement);
        }
        rewrite.replace(toRemove, moveTarget, null);
    }
    proposals.add(proposal);
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) List(java.util.List) ArrayList(java.util.ArrayList) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 7 with EmptyStatement

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

the class RemoveEmptyStatementCleanUp method maybeRemoveStmtWithEmptyBody.

private boolean maybeRemoveStmtWithEmptyBody(final Statement visited, final Statement emptyCode) {
    if (isEmptyCode(emptyCode)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveEmptyStatementCleanUp_description);
        if (ASTNodes.canHaveSiblings(visited) || visited.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
            rewrite.remove(visited, group);
            return false;
        }
        if (visited instanceof EmptyStatement) {
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            ASTNodes.replaceButKeepComment(rewrite, visited, ast.newBlock(), group);
            return false;
        }
    }
    return true;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

EmptyStatement (org.eclipse.jdt.core.dom.EmptyStatement)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 Block (org.eclipse.jdt.core.dom.Block)4 IfStatement (org.eclipse.jdt.core.dom.IfStatement)3 Statement (org.eclipse.jdt.core.dom.Statement)3 TextEditGroup (org.eclipse.text.edits.TextEditGroup)3 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)2 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)2 AST (org.eclipse.jdt.core.dom.AST)2 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)2 Expression (org.eclipse.jdt.core.dom.Expression)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)2 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1