use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class StringBuilderCleanUp method visit.
@Override
public boolean visit(final InfixExpression visited) {
if (isStringConcat(visited)) {
List<Pair<ITypeBinding, Expression>> allOperands = new LinkedList<>();
readSubExpressions(visited, allOperands, new AtomicBoolean(false));
boolean replaceNeeded = filterOutEmptyStringsFromStringConcat(allOperands);
if (replaceNeeded) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.replace(visited, createStringConcats(allOperands), group);
return false;
}
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class RemoveEmptyStatementCleanUp method visit.
@Override
public boolean visit(final EmptyStatement visited) {
if (isEmptyCode(visited)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveEmptyStatementCleanUp_description);
if (ASTNodes.canHaveSiblings(visited) || visited.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
rewrite.remove(visited, group);
return false;
}
if (visited instanceof EmptyStatement) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
ASTNodes.replaceButKeepComment(rewrite, visited, ast.newBlock(), group);
return false;
}
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class SeparateAssertionsRatherThanBooleanExpressionCleanUp method maybeRefactorMethod.
private boolean maybeRefactorMethod(final ExpressionStatement visited, final MethodInvocation originalMethod, final InfixExpression.Operator operator, final int parameterIndex) {
InfixExpression booleanExpression = ASTNodes.as((Expression) originalMethod.arguments().get(parameterIndex), InfixExpression.class);
if (booleanExpression != null && ASTNodes.hasOperator(booleanExpression, operator)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.SeparateAssertionsRatherThanBooleanExpressionCleanUp_description);
List<Expression> allOperands = ASTNodes.allOperands(booleanExpression);
rewrite.replace(booleanExpression, ASTNodes.createMoveTarget(rewrite, allOperands.remove(0)), group);
List<Statement> expressionStatements = new ArrayList<>(allOperands.size());
for (Expression operand : allOperands) {
List<Expression> newArguments = new ArrayList<>(originalMethod.arguments().size());
for (Object argument : originalMethod.arguments()) {
newArguments.add(rewrite.createCopyTarget(ASTNodes.getUnparenthesedExpression((Expression) argument)));
}
newArguments.set(parameterIndex, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(operand)));
MethodInvocation newMethod;
if (originalMethod.getExpression() != null) {
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
newMethodInvocation.setExpression(rewrite.createCopyTarget(originalMethod.getExpression()));
newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
newMethodInvocation.arguments().addAll(newArguments);
newMethod = newMethodInvocation;
} else {
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
newMethodInvocation.setExpression(null);
newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
newMethodInvocation.arguments().addAll(newArguments);
newMethod = newMethodInvocation;
}
ExpressionStatement newStatement = ast.newExpressionStatement(newMethod);
expressionStatements.add(newStatement);
}
if (ASTNodes.canHaveSiblings(visited)) {
Collections.reverse(expressionStatements);
for (Statement expressionStatement : expressionStatements) {
rewrite.insertAfter(expressionStatement, visited, group);
}
} else {
expressionStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
Block newBlock = ast.newBlock();
newBlock.statements().addAll(expressionStatements);
ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
}
return false;
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class StringCleanUp method replaceStringValueOfByArg0.
private void replaceStringValueOfByArg0(final Expression toReplace, final MethodInvocation methodInvocation, final ITypeBinding expectedType) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringCleanUp_description);
ITypeBinding actualType = ((Expression) methodInvocation.arguments().get(0)).resolveTypeBinding();
if (ASTNodes.hasType(actualType, String.class.getCanonicalName()) || expectedType.equals(actualType) || Bindings.getBoxedTypeBinding(expectedType, methodInvocation.getAST()).equals(actualType)) {
rewrite.replace(toReplace, ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0))), group);
} else {
rewrite.replace(toReplace, ast.newCastExpression(ast.type(expectedType.getQualifiedName()), ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0))), group);
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class StringCleanUp method removeToString.
private void removeToString(final MethodInvocation visited) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringCleanUp_description);
if (visited.getExpression() != null) {
rewrite.replace(visited, ASTNodes.createMoveTarget(rewrite, visited.getExpression()), group);
} else {
rewrite.replace(visited, ast.newThisExpression(), group);
}
}
Aggregations