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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations