use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class MatchingStreamRatherThanCountCleanUp method replaceMethod.
private void replaceMethod(final InfixExpression visited, final MethodInvocation filterMethod, final boolean hasElement) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.MatchingStreamRatherThanCountCleanUp_description);
rewrite.replace(visited, filterMethod, group);
rewrite.replace(filterMethod.getName(), ast.newSimpleName(hasElement ? ANYMATCH_METHOD : NONEMATCH_METHOD), group);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class NamedMethodRatherThanLogLevelParameterCleanUp method replaceLevelByMethodName.
private void replaceLevelByMethodName(final MethodInvocation visited, final String methodName) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.NamedMethodRatherThanLogLevelParameterCleanUp_description);
rewrite.replace(visited.getName(), ast.newSimpleName(methodName), group);
rewrite.remove((Expression) visited.arguments().get(0), group);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class NoLoopIterationRatherThanEmptyCheckCleanUp method removeCondition.
private void removeCondition(final InfixExpression condition, final List<Expression> operands) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.NoLoopIterationRatherThanEmptyCheckCleanUp_description);
if (operands.size() == 2) {
rewrite.replace(condition, ASTNodes.createMoveTarget(rewrite, operands.get(0)), group);
} else {
operands.remove(operands.size() - 1);
InfixExpression newCondition = ast.newInfixExpression(condition.getOperator(), ASTNodes.createMoveTarget(rewrite, operands));
rewrite.replace(condition, newCondition, group);
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class NoLoopIterationRatherThanEmptyCheckCleanUp method visit.
@Override
public boolean visit(final IfStatement visited) {
if (visited.getElseStatement() == null) {
List<Statement> statements = ASTNodes.asList(visited.getThenStatement());
if (statements != null && statements.size() == 1) {
Expression container = getContainer(statements);
if (ASTNodes.isArray(container) && ASTNodes.isPassive(container)) {
InfixExpression condition = ASTNodes.as(visited.getExpression(), InfixExpression.class);
if (isConditionValid(condition, container)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.NoLoopIterationRatherThanEmptyCheckCleanUp_description);
ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, statements.get(0)), group);
return false;
}
if (ASTNodes.hasOperator(condition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND)) {
List<Expression> operands = ASTNodes.allOperands(condition);
Expression operand = ASTNodes.as(operands.get(operands.size() - 1), InfixExpression.class);
if (isConditionValid(operand, container)) {
removeCondition(condition, operands);
return false;
}
}
}
}
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite 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);
}
Aggregations