Skip to content

Commit a1b70c6

Browse files
author
Fabrice Tiercelin
committed
Create if like Eclipse
1 parent 5106e44 commit a1b70c6

10 files changed

+101
-63
lines changed

plugin/src/main/java/org/autorefactor/jdt/internal/corext/dom/ASTNodeFactory.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,26 @@ public List<SingleVariableDeclaration> parameters(final SingleVariableDeclaratio
152152
/**
153153
* Builds a new {@link Assignment} instance.
154154
*
155-
* @param lhs the left hand side expression
155+
* @return a new Assignment
156+
*/
157+
public Assignment newAssignment() {
158+
return ast.newAssignment();
159+
}
160+
161+
/**
162+
* Builds a new {@link Assignment} instance.
163+
*
164+
* @param leftHandSide the left hand side expression
156165
* @param operator the assignment operator
157-
* @param rhs the right hand side expression
158-
* @return a new Block
166+
* @param rightHandSide the right hand side expression
167+
* @return a new Assignment
159168
*/
160-
public Assignment newAssignment(final Expression lhs, final Assignment.Operator operator, final Expression rhs) {
161-
Assignment assign= ast.newAssignment();
162-
assign.setLeftHandSide(lhs);
163-
assign.setOperator(operator);
164-
assign.setRightHandSide(rhs);
165-
return assign;
169+
public Assignment newAssignment(final Expression leftHandSide, final Assignment.Operator operator, final Expression rightHandSide) {
170+
Assignment newAssignment= newAssignment();
171+
newAssignment.setLeftHandSide(leftHandSide);
172+
newAssignment.setOperator(operator);
173+
newAssignment.setRightHandSide(rightHandSide);
174+
return newAssignment;
166175
}
167176

168177
/**
@@ -221,6 +230,15 @@ public SwitchCase newSwitchCase(final Expression expression) {
221230
return sc;
222231
}
223232

233+
/**
234+
* Builds a new {@link CastExpression} instance.
235+
*
236+
* @return a new CastExpression
237+
*/
238+
public CastExpression newCastExpression() {
239+
return ast.newCastExpression();
240+
}
241+
224242
/**
225243
* Builds a new {@link CastExpression} instance.
226244
*
@@ -229,10 +247,10 @@ public SwitchCase newSwitchCase(final Expression expression) {
229247
* @return a new CastExpression
230248
*/
231249
public CastExpression newCastExpression(final Type type, final Expression expression) {
232-
CastExpression ce= ast.newCastExpression();
233-
ce.setType(type);
234-
ce.setExpression(parenthesizeIfNeeded(expression));
235-
return ce;
250+
CastExpression newCastExpression= newCastExpression();
251+
newCastExpression.setType(type);
252+
newCastExpression.setExpression(parenthesizeIfNeeded(expression));
253+
return newCastExpression;
236254
}
237255

238256
/**
@@ -663,17 +681,6 @@ public SingleVariableDeclaration newSingleVariableDeclaration(final String varNa
663681
return svd;
664682
}
665683

666-
/**
667-
* Builds a new {@link IfStatement} instance.
668-
*
669-
* @param condition the if condition
670-
* @param thenStatement the then statement
671-
* @return a new if statement
672-
*/
673-
public IfStatement newIfStatement(final Expression condition, final Statement thenStatement) {
674-
return newIfStatement(condition, thenStatement, null);
675-
}
676-
677684
/**
678685
* Builds a new {@link DoStatement} instance.
679686
*
@@ -691,17 +698,10 @@ public DoStatement newDoStatement(final Expression condition, final Statement st
691698
/**
692699
* Builds a new {@link IfStatement} instance.
693700
*
694-
* @param condition the if condition
695-
* @param thenStatement the statement of the then clause
696-
* @param elseStatement the statement of the else clause
697701
* @return a new if statement
698702
*/
699-
public IfStatement newIfStatement(final Expression condition, final Statement thenStatement, final Statement elseStatement) {
700-
IfStatement is= ast.newIfStatement();
701-
is.setExpression(condition);
702-
is.setThenStatement(thenStatement);
703-
is.setElseStatement(elseStatement);
704-
return is;
703+
public IfStatement newIfStatement() {
704+
return ast.newIfStatement();
705705
}
706706

707707
/**

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/AbstractCollectionMethodRatherThanLoopCleanUp.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ public AssignmentForAndReturnVisitor(final Set<String> classesToUseWithImport, f
137137
@Override
138138
public boolean visit(final EnhancedForStatement node) {
139139
SingleVariableDeclaration loopVariable= node.getParameter();
140-
IfStatement is= uniqueStmtAs(node.getBody(), IfStatement.class);
141-
return maybeReplaceWithCollectionContains(node, node.getExpression(), loopVariable.getName(), is);
140+
IfStatement ifStatement= uniqueStmtAs(node.getBody(), IfStatement.class);
141+
return maybeReplaceWithCollectionContains(node, node.getExpression(), loopVariable.getName(), ifStatement);
142142
}
143143

144144
private boolean maybeReplaceWithCollectionContains(final Statement forNode, final Expression iterable,
145-
final Expression loopElement, final IfStatement is) {
146-
if (result && is != null && is.getElseStatement() == null && ASTNodes.instanceOf(iterable, Collection.class.getCanonicalName())) {
147-
MethodInvocation condition= getMethodToReplace(is.getExpression());
148-
List<Statement> thenStatements= ASTNodes.asList(is.getThenStatement());
145+
final Expression loopElement, final IfStatement ifStatement) {
146+
if (result && ifStatement != null && ifStatement.getElseStatement() == null && ASTNodes.instanceOf(iterable, Collection.class.getCanonicalName())) {
147+
MethodInvocation condition= getMethodToReplace(ifStatement.getExpression());
148+
List<Statement> thenStatements= ASTNodes.asList(ifStatement.getThenStatement());
149149

150150
if (!thenStatements.isEmpty()
151151
&& condition != null) {
@@ -154,12 +154,12 @@ private boolean maybeReplaceWithCollectionContains(final Statement forNode, fina
154154
if (toFind != null) {
155155
if (thenStatements.size() == 1) {
156156
Statement thenStatement= thenStatements.get(0);
157-
BooleanLiteral innerBl= getReturnedBooleanLiteral(thenStatement);
157+
BooleanLiteral innerBooleanLiteral= getReturnedBooleanLiteral(thenStatement);
158158

159159
Statement forNextStatement= ASTNodes.getNextStatement(forNode);
160-
BooleanLiteral outerBl= getReturnedBooleanLiteral(forNextStatement);
160+
BooleanLiteral outerBooleanLiteral= getReturnedBooleanLiteral(forNextStatement);
161161

162-
Boolean isPositive= signCollectionContains(innerBl, outerBl);
162+
Boolean isPositive= signCollectionContains(innerBooleanLiteral, outerBooleanLiteral);
163163

164164
if (isPositive != null) {
165165
replaceLoopAndReturn(forNode, iterable, toFind, forNextStatement, isPositive);
@@ -170,9 +170,9 @@ private boolean maybeReplaceWithCollectionContains(final Statement forNode, fina
170170
return maybeReplaceLoopAndVariable(forNode, iterable, thenStatement, toFind);
171171
}
172172

173-
BreakStatement bs= ASTNodes.as(thenStatements.get(thenStatements.size() - 1), BreakStatement.class);
173+
BreakStatement breakStatement= ASTNodes.as(thenStatements.get(thenStatements.size() - 1), BreakStatement.class);
174174

175-
if (bs != null && bs.getLabel() == null) {
175+
if (breakStatement != null && breakStatement.getLabel() == null) {
176176
if (thenStatements.size() == 2 && !maybeReplaceLoopAndVariable(forNode, iterable,
177177
thenStatements.get(0), toFind)) {
178178
return false;
@@ -183,7 +183,7 @@ private boolean maybeReplaceWithCollectionContains(final Statement forNode, fina
183183
return true;
184184
}
185185

186-
replaceLoopByIf(forNode, iterable, thenStatements, toFind, bs);
186+
replaceLoopByIf(forNode, iterable, thenStatements, toFind, breakStatement);
187187
result= false;
188188
return false;
189189
}
@@ -231,8 +231,10 @@ private void replaceLoopByIf(final Statement forNode, final Expression iterable,
231231
thenStatements.remove(thenStatements.size() - 1);
232232

233233
ASTNodeFactory ast= cuRewrite.getASTBuilder();
234+
IfStatement replacement= ast.newIfStatement();
235+
replacement.setExpression(newMethod(iterable, toFind, true, classesToUseWithImport, importsToAdd));
236+
replacement.setThenStatement(ast.newBlock(ast.copyRange(thenStatements)));
234237

235-
Statement replacement= ast.newIfStatement(newMethod(iterable, toFind, true, classesToUseWithImport, importsToAdd), ast.newBlock(ast.copyRange(thenStatements)));
236238
TextEditGroup group= new TextEditGroup(""); //$NON-NLS-1$
237239
ASTNodes.replaceButKeepComment(cuRewrite.getASTRewrite(), forNode, replacement, group);
238240

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/AndroidViewHolderCleanUp.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ public boolean visit(final MethodDeclaration node) {
114114
Variable convertViewVar= new Variable(viewArg.getName().getIdentifier(), ast);
115115
InfixExpression condition= ast.newInfixExpression(convertViewVar.varName(), InfixExpression.Operator.EQUALS, ast.newNullLiteral());
116116
Block thenBlock= ast.newBlock();
117-
IfStatement ifStatement= ast.newIfStatement(condition, thenBlock);
118-
rewrite.insertBefore(ifStatement, visitor.viewAssignmentStatement, group);
117+
IfStatement newIfStatement= ast.newIfStatement();
118+
newIfStatement.setExpression(condition);
119+
newIfStatement.setThenStatement(thenBlock);
120+
rewrite.insertBefore(newIfStatement, visitor.viewAssignmentStatement, group);
119121
List<Statement> thenStatements= thenBlock.statements();
120122

121123
thenStatements.add(ast.newExpressionStatement(ast.newAssignment(convertViewVar.varName(), Assignment.Operator.ASSIGN, ast.createCopyTarget(visitor.getInflateExpression()))));
@@ -176,7 +178,7 @@ public boolean visit(final MethodDeclaration node) {
176178
thenStatements.add(ast.newExpressionStatement(ast.newMethodInvocation("convertView", "setTag", viewHolderItemVar.varName()))); //$NON-NLS-1$ //$NON-NLS-2$
177179

178180
// Retrieve viewHolderItem from convertView
179-
ifStatement.setElseStatement(ast.newBlock(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN,
181+
newIfStatement.setElseStatement(ast.newBlock(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN,
180182
ast.newCastExpression(viewHolderItemVar.type(), ast.newMethodInvocation("convertView", "getTag")))))); //$NON-NLS-1$ //$NON-NLS-2$
181183
}
182184
rewrite.remove(visitor.viewAssignmentStatement, group);

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/AndroidWakeLockCleanUp.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.eclipse.jdt.core.dom.ASTNode;
3535
import org.eclipse.jdt.core.dom.Block;
3636
import org.eclipse.jdt.core.dom.IMethodBinding;
37+
import org.eclipse.jdt.core.dom.IfStatement;
3738
import org.eclipse.jdt.core.dom.MethodDeclaration;
3839
import org.eclipse.jdt.core.dom.MethodInvocation;
3940
import org.eclipse.jdt.core.dom.Statement;
@@ -109,8 +110,10 @@ public boolean visit(final MethodInvocation node) {
109110
private Statement createWakelockReleaseStatement(final MethodInvocation methodInvocation) {
110111
ASTNodeFactory ast= cuRewrite.getASTBuilder();
111112

112-
return ast.newIfStatement(ast.not(ast.newMethodInvocation(ast.copyExpression(methodInvocation), "isHeld")), //$NON-NLS-1$
113-
ast.newBlock(ast.newExpressionStatement(ast.newMethodInvocation(ast.copyExpression(methodInvocation), "release")))); //$NON-NLS-1$
113+
IfStatement newIfStatement= ast.newIfStatement();
114+
newIfStatement.setExpression(ast.not(ast.newMethodInvocation(ast.copyExpression(methodInvocation), "isHeld"))); //$NON-NLS-1$
115+
newIfStatement.setThenStatement(ast.newBlock(ast.newExpressionStatement(ast.newMethodInvocation(ast.copyExpression(methodInvocation), "release")))); //$NON-NLS-1$
116+
return newIfStatement;
114117
}
115118

116119
private MethodDeclaration createOnPauseMethodDeclaration() {

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/CommonCodeInIfElseStatementCleanUp.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,13 @@ private void removeIdenticalTrailingCode(final IfStatement node, final List<ASTN
208208
if (i == areCasesRemovable.length - 2 && !areCasesRemovable[i + 1]) {
209209
// Then clause is empty and there is only one else clause
210210
// => revert if statement
211-
ASTNodes.replaceButKeepComment(rewrite, parent, ast.newIfStatement(ast.negate(((IfStatement) parent).getExpression(), true), ASTNodes.createMoveTarget(rewrite, ((IfStatement) parent).getElseStatement())), group);
211+
IfStatement newIfStatement= ast.newIfStatement();
212+
newIfStatement.setExpression(ast.negate(((IfStatement) parent).getExpression(), true));
213+
newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, ((IfStatement) parent).getElseStatement()));
214+
ASTNodes.replaceButKeepComment(rewrite, parent, newIfStatement, group);
212215
break;
213216
}
217+
214218
if (allRemovable(areCasesRemovable, i)) {
215219
rewrite.remove(parent, group);
216220
break;

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/CommonIfInIfElseCleanUp.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,16 @@ private void refactorIf(final IfStatement node, final IfStatement thenInnerIfSta
9999
ASTNodeFactory ast= cuRewrite.getASTBuilder();
100100
TextEditGroup group= new TextEditGroup(MultiFixMessages.CommonIfInIfElseCleanUp_description);
101101

102+
IfStatement newInnerIf= ast.newIfStatement();
103+
newInnerIf.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())));
104+
newInnerIf.setThenStatement(ASTNodes.createMoveTarget(rewrite, thenInnerIfStatement.getThenStatement()));
105+
newInnerIf.setElseStatement(ASTNodes.createMoveTarget(rewrite, elseInnerIfStatement.getThenStatement()));
106+
102107
Expression newCondition= ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(thenInnerIfStatement.getExpression()));
103-
IfStatement newInnerIf= ast.newIfStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())),
104-
ASTNodes.createMoveTarget(rewrite, thenInnerIfStatement.getThenStatement()), ASTNodes.createMoveTarget(rewrite, elseInnerIfStatement.getThenStatement()));
105-
ASTNodes.replaceButKeepComment(rewrite, node, ast.newIfStatement(newCondition, ast.newBlock(newInnerIf)), group);
108+
IfStatement newIfStatement= ast.newIfStatement();
109+
newIfStatement.setExpression(newCondition);
110+
newIfStatement.setThenStatement(ast.newBlock(newInnerIf));
111+
112+
ASTNodes.replaceButKeepComment(rewrite, node, newIfStatement, group);
106113
}
107114
}

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/IfRatherThanTwoSwitchCasesCleanUp.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.eclipse.jdt.core.dom.Block;
3939
import org.eclipse.jdt.core.dom.BreakStatement;
4040
import org.eclipse.jdt.core.dom.Expression;
41+
import org.eclipse.jdt.core.dom.IfStatement;
4142
import org.eclipse.jdt.core.dom.InfixExpression;
4243
import org.eclipse.jdt.core.dom.SimpleName;
4344
import org.eclipse.jdt.core.dom.Statement;
@@ -193,13 +194,20 @@ private void replaceSwitch(final SwitchStatement node,
193194
Block newBlock= ast.newBlock(copyOfStatements);
194195

195196
if (currentBlock != null) {
196-
currentBlock= ast.newIfStatement(newCondition, newBlock, currentBlock);
197+
IfStatement newIfStatement= ast.newIfStatement();
198+
newIfStatement.setExpression(newCondition);
199+
newIfStatement.setThenStatement(newBlock);
200+
newIfStatement.setElseStatement(currentBlock);
201+
currentBlock= newIfStatement;
197202
} else if (copyOfStatements.length == 0) {
198-
localCaseIndexWithDefault= -1;
203+
localCaseIndexWithDefault = -1;
199204
} else if (localCaseIndexWithDefault == -1) {
200-
currentBlock= ast.newIfStatement(newCondition, newBlock);
201-
} else {
202-
currentBlock= newBlock;
205+
IfStatement newIfStatement= ast.newIfStatement();
206+
newIfStatement.setExpression(newCondition);
207+
newIfStatement.setThenStatement(newBlock);
208+
currentBlock= newIfStatement;
209+
} else {
210+
currentBlock= newBlock;
203211
}
204212
}
205213

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/IfRatherThanWhileAndFallsThroughCleanUp.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ private void replaceByIf(final WhileStatement node, final BreakVisitor breakVisi
9696
}
9797
}
9898

99-
ASTNodes.replaceButKeepComment(rewrite, node, ast.newIfStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())), ASTNodes.createMoveTarget(rewrite, node.getBody())), group);
99+
IfStatement newIfStatement= ast.newIfStatement();
100+
newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())));
101+
newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, node.getBody()));
102+
103+
ASTNodes.replaceButKeepComment(rewrite, node, newIfStatement, group);
100104
}
101105

102106
private static class BreakVisitor extends InterruptibleVisitor {

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/OppositeConditionRatherThanDuplicateConditionCleanUp.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ private void refactorCondition(final IfStatement node, final Expression duplicat
146146

147147
ASTNodes.replaceButKeepComment(rewrite, node.getExpression(), ast.negate(duplicateExpression, true), group);
148148
ASTNodes.replaceButKeepComment(rewrite, node.getThenStatement(), negativeStmtCopy, group);
149-
ASTNodes.replaceButKeepComment(rewrite, node.getElseStatement(), ast.newIfStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondCond)), secondStmtCopy, thirdStmtCopy), group);
149+
IfStatement newIfStatement= ast.newIfStatement();
150+
newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondCond)));
151+
newIfStatement.setThenStatement(secondStmtCopy);
152+
newIfStatement.setElseStatement(thirdStmtCopy);
153+
ASTNodes.replaceButKeepComment(rewrite, node.getElseStatement(), newIfStatement, group);
150154
}
151155
}

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/StringBuilderCleanUp.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.eclipse.jdt.core.dom.ASTNode;
4545
import org.eclipse.jdt.core.dom.Assignment;
4646
import org.eclipse.jdt.core.dom.Block;
47+
import org.eclipse.jdt.core.dom.CastExpression;
4748
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
4849
import org.eclipse.jdt.core.dom.Expression;
4950
import org.eclipse.jdt.core.dom.ExpressionStatement;
@@ -717,7 +718,10 @@ private Expression getTypedExpression(final Pair<ITypeBinding, Expression> typeA
717718

718719
Expression expression= null;
719720
if (typeAndValue.getFirst() != null) {
720-
expression= ast.newCastExpression(ast.type(typeAndValue.getFirst().getQualifiedName()), ast.createCopyTarget(typeAndValue.getSecond()));
721+
CastExpression newCastExpression= ast.newCastExpression();
722+
newCastExpression.setType(ast.type(typeAndValue.getFirst().getQualifiedName()));
723+
newCastExpression.setExpression(ast.parenthesizeIfNeeded(ast.createCopyTarget(typeAndValue.getSecond())));
724+
expression= newCastExpression;
721725
} else if (typeAndValue.getFirst() == null) {
722726
expression= ast.createCopyTarget(typeAndValue.getSecond());
723727
}

0 commit comments

Comments
 (0)