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);
}
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class IfElseIfRefactoring method visit.
// TODO JNR
// UseIfElseIfRefactoring
// if (b) {
// return i;
// }
// if (c) {
// return j;
// }
// if (d) {
// return k;
// }
// return l;
@Override
public boolean visit(IfStatement node) {
final Statement elseStmt = node.getElseStatement();
if (elseStmt instanceof Block) {
List<Statement> elseStmts = statements((Block) elseStmt);
if (elseStmts.size() == 1 && elseStmts.get(0) instanceof IfStatement) {
final ASTBuilder b = this.ctx.getASTBuilder();
this.ctx.getRefactorings().set(node, ELSE_STATEMENT_PROPERTY, b.copy(elseStmts.get(0)));
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class TryWithResourceRefactoring method visit.
@Override
public boolean visit(TryStatement node) {
final List<Statement> tryStmts = asList(node.getBody());
if (tryStmts.size() >= 1 && tryStmts.get(0).getNodeType() == TRY_STATEMENT) {
final TryStatement innerTryStmt = as(tryStmts.get(0), TryStatement.class);
if (innerTryStmt != null && !innerTryStmt.resources().isEmpty() && innerTryStmt.catchClauses().isEmpty()) {
return collapseTryStatements(node, innerTryStmt);
}
}
final VariableDeclarationStatement previousDeclStmt = as(getPreviousStatement(node), VariableDeclarationStatement.class);
if (previousDeclStmt == null) {
return VISIT_SUBTREE;
}
final VariableDeclarationFragment previousDeclFragment = getUniqueFragment(previousDeclStmt);
final List<Statement> finallyStmts = asList(node.getFinally());
if (previousDeclFragment != null && finallyStmts.size() >= 1) {
final List<ASTNode> nodesToRemove = new ArrayList<ASTNode>();
nodesToRemove.add(previousDeclStmt);
final Statement finallyStmt = finallyStmts.get(0);
nodesToRemove.add(finallyStmts.size() == 1 ? node.getFinally() : finallyStmt);
final ExpressionStatement finallyEs = as(finallyStmt, ExpressionStatement.class);
final IfStatement finallyIs = as(finallyStmt, IfStatement.class);
if (finallyEs != null) {
final MethodInvocation mi = as(finallyEs.getExpression(), MethodInvocation.class);
if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, mi.getExpression())) {
final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
return refactorToTryWithResources(node, newResource, nodesToRemove);
}
} else if (finallyIs != null && asList(finallyIs.getThenStatement()).size() == 1 && asList(finallyIs.getElseStatement()).isEmpty()) {
final Expression nullCheckedExpr = getNullCheckedExpression(finallyIs.getExpression());
final Statement thenStmt = asList(finallyIs.getThenStatement()).get(0);
final MethodInvocation mi = asExpression(thenStmt, MethodInvocation.class);
if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, nullCheckedExpr, mi.getExpression())) {
final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
return refactorToTryWithResources(node, newResource, nodesToRemove);
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.IfStatement in project AutoRefactor by JnRouvignac.
the class UpdateSetRatherThanTestingFirstRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final Statement elseStmt = node.getElseStatement();
final Statement thenStmt = node.getThenStatement();
final PrefixExpression pe = as(node.getExpression(), PrefixExpression.class);
if (hasOperator(pe, NOT)) {
return maybeReplaceSetContains(node, pe.getOperand(), thenStmt, elseStmt, false);
} else {
return maybeReplaceSetContains(node, node.getExpression(), elseStmt, thenStmt, true);
}
}
Aggregations