use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.
the class QuickAssistProcessor method removeCatchBlock.
private static void removeCatchBlock(ASTRewrite rewrite, CatchClause catchClause) {
TryStatement tryStatement = (TryStatement) catchClause.getParent();
if (tryStatement.catchClauses().size() > 1 || tryStatement.getFinally() != null || !tryStatement.resources().isEmpty()) {
rewrite.remove(catchClause, null);
} else {
Block block = tryStatement.getBody();
List<Statement> statements = block.statements();
int nStatements = statements.size();
if (nStatements == 1) {
ASTNode first = statements.get(0);
rewrite.replace(tryStatement, rewrite.createCopyTarget(first), null);
} else if (nStatements > 1) {
ListRewrite listRewrite = rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
ASTNode first = statements.get(0);
ASTNode last = statements.get(statements.size() - 1);
ASTNode newStatement = listRewrite.createCopyTarget(first, last);
if (ASTNodes.isControlStatementBody(tryStatement.getLocationInParent())) {
Block newBlock = rewrite.getAST().newBlock();
newBlock.statements().add(newStatement);
newStatement = newBlock;
}
rewrite.replace(tryStatement, newStatement, null);
} else {
rewrite.remove(tryStatement, null);
}
}
}
use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method copyStatementExceptBreak.
private static Statement copyStatementExceptBreak(AST ast, ASTRewrite rewrite, Statement source) {
if (source instanceof Block) {
Block block = (Block) source;
Block newBlock = ast.newBlock();
for (Iterator<Statement> iter = block.statements().iterator(); iter.hasNext(); ) {
Statement statement = iter.next();
if (statement instanceof BreakStatement) {
continue;
}
newBlock.statements().add(copyStatementExceptBreak(ast, rewrite, statement));
}
return newBlock;
}
return (Statement) rewrite.createMoveTarget(source);
}
use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getJoinIfListInIfElseIfProposals.
private static boolean getJoinIfListInIfElseIfProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
if (coveredNodes.isEmpty()) {
return false;
}
// check that we have more than one covered statement
if (coveredNodes.size() < 2) {
return false;
}
// check that all selected nodes are 'if' statements with only 'then' statement
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
ASTNode node = iter.next();
if (!(node instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) node;
if (ifStatement.getElseStatement() != null) {
return false;
}
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
final AST ast = covering.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
//
IfStatement firstIfStatement = (IfStatement) coveredNodes.get(0);
IfStatement firstNewIfStatement = null;
//
IfStatement prevIfStatement = null;
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
IfStatement ifStatement = (IfStatement) iter.next();
// prepare new 'if' statement
IfStatement newIfStatement = ast.newIfStatement();
newIfStatement.setExpression((Expression) rewrite.createMoveTarget(ifStatement.getExpression()));
// prepare 'then' statement and convert into block if needed
Statement thenStatement = (Statement) rewrite.createMoveTarget(ifStatement.getThenStatement());
if (ifStatement.getThenStatement() instanceof IfStatement) {
IfStatement ifBodyStatement = (IfStatement) ifStatement.getThenStatement();
if (ifBodyStatement.getElseStatement() == null) {
Block thenBlock = ast.newBlock();
thenBlock.statements().add(thenStatement);
thenStatement = thenBlock;
}
}
newIfStatement.setThenStatement(thenStatement);
//
if (prevIfStatement != null) {
prevIfStatement.setElseStatement(newIfStatement);
rewrite.remove(ifStatement, null);
} else {
firstNewIfStatement = newIfStatement;
}
prevIfStatement = newIfStatement;
}
rewrite.replace(firstIfStatement, firstNewIfStatement, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_joinIfSequence;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_IF_SEQUENCE);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.
the class ScopeAnalyzer method getDeclarationsAfter.
public IBinding[] getDeclarationsAfter(int offset, int flags) {
try {
org.eclipse.jdt.core.dom.NodeFinder finder = new org.eclipse.jdt.core.dom.NodeFinder(fRoot, offset, 0);
ASTNode node = finder.getCoveringNode();
if (node == null) {
return null;
}
ASTNode declaration = ASTResolving.findParentStatement(node);
while (declaration instanceof Statement && declaration.getNodeType() != ASTNode.BLOCK) {
declaration = declaration.getParent();
}
if (declaration instanceof Block) {
DefaultBindingRequestor requestor = new DefaultBindingRequestor();
DeclarationsAfterVisitor visitor = new DeclarationsAfterVisitor(node.getStartPosition(), flags, requestor);
declaration.accept(visitor);
List<IBinding> result = requestor.getResult();
return result.toArray(new IBinding[result.size()]);
}
return NO_BINDING;
} finally {
clearLists();
}
}
use of org.eclipse.jdt.core.dom.Statement in project che by eclipse.
the class ExtractMethodAnalyzer method getParentLoopBody.
private Statement getParentLoopBody(ASTNode node) {
Statement stmt = null;
ASTNode start = node;
while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
start = start.getParent();
}
if (start instanceof ForStatement) {
stmt = ((ForStatement) start).getBody();
} else if (start instanceof DoStatement) {
stmt = ((DoStatement) start).getBody();
} else if (start instanceof WhileStatement) {
stmt = ((WhileStatement) start).getBody();
} else if (start instanceof EnhancedForStatement) {
stmt = ((EnhancedForStatement) start).getBody();
}
if (start != null && start.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
fEnclosingLoopLabel = labeledStatement.getLabel();
}
return stmt;
}
Aggregations