use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class RemoveEmptyIfRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final Refactorings r = this.ctx.getRefactorings();
final Statement thenStmt = node.getThenStatement();
final Statement elseStmt = node.getElseStatement();
if (elseStmt != null && asList(elseStmt).isEmpty()) {
r.remove(elseStmt);
return DO_NOT_VISIT_SUBTREE;
} else if (thenStmt != null && asList(thenStmt).isEmpty()) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Expression condition = node.getExpression();
if (elseStmt != null) {
r.replace(node, b.if0(b.negate(condition), b.move(elseStmt)));
} else if (isPassive(condition)) {
removeBlock(node, r, b);
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class SwitchRefactoring method visit.
@Override
public boolean visit(final IfStatement node) {
if (hasUnlabeledBreak(node)) {
return VISIT_SUBTREE;
}
Variable variable = extractVariableAndValues(node);
if (variable == null) {
return VISIT_SUBTREE;
}
final SimpleName switchExpr = variable.name;
final List<SwitchCaseSection> cases = new ArrayList<SwitchCaseSection>();
Statement remainingStmt = null;
final Set<String> variableDeclarationIds = new HashSet<String>();
IfStatement currentNode = node;
while (haveSameIdentifier(switchExpr, variable.name) && haveSameType(switchExpr, variable.name)) {
if (detectDeclarationConflicts(currentNode.getThenStatement(), variableDeclarationIds)) {
// Cannot declare two variables with the same name in two cases
return VISIT_SUBTREE;
}
cases.add(new SwitchCaseSection(variable.constantValues, asList(currentNode.getThenStatement())));
remainingStmt = currentNode.getElseStatement();
variable = extractVariableAndValues(remainingStmt);
if (variable == null) {
break;
}
currentNode = (IfStatement) remainingStmt;
}
final List<SwitchCaseSection> filteredCases = filterDuplicateCaseValues(cases);
return maybeReplaceWithSwitchStmt(node, switchExpr, filteredCases, remainingStmt);
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class CFGBuilder method buildCFG.
/**
* Builds a CFG for the passed in statement.
*
* @param node the statement for which to build a CFG
* @param state the liveness state before the current statement.
* It contains: the live edges before the current statement,
* the live basic block to which the current statement might be added.
* If null, then the new basic block must be created for the current statement.
* @param throwers the thrower blocks information
* @return the new live state after the current statement
*/
public LivenessState buildCFG(IfStatement node, LivenessState state, ThrowerBlocks throwers) {
final CFGBasicBlock exprBlock = getCFGBasicBlock(node, state.nextStmtWillCreateNewBlock(), true);
try {
addVariableAccess(exprBlock, node.getExpression(), READ, throwers);
final LivenessState result = new LivenessState();
CFGEdgeBuilder thenEdge = new CFGEdgeBuilder(node.getExpression(), true, exprBlock);
result.addAll(buildCFG(node.getThenStatement(), LivenessState.of(thenEdge), throwers));
final Statement elseStmt = node.getElseStatement();
CFGEdgeBuilder elseEdge = new CFGEdgeBuilder(node.getExpression(), false, exprBlock);
if (elseStmt != null) {
result.addAll(buildCFG(elseStmt, LivenessState.of(elseEdge), throwers));
} else {
result.add(elseEdge);
}
return result.nextStmtWillCreateNewBlock();
} finally {
moveAllEdgesToBuild(node, state);
}
}
Aggregations