use of org.eclipse.jdt.core.dom.WhileStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInverseIfIntoContinueInLoopsProposals.
private static boolean getInverseIfIntoContinueInLoopsProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
if (!(covering instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) covering;
if (ifStatement.getElseStatement() != null) {
return false;
}
// prepare outer control structure and block that contains 'if' statement
ASTNode ifParent = ifStatement.getParent();
Block ifParentBlock = null;
ASTNode ifParentStructure = ifParent;
if (ifParentStructure instanceof Block) {
ifParentBlock = (Block) ifParent;
ifParentStructure = ifParentStructure.getParent();
}
// check that control structure is loop and 'if' statement if last statement
if (!(ifParentStructure instanceof ForStatement) && !(ifParentStructure instanceof WhileStatement)) {
return false;
}
if (ifParentBlock != null && ifParentBlock.statements().indexOf(ifStatement) != ifParentBlock.statements().size() - 1) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// create inverted 'if' statement
Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(inversedExpression);
newIf.setThenStatement(ast.newContinueStatement());
//
if (ifParentBlock == null) {
// if there is no block, create it
ifParentBlock = ast.newBlock();
ifParentBlock.statements().add(newIf);
for (Iterator<Statement> iter = getUnwrappedStatements(ifStatement.getThenStatement()).iterator(); iter.hasNext(); ) {
Statement statement = iter.next();
ifParentBlock.statements().add(rewrite.createMoveTarget(statement));
}
// replace 'if' statement as body with new block
if (ifParentStructure instanceof ForStatement) {
rewrite.set(ifParentStructure, ForStatement.BODY_PROPERTY, ifParentBlock, null);
} else if (ifParentStructure instanceof WhileStatement) {
rewrite.set(ifParentStructure, WhileStatement.BODY_PROPERTY, ifParentBlock, null);
}
} else {
// if there was block, replace
ListRewrite listRewriter = rewrite.getListRewrite(ifParentBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
listRewriter.replace(ifStatement, newIf, null);
// add statements from 'then' to the end of block
for (Iterator<Statement> iter = getUnwrappedStatements(ifStatement.getThenStatement()).iterator(); iter.hasNext(); ) {
Statement statement = iter.next();
listRewriter.insertLast(rewrite.createMoveTarget(statement), null);
}
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseIfToContinue_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERT_IF_TO_CONTINUE);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.WhileStatement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInverseIfContinueIntoIfThenInLoopsProposals.
private static boolean getInverseIfContinueIntoIfThenInLoopsProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
if (!(covering instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) covering;
if (ifStatement.getElseStatement() != null) {
return false;
}
// check that 'then' is 'continue'
if (!(ifStatement.getThenStatement() instanceof ContinueStatement)) {
return false;
}
// check that 'if' statement is statement in block that is body of loop
Block loopBlock = null;
if (ifStatement.getParent() instanceof Block && ifStatement.getParent().getParent() instanceof ForStatement) {
loopBlock = (Block) ifStatement.getParent();
} else if (ifStatement.getParent() instanceof Block && ifStatement.getParent().getParent() instanceof WhileStatement) {
loopBlock = (Block) ifStatement.getParent();
} else {
return false;
}
if (resultingCollections == null) {
return true;
}
//
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// create inverted 'if' statement
Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(inversedExpression);
// prepare 'then' for new 'if'
Block thenBlock = ast.newBlock();
int ifIndex = loopBlock.statements().indexOf(ifStatement);
for (int i = ifIndex + 1; i < loopBlock.statements().size(); i++) {
Statement statement = (Statement) loopBlock.statements().get(i);
thenBlock.statements().add(rewrite.createMoveTarget(statement));
}
newIf.setThenStatement(thenBlock);
// replace 'if' statement in loop
rewrite.replace(ifStatement, newIf, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseIfContinue_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_IF_CONTINUE);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.WhileStatement 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.WhileStatement 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.WhileStatement in project che by eclipse.
the class ControlStatementsFix method createRemoveBlockFix.
public static ControlStatementsFix[] createRemoveBlockFix(CompilationUnit compilationUnit, ASTNode node) {
if (!(node instanceof Statement)) {
return null;
}
Statement statement = (Statement) node;
if (statement instanceof Block) {
Block block = (Block) statement;
if (block.statements().size() != 1)
return null;
ASTNode parent = block.getParent();
if (!(parent instanceof Statement))
return null;
statement = (Statement) parent;
}
if (statement instanceof IfStatement) {
List<ControlStatementsFix> result = new ArrayList<ControlStatementsFix>();
List<RemoveBlockOperation> removeAllList = new ArrayList<RemoveBlockOperation>();
IfElseIterator iter = new IfElseIterator((IfStatement) statement);
IfStatement item = null;
while (iter.hasNext()) {
item = iter.next();
if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(item, IfStatement.THEN_STATEMENT_PROPERTY)) {
RemoveBlockOperation op = new RemoveBlockOperation(item, IfStatement.THEN_STATEMENT_PROPERTY);
removeAllList.add(op);
if (item == statement)
result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeIfBlock_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }));
}
}
if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(item, IfStatement.ELSE_STATEMENT_PROPERTY)) {
RemoveBlockOperation op = new RemoveBlockOperation(item, IfStatement.ELSE_STATEMENT_PROPERTY);
removeAllList.add(op);
if (item == statement)
result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeElseBlock_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }));
}
if (removeAllList.size() > 1) {
CompilationUnitRewriteOperation[] allConvert = removeAllList.toArray(new CompilationUnitRewriteOperation[removeAllList.size()]);
result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeIfElseBlock_proposalDescription, compilationUnit, allConvert));
}
return result.toArray(new ControlStatementsFix[result.size()]);
} else if (statement instanceof WhileStatement) {
if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, WhileStatement.BODY_PROPERTY)) {
RemoveBlockOperation op = new RemoveBlockOperation(statement, WhileStatement.BODY_PROPERTY);
return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
}
} else if (statement instanceof ForStatement) {
if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, ForStatement.BODY_PROPERTY)) {
RemoveBlockOperation op = new RemoveBlockOperation(statement, ForStatement.BODY_PROPERTY);
return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
}
} else if (statement instanceof EnhancedForStatement) {
if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, EnhancedForStatement.BODY_PROPERTY)) {
RemoveBlockOperation op = new RemoveBlockOperation(statement, EnhancedForStatement.BODY_PROPERTY);
return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
}
} else if (statement instanceof DoStatement) {
if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, DoStatement.BODY_PROPERTY)) {
RemoveBlockOperation op = new RemoveBlockOperation(statement, DoStatement.BODY_PROPERTY);
return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
}
}
return null;
}
Aggregations