use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class BooleanRefactoring method visitIfStatement.
private boolean visitIfStatement(final IfStatement node) {
final BooleanASTMatcher matcher = new BooleanASTMatcher();
if (match(matcher, node.getThenStatement(), node.getElseStatement())) {
// Then and else statement are matching, bar the boolean values
// which are opposite
final Statement copyStmt = b.copySubtree(node.getThenStatement());
// identify the node that need to be replaced after the copy
final BooleanASTMatcher matcher2 = new BooleanASTMatcher(matcher.matches);
if (match(matcher2, copyStmt, node.getElseStatement())) {
final Expression ifCondition = node.getExpression();
copyStmt.accept(new BooleanReplaceVisitor(ifCondition, matcher2.matches.values(), getBooleanName(node)));
// make sure to keep curly braces if the node is an else statement
ctx.getRefactorings().replace(node, isElseStatementOfParent(node) ? copyStmt : toSingleStmt(copyStmt));
return DO_NOT_VISIT_SUBTREE;
}
}
final ReturnStatement thenRs = as(node.getThenStatement(), ReturnStatement.class);
if (thenRs != null) {
final ReturnStatement elseRs = as(node.getElseStatement() != null ? node.getElseStatement() : getNextSibling(node), ReturnStatement.class);
if (elseRs != null) {
return withThenReturnStmt(node, thenRs, elseRs);
}
return VISIT_SUBTREE;
} else {
return noThenReturnStmt(node);
}
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class BooleanRefactoring method noThenReturnStmt.
private boolean noThenReturnStmt(final IfStatement node) {
final Assignment thenA = asExpression(node.getThenStatement(), Assignment.class);
if (hasOperator(thenA, ASSIGN) && asList(node.getElseStatement()).isEmpty() && (thenA.getLeftHandSide() instanceof Name || thenA.getLeftHandSide() instanceof FieldAccess)) {
final Statement previousSibling = getPreviousSibling(node);
if (previousSibling instanceof VariableDeclarationStatement) {
final VariableDeclarationStatement vds = (VariableDeclarationStatement) previousSibling;
VariableDeclarationFragment vdf = getVariableDeclarationFragment(vds, thenA.getLeftHandSide());
if (vdf != null) {
final VariableDefinitionsUsesVisitor variableUseVisitor = new VariableDefinitionsUsesVisitor(vdf.resolveBinding(), node.getExpression()).find();
if (variableUseVisitor.getUses().isEmpty()) {
final ITypeBinding typeBinding = vds.getType().resolveBinding();
return maybeReplace(node, thenA, typeBinding, vdf.getInitializer());
}
}
} else if (previousSibling instanceof ExpressionStatement) {
final Assignment elseA = asExpression(previousSibling, Assignment.class);
if (hasOperator(elseA, ASSIGN) && isSameVariable(thenA.getLeftHandSide(), elseA.getLeftHandSide())) {
final ITypeBinding typeBinding = elseA.resolveTypeBinding();
return maybeReplace(node, thenA, typeBinding, elseA.getRightHandSide());
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class CommonCodeInIfElseStatementRefactoring method collectAllCases.
/**
* Collects all cases (if/else, if/else if/else, etc.) and returns whether all are covered.
*
* @param allCases the output collection for all the cases
* @param node the {@link IfStatement} to examine
* @return true if all cases (if/else, if/else if/else, etc.) are covered,
* false otherwise
*/
private boolean collectAllCases(List<List<Statement>> allCases, IfStatement node) {
final List<Statement> thenStmts = asList(node.getThenStatement());
final List<Statement> elseStmts = asList(node.getElseStatement());
if (thenStmts.isEmpty() || elseStmts.isEmpty()) {
// let other refactorings take care of removing empty blocks.
return false;
}
allCases.add(thenStmts);
if (elseStmts.size() == 1) {
final IfStatement is = as(elseStmts.get(0), IfStatement.class);
if (is != null) {
return collectAllCases(allCases, is);
}
}
allCases.add(elseStmts);
return true;
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class ASTBuilder method if0.
/**
* 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 if0(Expression condition, Statement thenStatement, Statement elseStatement) {
final IfStatement is = ast.newIfStatement();
is.setExpression(condition);
is.setThenStatement(thenStatement);
is.setElseStatement(elseStatement);
return is;
}
use of org.eclipse.jdt.core.dom.IfStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getSplitOrConditionProposals.
public static boolean getSplitOrConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
// check that user invokes quick assist on infix expression
if (!(node instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression = (InfixExpression) node;
if (infixExpression.getOperator() != orOperator) {
return false;
}
int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
if (offset == -1) {
return false;
}
// check that infix expression belongs to IfStatement
Statement statement = ASTResolving.findParentStatement(node);
if (!(statement instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) statement;
// check that infix expression is part of first level || condition of IfStatement
InfixExpression topInfixExpression = infixExpression;
while (topInfixExpression.getParent() instanceof InfixExpression && ((InfixExpression) topInfixExpression.getParent()).getOperator() == orOperator) {
topInfixExpression = (InfixExpression) topInfixExpression.getParent();
}
if (ifStatement.getExpression() != topInfixExpression) {
return false;
}
//
if (resultingCollections == null) {
return true;
}
AST ast = ifStatement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare left and right conditions
Expression[] newOperands = { null, null };
breakInfixOperationAtOperation(rewrite, topInfixExpression, orOperator, offset, true, newOperands);
Expression leftCondition = newOperands[0];
Expression rightCondition = newOperands[1];
// prepare first statement
rewrite.replace(ifStatement.getExpression(), leftCondition, null);
IfStatement secondIf = ast.newIfStatement();
secondIf.setExpression(rightCondition);
secondIf.setThenStatement((Statement) rewrite.createCopyTarget(ifStatement.getThenStatement()));
Statement elseStatement = ifStatement.getElseStatement();
if (elseStatement == null) {
rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, secondIf, null);
} else {
rewrite.replace(elseStatement, secondIf, null);
secondIf.setElseStatement((Statement) rewrite.createMoveTarget(elseStatement));
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitOrCondition_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_OR_CONDITION);
resultingCollections.add(proposal);
return true;
}
Aggregations