Search in sources :

Example 61 with ASTNodeFactory

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

the class RemoveEmptyIfCleanUp method replaceThen.

private void replaceThen(final IfStatement visited, final Statement elseStatement, final Expression condition) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveEmptyIfCleanUp_description);
    rewrite.replace(condition, ast.negate(condition, true), group);
    ASTNodes.replaceButKeepComment(rewrite, visited.getThenStatement(), ASTNodes.createMoveTarget(rewrite, elseStatement), 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 62 with ASTNodeFactory

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

the class ObsoleteOppositeConditionRatherThanDuplicateConditionCleanUp method refactorCondition.

private void refactorCondition(final IfStatement node, final Expression duplicateExpression, final Expression notDuplicateExpression, final Statement positiveStatement, final Statement negativeStatement) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOppositeConditionRatherThanDuplicateConditionCleanUp_description);
    Statement negativeStmtCopy;
    if (negativeStatement instanceof IfStatement) {
        Block newBlock = ast.newBlock();
        newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, negativeStatement));
        negativeStmtCopy = newBlock;
    } else {
        negativeStmtCopy = ASTNodes.createMoveTarget(rewrite, negativeStatement);
    }
    Expression secondCond;
    Statement secondStmtCopy;
    Statement thirdStmtCopy;
    PrefixExpression negativeCond = ASTNodes.as(notDuplicateExpression, PrefixExpression.class);
    if (negativeCond != null && ASTNodes.hasOperator(negativeCond, PrefixExpression.Operator.NOT)) {
        secondCond = negativeCond.getOperand();
        secondStmtCopy = ASTNodes.createMoveTarget(rewrite, positiveStatement);
        thirdStmtCopy = ASTNodes.createMoveTarget(rewrite, node.getThenStatement());
    } else {
        secondCond = notDuplicateExpression;
        secondStmtCopy = ASTNodes.createMoveTarget(rewrite, node.getThenStatement());
        thirdStmtCopy = ASTNodes.createMoveTarget(rewrite, positiveStatement);
    }
    ASTNodes.replaceButKeepComment(rewrite, node.getExpression(), ast.negate(duplicateExpression, true), group);
    ASTNodes.replaceButKeepComment(rewrite, node.getThenStatement(), negativeStmtCopy, group);
    IfStatement newIfStatement = ast.newIfStatement();
    newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondCond)));
    newIfStatement.setThenStatement(secondStmtCopy);
    newIfStatement.setElseStatement(thirdStmtCopy);
    ASTNodes.replaceButKeepComment(rewrite, node.getElseStatement(), newIfStatement, group);
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 63 with ASTNodeFactory

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

the class ObsoleteParsingRatherThanValueOfCleanUp method replaceByParsing.

private void replaceByParsing(final MethodInvocation visited, final ITypeBinding typeBinding, final String methodName, final Expression arg0) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteParsingRatherThanValueOfCleanUp_description);
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(ASTNodeFactory.newName(ast, typeBinding.getName()));
    methodInvocation.setName(ast.newSimpleName(methodName));
    methodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arg0)));
    MethodInvocation newMethodInvocation = methodInvocation;
    ASTNodes.replaceButKeepComment(rewrite, visited, newMethodInvocation, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 64 with ASTNodeFactory

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

the class ObsoleteOperandFactorizationCleanUp method replaceDuplicateExpression.

private void replaceDuplicateExpression(final InfixExpression visited, final InfixExpression firstCondition, final Expression firstExpression, final Expression secondExpression, final Expression secondOppositeExpression) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOperandFactorizationCleanUp_description);
    InfixExpression newInnerInfixExpression = ast.newInfixExpression();
    newInnerInfixExpression.setOperator(visited.getOperator());
    newInnerInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
    newInnerInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondOppositeExpression));
    InfixExpression newMainInfixExpression = ast.newInfixExpression();
    newMainInfixExpression.setOperator(firstCondition.getOperator());
    newMainInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
    newMainInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, newInnerInfixExpression));
    ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodeFactory.parenthesizeIfNeeded(ast, newMainInfixExpression), 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 65 with ASTNodeFactory

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

the class ObsoleteRedundantTruthCleanUp method removeBooleanConstant.

private void removeBooleanConstant(final ASTNode visited, final Expression expressionToCopy, final boolean isTrue, final boolean isEquals) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantTruthCleanUp_description);
    Expression operand;
    if (isTrue == isEquals) {
        operand = ASTNodes.createMoveTarget(rewrite, expressionToCopy);
    } else {
        operand = ast.negate(expressionToCopy, true);
    }
    rewrite.replace(visited, ASTNodeFactory.parenthesizeIfNeeded(ast, operand), group);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) 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