use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class AbstractUnitTestCleanUp method maybeRefactorComparison.
private boolean maybeRefactorComparison(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final InfixExpression infixExpression, final boolean isAssertEquals, final Expression failureMessage) {
Pair<Expression, Expression> actualAndExpected = getActualAndExpected(infixExpression.getLeftOperand(), infixExpression.getRightOperand());
if (isComparingObjects(infixExpression) && !ASTNodes.is(infixExpression.getLeftOperand(), NullLiteral.class) && !ASTNodes.is(infixExpression.getRightOperand(), NullLiteral.class)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
// $NON-NLS-1$
TextEditGroup group = new TextEditGroup("");
MethodInvocation newAssert = invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, // $NON-NLS-1$
getAssertName(isAssertEquals, "Same"), // $NON-NLS-1$
ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actualAndExpected.getFirst())), // $NON-NLS-1$
ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actualAndExpected.getSecond())), null, failureMessage);
ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, newAssert), group);
return false;
}
return maybeRefactorToEquality(classesToUseWithImport, importsToAdd, nodeToReplace, originalMethod, isAssertEquals, actualAndExpected.getFirst(), actualAndExpected.getSecond(), failureMessage, true);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class SingleDeclarationsRatherThanMultiDeclarationCleanUp method refactorMultiDeclaration.
private void refactorMultiDeclaration(final ASTNode visited, final List<IExtendedModifier> modifiers, final Type type, final List<VariableDeclarationFragment> fragments, final Javadoc docComment) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.SingleDeclarationsRatherThanMultiDeclarationCleanUp_description);
for (int i = fragments.size() - 1; 0 <= i; i--) {
VariableDeclarationFragment fragment = fragments.get(i);
SimpleName copyOfFragment = ASTNodes.createMoveTarget(rewrite, fragment.getName());
Type copyOfType = ast.createCopyTarget(type);
Expression copyOfInitializer;
if (fragment.getInitializer() != null) {
copyOfInitializer = ASTNodes.createMoveTarget(rewrite, fragment.getInitializer());
} else {
copyOfInitializer = null;
}
VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(copyOfFragment, copyOfInitializer);
newFragment.extraDimensions().addAll(ASTNodes.createMoveTarget(rewrite, fragment.extraDimensions()));
ASTNode newNode;
if (visited instanceof VariableDeclarationStatement) {
VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement(copyOfType, newFragment);
updateModifiers(modifiers, newStatement.modifiers());
newNode = newStatement;
} else {
FieldDeclaration newField = ast.newFieldDeclaration(copyOfType, newFragment);
if (docComment != null) {
newField.setJavadoc(ast.createCopyTarget(docComment));
}
updateModifiers(modifiers, newField.modifiers());
newNode = newField;
}
if (i > 0) {
rewrite.insertAfter(newNode, visited, group);
} else {
ASTNodes.replaceButKeepComment(rewrite, visited, newNode, group);
}
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class StandardMethodRatherThanLibraryMethodCleanUp method copyArguments.
private List<Expression> copyArguments(final MethodInvocation visited) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
List<Expression> copyOfArgs = new ArrayList<>(visited.arguments().size());
for (Object expression : visited.arguments()) {
copyOfArgs.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) expression)));
}
return copyOfArgs;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class StringBuilderCleanUp method replaceWithString.
private void replaceWithString(final Type type, final Expression initializer, final List<Pair<ITypeBinding, Expression>> allAppendedStrings, final List<Statement> statementsToRemove, final List<MethodInvocation> toStringToRefactor) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
rewrite.replace(initializer, createStringConcats(allAppendedStrings), group);
ASTNodes.replaceButKeepComment(rewrite, type, ast.type(String.class.getSimpleName()), group);
for (Statement statementToRemove : statementsToRemove) {
ASTNodes.removeButKeepComment(rewrite, statementToRemove, group);
}
for (MethodInvocation readToRefactor : toStringToRefactor) {
rewrite.replace(readToRefactor, ASTNodes.createMoveTarget(rewrite, readToRefactor.getExpression()), group);
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class StringBuilderCleanUp method maybeReplaceWithNewStringAppends.
/**
* Rewrite the successive calls to append()
*
* @param visited The node to replace.
* @param allAppendedStrings All appended strings.
* @param lastExpression The expression on which the methods are
* called.
* @param isInstanceCreationToRewrite
* @return
*/
private boolean maybeReplaceWithNewStringAppends(final Expression visited, final List<Pair<ITypeBinding, Expression>> allAppendedStrings, final Expression lastExpression, final boolean isInstanceCreationToRewrite) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
Expression result = null;
List<Expression> tempStringLiterals = new ArrayList<>();
List<Expression> finalStrings = new ArrayList<>();
AtomicBoolean isFirst = new AtomicBoolean(true);
for (Pair<ITypeBinding, Expression> appendedString : allAppendedStrings) {
if (appendedString == null) {
return true;
}
if (isValuedStringLiteralOrConstant(appendedString.getSecond())) {
tempStringLiterals.add(ASTNodes.createMoveTarget(rewrite, appendedString.getSecond()));
} else {
result = handleTempStringLiterals(lastExpression, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
if (isFirst.get()) {
isFirst.lazySet(false);
if (!isInstanceCreationToRewrite) {
result = ASTNodes.createMoveTarget(rewrite, lastExpression);
finalStrings.add(getTypedExpression(appendedString));
} else if (appendedString.getFirst() != null ? ASTNodes.hasType(appendedString.getFirst(), String.class.getCanonicalName()) : ASTNodes.hasType(appendedString.getSecond(), String.class.getCanonicalName())) {
result = ast.newClassInstanceCreation(ASTNodes.createMoveTarget(rewrite, ((ClassInstanceCreation) lastExpression).getType()), getTypedExpression(appendedString));
} else {
result = ast.newClassInstanceCreation(ASTNodes.createMoveTarget(rewrite, ((ClassInstanceCreation) lastExpression).getType()));
finalStrings.add(getTypedExpression(appendedString));
}
} else {
finalStrings.add(getTypedExpression(appendedString));
}
}
}
result = handleTempStringLiterals(lastExpression, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
for (Expression finalString : finalStrings) {
if (result == null) {
result = finalString;
} else {
MethodInvocation appendMethod = ast.newMethodInvocation();
appendMethod.setExpression(result);
// $NON-NLS-1$
appendMethod.setName(ast.newSimpleName("append"));
appendMethod.arguments().add(finalString);
result = appendMethod;
}
}
rewrite.replace(visited, result, group);
return false;
}
Aggregations