Skip to content

Commit

Permalink
Use ASTNodes.createMoveTarget()
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice Tiercelin committed Apr 28, 2020
1 parent caa9410 commit 90aac00
Show file tree
Hide file tree
Showing 83 changed files with 220 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import org.autorefactor.environment.EventLoop;
import org.autorefactor.jdt.internal.corext.dom.ASTCommentRewriter;
import org.autorefactor.jdt.internal.corext.dom.ASTNodes;
import org.autorefactor.jdt.internal.corext.dom.SourceLocation;
import org.autorefactor.jdt.internal.corext.dom.SourceRewriter;
import org.autorefactor.util.Pair;
Expand Down Expand Up @@ -222,7 +223,8 @@ public <T extends ASTNode> List<T> createMoveTarget(final Collection<T> nodes) {
List<T> movedNodes= new ArrayList<>(nodes.size());

for (T astNode : nodes) {
movedNodes.add(createMoveTarget(astNode));
final T node= astNode;
movedNodes.add(ASTNodes.createMoveTarget(this, node));
}

return movedNodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ public <T extends ASTNode> T createCopyTarget(final T nodeToCopy) {
}

private <T extends ASTNode> T createMoveTarget(final T nodeToMove) {
return refactorings.createMoveTarget(nodeToMove);
return ASTNodes.createMoveTarget(refactorings, nodeToMove);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Set;
import java.util.TreeSet;

import org.autorefactor.jdt.core.dom.ASTRewrite;
import org.autorefactor.util.IllegalStateException;
import org.autorefactor.util.NotImplementedException;
import org.autorefactor.util.Pair;
Expand Down Expand Up @@ -2995,4 +2996,24 @@ public static int getNbOperands(final Expression node) {

return nbOperands;
}

/**
* Creates and returns a placeholder node where to move the source code of the
* provided node.<br>
* The placeholder node can be used like any new node created via the AST
* class.<br>
* When the document is rewritten, the source code for the provided node is
* inserted into the output document at the position corresponding to the
* placeholder (indentation is adjusted) and it is removed from the old
* location.
*
* @param <T> the type of the provided node
* @param rewrite the AST rewrite
* @param expression the node for which to create a move placeholder
* @return the new placeholder node
* @see ASTRewrite#createMoveTargetOld(ASTNode)
*/
public static <T extends ASTNode> T createMoveTarget(final ASTRewrite rewrite, final T expression) {
return rewrite.createMoveTarget(expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ private void replaceLoopAndVariable(final Statement forNode, final Expression it

Statement replacement;
if (previousStmtIsPreviousSibling && previousStatement instanceof VariableDeclarationStatement) {
replacement= ast.declareStatement(ast.type(boolean.class.getSimpleName()), rewrite.createMoveTarget((SimpleName) initName),
replacement= ast.declareStatement(ast.type(boolean.class.getSimpleName()), ASTNodes.createMoveTarget(rewrite, (SimpleName) initName),
newMethod(iterable, toFind, isPositive, classesToUseWithImport, importsToAdd));
} else if (!previousStmtIsPreviousSibling || previousStatement instanceof ExpressionStatement) {
replacement= ast.toStatement(ast.assign(rewrite.createMoveTarget(initName), Assignment.Operator.ASSIGN, newMethod(iterable, toFind, isPositive, classesToUseWithImport, importsToAdd)));
replacement= ast.toStatement(ast.assign(ASTNodes.createMoveTarget(rewrite, initName), Assignment.Operator.ASSIGN, newMethod(iterable, toFind, isPositive, classesToUseWithImport, importsToAdd)));
} else {
throw new NotImplementedException(forNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private void refactorToAssertTrueOrFalse(final Set<String> classesToUseWithImpor
String methodName= isAssertTrue ? "assertTrue" : "assertFalse"; //$NON-NLS-1$ //$NON-NLS-2$

rewrite.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, ast,
invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, methodName, rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(condition)), null, null, failureMessage)), null);
invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, methodName, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(condition)), null, null, failureMessage)), null);
}

private boolean maybeReplaceOrRemove(final Set<String> classesToUseWithImport, final Set<String> importsToAdd,
Expand Down Expand Up @@ -361,7 +361,7 @@ private boolean maybeRefactorComparison(final Set<String> classesToUseWithImport
ASTRewrite rewrite= cuRewrite.getASTRewrite();

MethodInvocation newAssert= invokeMethod(classesToUseWithImport, importsToAdd, originalMethod,
getAssertName(isAssertEquals, "Same"), rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(actualAndExpected.getFirst())), rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(actualAndExpected.getSecond())), //$NON-NLS-1$
getAssertName(isAssertEquals, "Same"), ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actualAndExpected.getFirst())), ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actualAndExpected.getSecond())), //$NON-NLS-1$
null, failureMessage);
rewrite.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, ast, newAssert), null);
return false;
Expand Down Expand Up @@ -478,7 +478,7 @@ private MethodInvocation invokeAssertNull(final Set<String> classesToUseWithImpo
ASTRewrite rewrite= cuRewrite.getASTRewrite();

String methodName= getAssertName(isPositive, "Null"); //$NON-NLS-1$
Expression copyOfActual= rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(actual));
Expression copyOfActual= ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actual));
return invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, methodName, copyOfActual, null, null, failureMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ private void replaceWithCollectionsAddAll(final Statement node, final Set<String
String classname= addImport(Collections.class, classesToUseWithImport, importsToAdd);
rewrite.replace(node,
ast.toStatement(ast.newMethodInvocation(ast.name(classname),
"addAll", addMethod.getExpression() != null ? rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(addMethod.getExpression())) : ast.this0(), //$NON-NLS-1$
rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(iterable)))), null);
"addAll", addMethod.getExpression() != null ? ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(addMethod.getExpression())) : ast.this0(), //$NON-NLS-1$
ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(iterable)))), null);
}

private int getVariableUseCount(final IVariableBinding variableBinding, final Statement toVisit) {
Expand Down Expand Up @@ -273,9 +273,9 @@ private void replaceWithCollectionMethod(final ASTNode toReplace, final String m

MethodInvocation newMethod;
if (affectedCollection != null) {
newMethod= ast.newMethodInvocation(rewrite.createMoveTarget(affectedCollection), methodName, rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(data)));
newMethod= ast.newMethodInvocation(ASTNodes.createMoveTarget(rewrite, affectedCollection), methodName, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(data)));
} else {
newMethod= ast.newMethodInvocation(methodName, rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(data)));
newMethod= ast.newMethodInvocation(methodName, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(data)));
}

rewrite.replace(toReplace, ast.toStatement(newMethod), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.autorefactor.jdt.core.dom.ASTRewrite;
import org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory;
import org.autorefactor.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.DoStatement;
import org.eclipse.jdt.core.dom.EnhancedForStatement;
Expand Down Expand Up @@ -106,7 +107,7 @@ private boolean maybeAddBrackets(final Statement statement) {
ASTNodeFactory ast= cuRewrite.getASTBuilder();
ASTRewrite rewrite= cuRewrite.getASTRewrite();

Block block= ast.block(rewrite.createMoveTarget(statement));
Block block= ast.block(ASTNodes.createMoveTarget(rewrite, statement));
rewrite.replace(statement, block, null);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ && maybeRefactor(node, classesToUseWithImport, importsToAdd, "Maps", LinkedHashM
String classname= addImport(EnumMap.class, classesToUseWithImport, importsToAdd);
Type type= ast.getAST().newParameterizedType(
ast.type(classname));
rewrite.replace(node, ast.new0(type, rewrite.createMoveTarget(arg)), null);
rewrite.replace(node, ast.new0(type, ASTNodes.createMoveTarget(rewrite, arg)), null);
importsToAdd.add(EnumMap.class.getCanonicalName());
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ private void replaceIfNoElseStatement(final IfStatement outerIf, final IfStateme
ASTNodeFactory ast= cuRewrite.getASTBuilder();
ASTRewrite rewrite= cuRewrite.getASTRewrite();

InfixExpression ie= ast.infixExpression(ast.parenthesizeIfNeeded(rewrite.createMoveTarget(outerIf.getExpression())), InfixExpression.Operator.CONDITIONAL_AND,
ast.parenthesizeIfNeeded(rewrite.createMoveTarget(innerIf.getExpression())));
InfixExpression ie= ast.infixExpression(ast.parenthesizeIfNeeded(ASTNodes.createMoveTarget(rewrite, outerIf.getExpression())), InfixExpression.Operator.CONDITIONAL_AND,
ast.parenthesizeIfNeeded(ASTNodes.createMoveTarget(rewrite, innerIf.getExpression())));
rewrite.replace(innerIf.getExpression(), ie, null);
rewrite.replace(outerIf, rewrite.createMoveTarget(innerIf), null);
rewrite.replace(outerIf, ASTNodes.createMoveTarget(rewrite, innerIf), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public boolean visit(final NormalAnnotation node) {

List<MemberValuePair> values= ASTNodes.values(node);
if (values.isEmpty()) {
rewrite.replace(node, ast.markerAnnotation(rewrite.createMoveTarget(node.getTypeName())), null);
rewrite.replace(node, ast.markerAnnotation(ASTNodes.createMoveTarget(rewrite, node.getTypeName())), null);
return false;
}
if (values.size() == 1) {
MemberValuePair pair= values.get(0);
if ("value".equals(pair.getName().getIdentifier())) { //$NON-NLS-1$
rewrite.replace(node, ast.singleValueAnnotation(rewrite.createMoveTarget(node.getTypeName()), rewrite.createMoveTarget(pair.getValue())), null);
rewrite.replace(node, ast.singleValueAnnotation(ASTNodes.createMoveTarget(rewrite, node.getTypeName()), ASTNodes.createMoveTarget(rewrite, pair.getValue())), null);
return false;
}
}
Expand All @@ -105,7 +105,7 @@ public boolean visit(final NormalAnnotation node) {
ArrayInitializer arrayInit= (ArrayInitializer) pair.getValue();
List<Expression> exprs= ASTNodes.expressions(arrayInit);
if (exprs.size() == 1) {
rewrite.replace(arrayInit, rewrite.createMoveTarget(exprs.get(0)), null);
rewrite.replace(arrayInit, ASTNodes.createMoveTarget(rewrite, exprs.get(0)), null);
result= false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected MethodInvocation invokeMethod(final Set<String> classesToUseWithImport
qualifiedClassName= ASSERTIONS_CLASS;
qualifiedClass= null;
} else if (originalMethod.getExpression() != null) {
qualifiedClass= cuRewrite.getASTRewrite().createMoveTarget(originalMethod.getExpression());
qualifiedClass= ASTNodes.createMoveTarget(cuRewrite.getASTRewrite(), originalMethod.getExpression());
} else {
qualifiedClass= null;
}
Expand All @@ -268,7 +268,7 @@ private MethodInvocation invokeFail(final Expression failureMessage, final Expre
List<Expression> copyOfMessages= new ArrayList<>(failureMethod.arguments().size());

for (Object message : failureMethod.arguments()) {
copyOfMessages.add(cuRewrite.getASTRewrite().createMoveTarget(ASTNodes.getUnparenthesedExpression((Expression) message)));
copyOfMessages.add(ASTNodes.createMoveTarget(cuRewrite.getASTRewrite(), ASTNodes.getUnparenthesedExpression((Expression) message)));
}

return ast.newMethodInvocation(qualifiedClass, FAIL_METHOD, copyOfMessages);
Expand All @@ -293,7 +293,7 @@ protected MethodInvocation invokeQualifiedMethod(final Set<String> classesToUseW
List<Expression> copyOfMessages= new ArrayList<>(failureMethod.arguments().size());

for (Object message : failureMethod.arguments()) {
copyOfMessages.add(cuRewrite.getASTRewrite().createMoveTarget(ASTNodes.getUnparenthesedExpression((Expression) message)));
copyOfMessages.add(ASTNodes.createMoveTarget(cuRewrite.getASTRewrite(), ASTNodes.getUnparenthesedExpression((Expression) message)));
}

assertionMethod= ast.newMethodInvocation(assertionMethod, DESCRIBED_AS_METHOD.equals(method) ? method : AS_METHOD, copyOfMessages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void replaceWithStraightAssign(final IfStatement node, final Expression
ASTNodeFactory ast= cuRewrite.getASTBuilder();
ASTRewrite rewrite= cuRewrite.getASTRewrite();
rewrite.replace(node,
ast.toStatement(ast.assign(rewrite.createMoveTarget(leftHandSide), Assignment.Operator.ASSIGN, rewrite.createMoveTarget(rightHandSide))), null);
ast.toStatement(ast.assign(ASTNodes.createMoveTarget(rewrite, leftHandSide), Assignment.Operator.ASSIGN, ASTNodes.createMoveTarget(rewrite, rightHandSide))), null);
}

private boolean maybeReplaceWithStraightReturn(final IfStatement node, final InfixExpression condition, final ReturnStatement valuedReturn,
Expand Down Expand Up @@ -209,7 +209,7 @@ private void replaceWithStraightReturn(final IfStatement node, final Expression
ASTRewrite rewrite= cuRewrite.getASTRewrite();

rewrite.remove(toRemove, null);
rewrite.replace(node, ast.return0(rewrite.createMoveTarget(returnedExpression)), null);
rewrite.replace(node, ast.return0(ASTNodes.createMoveTarget(rewrite, returnedExpression)), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ private void replaceWithValue(final ConditionalExpression node, final Expression
ASTNodeFactory ast= cuRewrite.getASTBuilder();
ASTRewrite rewrite= cuRewrite.getASTRewrite();

rewrite.replace(node, ast.parenthesizeIfNeeded(rewrite.createMoveTarget(valuedExpression)), null);
rewrite.replace(node, ast.parenthesizeIfNeeded(ASTNodes.createMoveTarget(rewrite, valuedExpression)), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ private void useAutoBoxing(final MethodInvocation node, final ITypeBinding primi
if (primitiveType != null && !primitiveType.equals(actualParameterType)
&& !primitiveType.equals(actualResultType)
&& (wrapperClass == null || !wrapperClass.equals(actualParameterType))) {
cuRewrite.getASTRewrite().replace(node, ast.cast(ast.type(primitiveType.getName()), rewrite.createMoveTarget(ASTNodes.arguments(node).get(0))), null);
cuRewrite.getASTRewrite().replace(node, ast.cast(ast.type(primitiveType.getName()), ASTNodes.createMoveTarget(rewrite, ASTNodes.arguments(node).get(0))), null);
} else {
cuRewrite.getASTRewrite().replace(node, rewrite.createMoveTarget(ASTNodes.arguments(node).get(0)), null);
cuRewrite.getASTRewrite().replace(node, ASTNodes.createMoveTarget(rewrite, ASTNodes.arguments(node).get(0)), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private ASTNode getClassInstanceCreatorNode(final Expression expression, final S
private InfixExpression getCompareToNode(final boolean isPositive, final MethodInvocation node) {
ASTNodeFactory ast= cuRewrite.getASTBuilder();
ASTRewrite rewrite= cuRewrite.getASTRewrite();
MethodInvocation mi= ast.newMethodInvocation(rewrite.createMoveTarget(node.getExpression()), "compareTo", rewrite.createMoveTarget(ASTNodes.arguments(node).get(0))); //$NON-NLS-1$
MethodInvocation mi= ast.newMethodInvocation(ASTNodes.createMoveTarget(rewrite, node.getExpression()), "compareTo", ASTNodes.createMoveTarget(rewrite, ASTNodes.arguments(node).get(0))); //$NON-NLS-1$

return ast.infixExpression(mi, isPositive ? InfixExpression.Operator.EQUALS : InfixExpression.Operator.NOT_EQUALS, ast.int0(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void useConstant(final MethodInvocation node, final BooleanLiteral liter
Name expression= ASTNodes.as(node.getExpression(), Name.class);

if (expression != null) {
fa.setExpression(rewrite.createMoveTarget(expression));
fa.setExpression(ASTNodes.createMoveTarget(rewrite, expression));
}

fa.setName(ast.simpleName(literal.booleanValue() ? "TRUE" : "FALSE")); //$NON-NLS-1$ //$NON-NLS-2$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void replaceNullCheck(final InfixExpression node, final Expression first
ASTRewrite rewrite= cuRewrite.getASTRewrite();

Name booleanConstant= ast.name(Boolean.class.getSimpleName(), isAndExpression == isPositiveExpression ? "TRUE" : "FALSE"); //$NON-NLS-1$ //$NON-NLS-2$
MethodInvocation equalsMethod= ast.newMethodInvocation(booleanConstant, "equals", rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(firstExpression))); //$NON-NLS-1$
MethodInvocation equalsMethod= ast.newMethodInvocation(booleanConstant, "equals", ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(firstExpression))); //$NON-NLS-1$

Expression newExpression= null;
if (!isNullCheck || isAndExpression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private void addBreak(final IfStatement ifStatement, final List<Statement> assig
if (ifStatement.getThenStatement() instanceof Block) {
rewrite.insertAfter(ast.break0(), Utils.getLast(assignments), null);
} else {
rewrite.replace(ifStatement.getThenStatement(), ast.block(rewrite.createMoveTarget(ifStatement.getThenStatement()), ast.break0()), null);
rewrite.replace(ifStatement.getThenStatement(), ast.block(ASTNodes.createMoveTarget(rewrite, ifStatement.getThenStatement()), ast.break0()), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private boolean maybeReplaceInitializer(final Expression nodeToReplace, final Ex
ASTNodeFactory ast= cuRewrite.getASTBuilder();
ASTRewrite rewrite= cuRewrite.getASTRewrite();

rewrite.replace(nodeToReplace, ast.new0(rewrite.createMoveTarget(cic.getType()), rewrite.createMoveTarget(arg0)), null);
rewrite.replace(nodeToReplace, ast.new0(ASTNodes.createMoveTarget(rewrite, cic.getType()), ASTNodes.createMoveTarget(rewrite, arg0)), null);
rewrite.remove(nodeToRemove, null);
setResult(false);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ private void refactorMethod(final MethodInvocation node, final MethodInvocation
String collectionsName= addImport(Collections.class, classesToUseWithImport, importsToAdd);

List<Expression> copyOfArguments= new ArrayList<>(asListMethod.arguments().size() + 1);
copyOfArguments.add(rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression(node.getExpression())));
copyOfArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())));

for (Object argument : asListMethod.arguments()) {
copyOfArguments.add(rewrite.createMoveTarget(ASTNodes.getUnparenthesedExpression((Expression) argument)));
copyOfArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) argument)));
}

MethodInvocation newCollectionsAddAllMethod= ast.newMethodInvocation(ast.name(collectionsName), ADD_ALL_METHOD, copyOfArguments);
Expand Down
Loading

0 comments on commit 90aac00

Please sign in to comment.