Search in sources :

Example 66 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class ObsoleteUseMultiCatchCleanUp method refactorCatches.

private void refactorCatches(final List<CatchClause> mergeableCatchClauses, final MergeDirection direction) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    AST ast = cuRewrite.getAST();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteUseMultiCatchCleanUp_description);
    List<Type> typesByClause = new ArrayList<>();
    for (CatchClause mergeableCatchClause : mergeableCatchClauses) {
        typesByClause.add(mergeableCatchClause.getException().getType());
    }
    List<Type> allTypes = new ArrayList<>();
    collectAllUnionedTypes(typesByClause, allTypes);
    removeSupersededAlternatives(allTypes);
    UnionType unionType = ast.newUnionType();
    List<Type> unionedTypes = unionType.types();
    unionedTypes.addAll(ASTNodes.createMoveTarget(rewrite, allTypes));
    List<CatchClause> removedClauses = new ArrayList<>(mergeableCatchClauses);
    CatchClause mergedClause;
    if (MergeDirection.UP.equals(direction)) {
        mergedClause = removedClauses.remove(0);
    } else {
        mergedClause = removedClauses.remove(removedClauses.size() - 1);
    }
    rewrite.set(mergedClause.getException(), SingleVariableDeclaration.TYPE_PROPERTY, unionType, group);
    for (CatchClause mergeableCatchClause : removedClauses) {
        rewrite.remove(mergeableCatchClause, group);
    }
}
Also used : UnionType(org.eclipse.jdt.core.dom.UnionType) AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) CatchClause(org.eclipse.jdt.core.dom.CatchClause) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 67 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method replaceDuplicateExpression.

private void replaceDuplicateExpression(final InfixExpression node, final Expression firstExpression, final Expression secondExpression, final AtomicBoolean isFirstExprPositive, final AtomicBoolean isSecondExprPositive) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteXORRatherThanDuplicateConditionsCleanUp_description);
    InfixExpression newInfixExpression = ast.newInfixExpression();
    newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
    newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
    if (isFirstExprPositive.get() == isSecondExprPositive.get()) {
        newInfixExpression.setOperator(InfixExpression.Operator.EQUALS);
    } else {
        newInfixExpression.setOperator(InfixExpression.Operator.XOR);
    }
    ASTNodes.replaceButKeepComment(rewrite, node, 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 68 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class OptimizeRegExCleanUp method rewriteRegEx.

private void rewriteRegEx(final StringLiteral visited, final String pattern) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.OptimizeRegExCleanUp_description);
    rewrite.replace(visited, ast.newStringLiteral(pattern), 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 69 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite 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 70 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class RemoveEmptyIfCleanUp method removeElse.

private void removeElse(final Statement elseStatement) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveEmptyIfCleanUp_description);
    rewrite.remove(elseStatement, group);
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)195 TextEditGroup (org.eclipse.text.edits.TextEditGroup)167 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)133 Expression (org.eclipse.jdt.core.dom.Expression)56 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)48 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)42 Statement (org.eclipse.jdt.core.dom.Statement)24 ArrayList (java.util.ArrayList)22 IfStatement (org.eclipse.jdt.core.dom.IfStatement)20 Block (org.eclipse.jdt.core.dom.Block)15 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 Modifier (org.eclipse.jdt.core.dom.Modifier)11 Type (org.eclipse.jdt.core.dom.Type)11 List (java.util.List)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7