Skip to content

Commit

Permalink
Create if like Eclipse
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice Tiercelin committed Oct 10, 2020
1 parent 5106e44 commit cc358af
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,26 @@ public List<SingleVariableDeclaration> parameters(final SingleVariableDeclaratio
/**
* Builds a new {@link Assignment} instance.
*
* @param lhs the left hand side expression
* @return a new Assignment
*/
public Assignment newAssignment() {
return ast.newAssignment();
}

/**
* Builds a new {@link Assignment} instance.
*
* @param leftHandSide the left hand side expression
* @param operator the assignment operator
* @param rhs the right hand side expression
* @return a new Block
* @param rightHandSide the right hand side expression
* @return a new Assignment
*/
public Assignment newAssignment(final Expression lhs, final Assignment.Operator operator, final Expression rhs) {
Assignment assign= ast.newAssignment();
assign.setLeftHandSide(lhs);
assign.setOperator(operator);
assign.setRightHandSide(rhs);
return assign;
public Assignment newAssignment(final Expression leftHandSide, final Assignment.Operator operator, final Expression rightHandSide) {
Assignment newAssignment= newAssignment();
newAssignment.setLeftHandSide(leftHandSide);
newAssignment.setOperator(operator);
newAssignment.setRightHandSide(rightHandSide);
return newAssignment;
}

/**
Expand Down Expand Up @@ -221,6 +230,15 @@ public SwitchCase newSwitchCase(final Expression expression) {
return sc;
}

/**
* Builds a new {@link CastExpression} instance.
*
* @return a new CastExpression
*/
public CastExpression newCastExpression() {
return ast.newCastExpression();
}

/**
* Builds a new {@link CastExpression} instance.
*
Expand All @@ -229,10 +247,10 @@ public SwitchCase newSwitchCase(final Expression expression) {
* @return a new CastExpression
*/
public CastExpression newCastExpression(final Type type, final Expression expression) {
CastExpression ce= ast.newCastExpression();
ce.setType(type);
ce.setExpression(parenthesizeIfNeeded(expression));
return ce;
CastExpression newCastExpression= newCastExpression();
newCastExpression.setType(type);
newCastExpression.setExpression(parenthesizeIfNeeded(expression));
return newCastExpression;
}

/**
Expand Down Expand Up @@ -663,17 +681,6 @@ public SingleVariableDeclaration newSingleVariableDeclaration(final String varNa
return svd;
}

/**
* Builds a new {@link IfStatement} instance.
*
* @param condition the if condition
* @param thenStatement the then statement
* @return a new if statement
*/
public IfStatement newIfStatement(final Expression condition, final Statement thenStatement) {
return newIfStatement(condition, thenStatement, null);
}

/**
* Builds a new {@link DoStatement} instance.
*
Expand All @@ -691,17 +698,10 @@ public DoStatement newDoStatement(final Expression condition, final Statement st
/**
* Builds a new {@link IfStatement} instance.
*
* @param condition the if condition
* @param thenStatement the statement of the then clause
* @param elseStatement the statement of the else clause
* @return a new if statement
*/
public IfStatement newIfStatement(final Expression condition, final Statement thenStatement, final Statement elseStatement) {
IfStatement is= ast.newIfStatement();
is.setExpression(condition);
is.setThenStatement(thenStatement);
is.setElseStatement(elseStatement);
return is;
public IfStatement newIfStatement() {
return ast.newIfStatement();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ public AssignmentForAndReturnVisitor(final Set<String> classesToUseWithImport, f
@Override
public boolean visit(final EnhancedForStatement node) {
SingleVariableDeclaration loopVariable= node.getParameter();
IfStatement is= uniqueStmtAs(node.getBody(), IfStatement.class);
return maybeReplaceWithCollectionContains(node, node.getExpression(), loopVariable.getName(), is);
IfStatement ifStatement= uniqueStmtAs(node.getBody(), IfStatement.class);
return maybeReplaceWithCollectionContains(node, node.getExpression(), loopVariable.getName(), ifStatement);
}

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

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

Statement forNextStatement= ASTNodes.getNextStatement(forNode);
BooleanLiteral outerBl= getReturnedBooleanLiteral(forNextStatement);
BooleanLiteral outerBooleanLiteral= getReturnedBooleanLiteral(forNextStatement);

Boolean isPositive= signCollectionContains(innerBl, outerBl);
Boolean isPositive= signCollectionContains(innerBooleanLiteral, outerBooleanLiteral);

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

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

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

replaceLoopByIf(forNode, iterable, thenStatements, toFind, bs);
replaceLoopByIf(forNode, iterable, thenStatements, toFind, breakStatement);
result= false;
return false;
}
Expand Down Expand Up @@ -231,8 +231,10 @@ private void replaceLoopByIf(final Statement forNode, final Expression iterable,
thenStatements.remove(thenStatements.size() - 1);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ public boolean visit(final MethodDeclaration node) {
Variable convertViewVar= new Variable(viewArg.getName().getIdentifier(), ast);
InfixExpression condition= ast.newInfixExpression(convertViewVar.varName(), InfixExpression.Operator.EQUALS, ast.newNullLiteral());
Block thenBlock= ast.newBlock();
IfStatement ifStatement= ast.newIfStatement(condition, thenBlock);
rewrite.insertBefore(ifStatement, visitor.viewAssignmentStatement, group);
IfStatement newIfStatement= ast.newIfStatement();
newIfStatement.setExpression(condition);
newIfStatement.setThenStatement(thenBlock);
rewrite.insertBefore(newIfStatement, visitor.viewAssignmentStatement, group);
List<Statement> thenStatements= thenBlock.statements();

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

// Retrieve viewHolderItem from convertView
ifStatement.setElseStatement(ast.newBlock(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN,
newIfStatement.setElseStatement(ast.newBlock(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN,
ast.newCastExpression(viewHolderItemVar.type(), ast.newMethodInvocation("convertView", "getTag")))))); //$NON-NLS-1$ //$NON-NLS-2$
}
rewrite.remove(visitor.viewAssignmentStatement, group);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Statement;
Expand Down Expand Up @@ -109,8 +110,10 @@ public boolean visit(final MethodInvocation node) {
private Statement createWakelockReleaseStatement(final MethodInvocation methodInvocation) {
ASTNodeFactory ast= cuRewrite.getASTBuilder();

return ast.newIfStatement(ast.not(ast.newMethodInvocation(ast.copyExpression(methodInvocation), "isHeld")), //$NON-NLS-1$
ast.newBlock(ast.newExpressionStatement(ast.newMethodInvocation(ast.copyExpression(methodInvocation), "release")))); //$NON-NLS-1$
IfStatement newIfStatement= ast.newIfStatement();
newIfStatement.setExpression(ast.not(ast.newMethodInvocation(ast.copyExpression(methodInvocation), "isHeld"))); //$NON-NLS-1$
newIfStatement.setThenStatement(ast.newBlock(ast.newExpressionStatement(ast.newMethodInvocation(ast.copyExpression(methodInvocation), "release")))); //$NON-NLS-1$
return newIfStatement;
}

private MethodDeclaration createOnPauseMethodDeclaration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ private void removeIdenticalTrailingCode(final IfStatement node, final List<ASTN
if (i == areCasesRemovable.length - 2 && !areCasesRemovable[i + 1]) {
// Then clause is empty and there is only one else clause
// => revert if statement
ASTNodes.replaceButKeepComment(rewrite, parent, ast.newIfStatement(ast.negate(((IfStatement) parent).getExpression(), true), ASTNodes.createMoveTarget(rewrite, ((IfStatement) parent).getElseStatement())), group);
IfStatement newIfStatement= ast.newIfStatement();
newIfStatement.setExpression(ast.negate(((IfStatement) parent).getExpression(), true));
newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, ((IfStatement) parent).getElseStatement()));
ASTNodes.replaceButKeepComment(rewrite, parent, newIfStatement, group);
break;
}

if (allRemovable(areCasesRemovable, i)) {
rewrite.remove(parent, group);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ private void refactorIf(final IfStatement node, final IfStatement thenInnerIfSta
ASTNodeFactory ast= cuRewrite.getASTBuilder();
TextEditGroup group= new TextEditGroup(MultiFixMessages.CommonIfInIfElseCleanUp_description);

IfStatement newInnerIf= ast.newIfStatement();
newInnerIf.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())));
newInnerIf.setThenStatement(ASTNodes.createMoveTarget(rewrite, thenInnerIfStatement.getThenStatement()));
newInnerIf.setElseStatement(ASTNodes.createMoveTarget(rewrite, elseInnerIfStatement.getThenStatement()));

Expression newCondition= ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(thenInnerIfStatement.getExpression()));
IfStatement newInnerIf= ast.newIfStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())),
ASTNodes.createMoveTarget(rewrite, thenInnerIfStatement.getThenStatement()), ASTNodes.createMoveTarget(rewrite, elseInnerIfStatement.getThenStatement()));
ASTNodes.replaceButKeepComment(rewrite, node, ast.newIfStatement(newCondition, ast.newBlock(newInnerIf)), group);
IfStatement newIfStatement= ast.newIfStatement();
newIfStatement.setExpression(newCondition);
newIfStatement.setThenStatement(ast.newBlock(newInnerIf));

ASTNodes.replaceButKeepComment(rewrite, node, newIfStatement, group);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BreakStatement;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Statement;
Expand Down Expand Up @@ -193,11 +194,18 @@ private void replaceSwitch(final SwitchStatement node,
Block newBlock= ast.newBlock(copyOfStatements);

if (currentBlock != null) {
currentBlock= ast.newIfStatement(newCondition, newBlock, currentBlock);
IfStatement newIfStatement= ast.newIfStatement();
newIfStatement.setExpression(newCondition);
newIfStatement.setThenStatement(newBlock);
newIfStatement.setElseStatement(currentBlock);
currentBlock= newIfStatement;
} else if (copyOfStatements.length == 0) {
localCaseIndexWithDefault= -1;
localCaseIndexWithDefault = -1;
} else if (localCaseIndexWithDefault == -1) {
currentBlock= ast.newIfStatement(newCondition, newBlock);
IfStatement newIfStatement= ast.newIfStatement();
newIfStatement.setExpression(newCondition);
newIfStatement.setThenStatement(newBlock);
currentBlock= newIfStatement;
} else {
currentBlock= newBlock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ private void replaceByIf(final WhileStatement node, final BreakVisitor breakVisi
}
}

ASTNodes.replaceButKeepComment(rewrite, node, ast.newIfStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())), ASTNodes.createMoveTarget(rewrite, node.getBody())), group);
IfStatement newIfStatement= ast.newIfStatement();
newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node.getExpression())));
newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, node.getBody()));

ASTNodes.replaceButKeepComment(rewrite, node, newIfStatement, group);
}

private static class BreakVisitor extends InterruptibleVisitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ private void refactorCondition(final IfStatement node, final Expression duplicat

ASTNodes.replaceButKeepComment(rewrite, node.getExpression(), ast.negate(duplicateExpression, true), group);
ASTNodes.replaceButKeepComment(rewrite, node.getThenStatement(), negativeStmtCopy, group);
ASTNodes.replaceButKeepComment(rewrite, node.getElseStatement(), ast.newIfStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondCond)), secondStmtCopy, thirdStmtCopy), group);
IfStatement newIfStatement= ast.newIfStatement();
newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondCond)));
newIfStatement.setThenStatement(secondStmtCopy);
newIfStatement.setElseStatement(thirdStmtCopy);
ASTNodes.replaceButKeepComment(rewrite, node.getElseStatement(), newIfStatement, group);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
Expand Down Expand Up @@ -717,7 +718,10 @@ private Expression getTypedExpression(final Pair<ITypeBinding, Expression> typeA

Expression expression= null;
if (typeAndValue.getFirst() != null) {
expression= ast.newCastExpression(ast.type(typeAndValue.getFirst().getQualifiedName()), ast.createCopyTarget(typeAndValue.getSecond()));
CastExpression newCastExpression= ast.newCastExpression();
newCastExpression.setType(ast.type(typeAndValue.getFirst().getQualifiedName()));
newCastExpression.setExpression(ast.parenthesizeIfNeeded(ast.createCopyTarget(typeAndValue.getSecond())));
expression= newCastExpression;
} else if (typeAndValue.getFirst() == null) {
expression= ast.createCopyTarget(typeAndValue.getSecond());
}
Expand Down

0 comments on commit cc358af

Please sign in to comment.