Search in sources :

Example 1 with EmptyStatement

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

the class ExtractToNullCheckedLocalProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    // infrastructure:
    AST ast = this.compilationUnit.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ImportRewrite imports = ImportRewrite.create(this.compilationUnit, true);
    TextEditGroup group = new TextEditGroup(FixMessages.ExtractToNullCheckedLocalProposal_extractCheckedLocal_editName);
    LinkedProposalPositionGroup localNameGroup = new LinkedProposalPositionGroup(LOCAL_NAME_POSITION_GROUP);
    getLinkedProposalModel().addPositionGroup(localNameGroup);
    // AST context:
    Statement origStmt = (Statement) ASTNodes.getParent(this.fieldReference, Statement.class);
    // determine suitable strategy for rearranging elements towards a new code structure:
    RearrangeStrategy rearrangeStrategy = RearrangeStrategy.create(origStmt, rewrite, group);
    Expression toReplace;
    ASTNode directParent = this.fieldReference.getParent();
    if (directParent instanceof FieldAccess) {
        toReplace = (Expression) directParent;
    } else if (directParent instanceof QualifiedName && this.fieldReference.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
        toReplace = (Expression) directParent;
    } else {
        toReplace = this.fieldReference;
    }
    // new local declaration initialized from the field reference
    VariableDeclarationFragment localFrag = ast.newVariableDeclarationFragment();
    VariableDeclarationStatement localDecl = ast.newVariableDeclarationStatement(localFrag);
    // ... type
    localDecl.setType(newType(toReplace.resolveTypeBinding(), ast, imports));
    localDecl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
    // ... name
    String localName = proposeLocalName(this.fieldReference, this.compilationUnit, getCompilationUnit().getJavaProject());
    localFrag.setName(ast.newSimpleName(localName));
    // ... initialization
    localFrag.setInitializer((Expression) ASTNode.copySubtree(ast, toReplace));
    rearrangeStrategy.insertLocalDecl(localDecl);
    // if statement:
    IfStatement ifStmt = ast.newIfStatement();
    // condition:
    InfixExpression nullCheck = ast.newInfixExpression();
    nullCheck.setLeftOperand(ast.newSimpleName(localName));
    nullCheck.setRightOperand(ast.newNullLiteral());
    nullCheck.setOperator(InfixExpression.Operator.NOT_EQUALS);
    ifStmt.setExpression(nullCheck);
    // then block: the original statement
    Block thenBlock = ast.newBlock();
    thenBlock.statements().add(rearrangeStrategy.createMoveTargetForOrigStmt());
    ifStmt.setThenStatement(thenBlock);
    // ... but with the field reference replaced by the new local:
    SimpleName dereferencedName = ast.newSimpleName(localName);
    rewrite.replace(toReplace, dereferencedName, group);
    // else block: a Todo comment
    Block elseBlock = ast.newBlock();
    //$NON-NLS-1$
    String elseStatement = "// TODO " + FixMessages.ExtractToNullCheckedLocalProposal_todoHandleNullDescription;
    if (origStmt instanceof ReturnStatement) {
        Type returnType = newType(((ReturnStatement) origStmt).getExpression().resolveTypeBinding(), ast, imports);
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0));
        elseStatement += '\n' + ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
    }
    EmptyStatement todoNode = (EmptyStatement) rewrite.createStringPlaceholder(elseStatement, ASTNode.EMPTY_STATEMENT);
    elseBlock.statements().add(todoNode);
    ifStmt.setElseStatement(elseBlock);
    // link all three occurrences of the new local variable:
    addLinkedPosition(rewrite.track(localFrag.getName()), true, /*first*/
    LOCAL_NAME_POSITION_GROUP);
    addLinkedPosition(rewrite.track(nullCheck.getLeftOperand()), false, LOCAL_NAME_POSITION_GROUP);
    addLinkedPosition(rewrite.track(dereferencedName), false, LOCAL_NAME_POSITION_GROUP);
    rearrangeStrategy.insertIfStatement(ifStmt, thenBlock);
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 2 with EmptyStatement

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

the class CleanCodeRatherThanSemicolonRefactoring 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) {
        IfStatement is = (IfStatement) parent;
        List<Statement> thenStmts = asList(is.getThenStatement());
        List<Statement> elseStmts = asList(is.getElseStatement());
        boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
        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 TryStatement) {
        TryStatement ts = (TryStatement) parent;
        return maybeRemoveEmptyStmtBody(node, ts, ts.getBody());
    } else if (parent instanceof EnhancedForStatement) {
        EnhancedForStatement efs = (EnhancedForStatement) parent;
        return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
    } else if (parent instanceof ForStatement) {
        ForStatement fs = (ForStatement) parent;
        return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
    } else if (parent instanceof WhileStatement) {
        WhileStatement ws = (WhileStatement) parent;
        return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
    }
    return VISIT_SUBTREE;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) 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 3 with EmptyStatement

use of org.eclipse.jdt.core.dom.EmptyStatement 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 4 with EmptyStatement

use of org.eclipse.jdt.core.dom.EmptyStatement in project eclipse-cs by checkstyle.

the class EmptyStatementQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartPosition) {
    return new ASTVisitor() {

        @Override
        public boolean visit(EmptyStatement node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                // early exit if the statement is mandatory, e.g. only
                // statement in a for-statement without block
                StructuralPropertyDescriptor p = node.getLocationInParent();
                if (p.isChildProperty() && ((ChildPropertyDescriptor) p).isMandatory()) {
                    return false;
                }
                node.delete();
            }
            return false;
        }
    };
}
Also used : EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 5 with EmptyStatement

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

the class RemoveEmptyStatementCleanUp method visit.

@Override
public boolean visit(final EmptyStatement visited) {
    if (isEmptyCode(visited)) {
        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