Search in sources :

Example 36 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteIfRatherThanTwoSwitchCasesCleanUp method replaceBySwitch.

private void replaceBySwitch(final SwitchStatement visited, final List<Pair<List<Expression>, List<Statement>>> switchStructure, final int caseIndexWithDefault, final List<BreakStatement> overBreaks) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteIfRatherThanTwoSwitchCasesCleanUp_description);
    List<Block> newBlocks = prepareNewBlocks(rewrite, ast, switchStructure);
    for (BreakStatement breakStatement : overBreaks) {
        rewrite.remove(breakStatement, group);
    }
    int localCaseIndexWithDefault = caseIndexWithDefault;
    Expression discriminant = visited.getExpression();
    Statement currentBlock = null;
    for (int i = switchStructure.size() - 1; i >= 0; i--) {
        Pair<List<Expression>, List<Statement>> caseStructure = switchStructure.get(i);
        Expression newCondition = buildNewCondition(rewrite, ast, discriminant, caseStructure);
        if (currentBlock != null) {
            IfStatement newIfStatement = ast.newIfStatement();
            newIfStatement.setExpression(newCondition);
            newIfStatement.setThenStatement(newBlocks.get(i));
            newIfStatement.setElseStatement(currentBlock);
            currentBlock = newIfStatement;
        } else if (caseStructure.getSecond().isEmpty()) {
            localCaseIndexWithDefault = -1;
        } else if (localCaseIndexWithDefault == -1) {
            IfStatement newIfStatement = ast.newIfStatement();
            newIfStatement.setExpression(newCondition);
            newIfStatement.setThenStatement(newBlocks.get(i));
            currentBlock = newIfStatement;
        } else {
            currentBlock = newBlocks.get(i);
        }
    }
    ASTNodes.replaceButKeepComment(rewrite, visited, currentBlock, group);
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) List(java.util.List) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 37 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteComparisonRatherThanEqualsCleanUp method replaceEquals.

private void replaceEquals(final boolean isPositive, final Expression visited, final MethodInvocation methodInvocation) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteComparisonRatherThanEqualsCleanUp_description);
    if (isInStringAppend(methodInvocation.getParent())) {
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        ASTNodes.replaceButKeepComment(rewrite, visited, ast.newParenthesizedExpression(getCompareToNode(isPositive, methodInvocation)), group);
    } else {
        ASTNodes.replaceButKeepComment(rewrite, visited, getCompareToNode(isPositive, methodInvocation), group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 38 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteComparisonRatherThanEqualsCleanUp method getCompareToNode.

private InfixExpression getCompareToNode(final boolean isPositive, final MethodInvocation visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    MethodInvocation compareToMethod = ast.newMethodInvocation();
    compareToMethod.setExpression(ASTNodes.createMoveTarget(rewrite, visited.getExpression()));
    // $NON-NLS-1$
    compareToMethod.setName(ast.newSimpleName("compareTo"));
    compareToMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, (Expression) visited.arguments().get(0)));
    InfixExpression newInfixExpression = ast.newInfixExpression();
    newInfixExpression.setLeftOperand(compareToMethod);
    newInfixExpression.setOperator(isPositive ? InfixExpression.Operator.EQUALS : InfixExpression.Operator.NOT_EQUALS);
    newInfixExpression.setRightOperand(ast.newNumberLiteral(0));
    return newInfixExpression;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 39 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteDoubleNegationCleanUp method removeDoubleNegation.

private void removeDoubleNegation(final InfixExpression visited, final Expression leftExpression, final Expression rightExpression, final Expression leftNegatedExpression, final Expression rightNegatedExpression) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteDoubleNegationCleanUp_description);
    InfixExpression newInfixExpression = ast.newInfixExpression();
    if (leftNegatedExpression != null) {
        newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, leftNegatedExpression));
        if (rightNegatedExpression != null) {
            newInfixExpression.setOperator(getAppropriateOperator(visited));
            newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightNegatedExpression));
        } else {
            newInfixExpression.setOperator(getNegatedOperator(visited));
            newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightExpression));
        }
    } else {
        newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, leftExpression));
        newInfixExpression.setOperator(getNegatedOperator(visited));
        newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightNegatedExpression));
    }
    rewrite.replace(visited, newInfixExpression, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 40 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteDuplicateAlternativeCleanUp method replaceIfNoElseStatement.

private void replaceIfNoElseStatement(final IfStatement visited, final IfStatement innerIf, final Statement innerMainStatement, final boolean isInnerMainFirst) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteDuplicateAlternativeCleanUp_description);
    if (visited.getThenStatement() instanceof Block || innerMainStatement instanceof Block) {
        InfixExpression newInfixExpression = ast.newInfixExpression();
        Expression outerCondition;
        if (isInnerMainFirst) {
            outerCondition = ASTNodes.createMoveTarget(rewrite, visited.getExpression());
        } else {
            outerCondition = ast.negate(visited.getExpression(), true);
        }
        newInfixExpression.setLeftOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, outerCondition));
        newInfixExpression.setOperator(isInnerMainFirst ? InfixExpression.Operator.CONDITIONAL_AND : InfixExpression.Operator.CONDITIONAL_OR);
        newInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, innerIf.getExpression())));
        ASTNodes.replaceButKeepComment(rewrite, innerIf.getExpression(), newInfixExpression, group);
        ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, innerIf), group);
    } else {
        // Workaround: Do not do the cleanup
        // Prepare the code for the next pass
        Block newBlock = ast.newBlock();
        newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, innerIf));
        ASTNodes.replaceButKeepComment(rewrite, innerIf, newBlock, group);
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)152 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)133 TextEditGroup (org.eclipse.text.edits.TextEditGroup)115 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 Expression (org.eclipse.jdt.core.dom.Expression)44 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)37 ArrayList (java.util.ArrayList)19 Block (org.eclipse.jdt.core.dom.Block)17 Statement (org.eclipse.jdt.core.dom.Statement)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)16 Type (org.eclipse.jdt.core.dom.Type)14 TypeNameDecider (org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 List (java.util.List)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Name (org.eclipse.jdt.core.dom.Name)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)6