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 CleanCodeRatherThanSemicolonRefactoring method visit.
@Override
public boolean visit(EmptyStatement node) {
ASTNode parent = node.getParent();
if (parent instanceof Block) {
this.ctx.getRefactorings().remove(node);
return DO_NOT_VISIT_SUBTREE;
}
parent = getParentIgnoring(node, Block.class);
if (parent instanceof IfStatement) {
IfStatement is = (IfStatement) parent;
List<Statement> thenStmts = asList(is.getThenStatement());
List<Statement> elseStmts = asList(is.getElseStatement());
boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
boolean elseIsEmptyStmt = elseStmts.size() == 1 && is(elseStmts.get(0), EmptyStatement.class);
if (thenIsEmptyStmt && elseIsEmptyStmt) {
this.ctx.getRefactorings().remove(parent);
return DO_NOT_VISIT_SUBTREE;
} else if (thenIsEmptyStmt && is.getElseStatement() == null) {
this.ctx.getRefactorings().remove(is);
return DO_NOT_VISIT_SUBTREE;
} else if (elseIsEmptyStmt) {
this.ctx.getRefactorings().remove(is.getElseStatement());
return DO_NOT_VISIT_SUBTREE;
}
} else if (parent instanceof TryStatement) {
TryStatement ts = (TryStatement) parent;
return maybeRemoveEmptyStmtBody(node, ts, ts.getBody());
} else if (parent instanceof EnhancedForStatement) {
EnhancedForStatement efs = (EnhancedForStatement) parent;
return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
} else if (parent instanceof ForStatement) {
ForStatement fs = (ForStatement) parent;
return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
} else if (parent instanceof WhileStatement) {
WhileStatement ws = (WhileStatement) parent;
return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class MergeConditionalBlocksRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final Expression firstCondition = node.getExpression();
final List<Statement> ifCode = asList(node.getThenStatement());
final List<Statement> elseCode = asList(node.getElseStatement());
if (elseCode != null && elseCode.size() == 1 && elseCode.get(0) instanceof IfStatement) {
final IfStatement subNode = (IfStatement) elseCode.get(0);
final Expression secondCondition = subNode.getExpression();
return maybeMergeBlocks(firstCondition, ifCode, subNode, secondCondition, subNode.getThenStatement(), subNode.getElseStatement(), true) && maybeMergeBlocks(firstCondition, ifCode, subNode, secondCondition, subNode.getElseStatement(), subNode.getThenStatement(), false);
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.IfStatement in project che by eclipse.
the class SourceProvider method isSingleControlStatementWithoutBlock.
private boolean isSingleControlStatementWithoutBlock() {
List<Statement> statements = fDeclaration.getBody().statements();
int size = statements.size();
if (size != 1)
return false;
Statement statement = statements.get(size - 1);
int nodeType = statement.getNodeType();
if (nodeType == ASTNode.IF_STATEMENT) {
IfStatement ifStatement = (IfStatement) statement;
return !(ifStatement.getThenStatement() instanceof Block) && !(ifStatement.getElseStatement() instanceof Block);
} else if (nodeType == ASTNode.FOR_STATEMENT) {
return !(((ForStatement) statement).getBody() instanceof Block);
} else if (nodeType == ASTNode.WHILE_STATEMENT) {
return !(((WhileStatement) statement).getBody() instanceof Block);
}
return false;
}
use of org.eclipse.jdt.core.dom.IfStatement in project che by eclipse.
the class InputFlowAnalyzer method endVisit.
@Override
public void endVisit(IfStatement node) {
if (skipNode(node))
return;
Statement thenPart = node.getThenStatement();
Statement elsePart = node.getElseStatement();
if ((thenPart != null && fSelection.coveredBy(thenPart)) || (elsePart != null && fSelection.coveredBy(elsePart))) {
GenericSequentialFlowInfo info = createSequential();
setFlowInfo(node, info);
endVisitConditional(info, node.getExpression(), new ASTNode[] { thenPart, elsePart });
} else {
super.endVisit(node);
}
}
Aggregations