use of org.eclipse.jdt.core.dom.LabeledStatement in project eclipse.jdt.ls by eclipse.
the class ExtractMethodAnalyzer method canHandleBranches.
private String canHandleBranches() {
if (fReturnValue != null) {
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
}
ASTNode[] selectedNodes = getSelectedNodes();
final ASTNode lastSelectedNode = selectedNodes[selectedNodes.length - 1];
Statement body = getParentLoopBody(lastSelectedNode.getParent());
if (!(body instanceof Block)) {
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
}
if (body != lastSelectedNode) {
Block block = (Block) body;
List<Statement> statements = block.statements();
ASTNode lastStatementInLoop = statements.get(statements.size() - 1);
if (lastSelectedNode != lastStatementInLoop) {
return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
}
}
final String[] continueMatchesLoopProblem = { null };
for (int i = 0; i < selectedNodes.length; i++) {
final ASTNode astNode = selectedNodes[i];
astNode.accept(new ASTVisitor() {
ArrayList<String> fLocalLoopLabels = new ArrayList<>();
@Override
public boolean visit(BreakStatement node) {
SimpleName label = node.getLabel();
if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
// $NON-NLS-1$
continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_break_mismatch, new Object[] { ("break " + label.getIdentifier()) });
}
return false;
}
@Override
public boolean visit(LabeledStatement node) {
SimpleName label = node.getLabel();
if (label != null) {
fLocalLoopLabels.add(label.getIdentifier());
}
return true;
}
@Override
public void endVisit(ContinueStatement node) {
SimpleName label = node.getLabel();
if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
if (fEnclosingLoopLabel == null || !label.getIdentifier().equals(fEnclosingLoopLabel.getIdentifier())) {
// $NON-NLS-1$
continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_continue_mismatch, new Object[] { "continue " + label.getIdentifier() });
}
}
}
});
}
return continueMatchesLoopProblem[0];
}
use of org.eclipse.jdt.core.dom.LabeledStatement in project eclipse.jdt.ls 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;
}
use of org.eclipse.jdt.core.dom.LabeledStatement 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;
}
use of org.eclipse.jdt.core.dom.LabeledStatement in project eclipse.jdt.ls by eclipse.
the class NavigateToDefinitionHandler method computeBreakContinue.
private Location computeBreakContinue(ITypeRoot typeRoot, int line, int column) throws CoreException {
int offset = JsonRpcHelpers.toOffset(typeRoot.getBuffer(), line, column);
if (offset >= 0) {
CompilationUnit unit = SharedASTProviderCore.getAST(typeRoot, SharedASTProviderCore.WAIT_YES, null);
if (unit == null) {
return null;
}
ASTNode selectedNode = NodeFinder.perform(unit, offset, 0);
ASTNode node = null;
SimpleName label = null;
if (selectedNode instanceof BreakStatement) {
node = selectedNode;
label = ((BreakStatement) node).getLabel();
} else if (selectedNode instanceof ContinueStatement) {
node = selectedNode;
label = ((ContinueStatement) node).getLabel();
} else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof BreakStatement) {
node = selectedNode.getParent();
label = ((BreakStatement) node).getLabel();
} else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof ContinueStatement) {
node = selectedNode.getParent();
label = ((ContinueStatement) node).getLabel();
}
if (node != null) {
ASTNode parent = node.getParent();
ASTNode target = null;
while (parent != null) {
if (parent instanceof MethodDeclaration || parent instanceof Initializer) {
break;
}
if (label == null) {
if (parent instanceof ForStatement || parent instanceof EnhancedForStatement || parent instanceof WhileStatement || parent instanceof DoStatement) {
target = parent;
break;
}
if (node instanceof BreakStatement) {
if (parent instanceof SwitchStatement || parent instanceof SwitchExpression) {
target = parent;
break;
}
}
if (node instanceof LabeledStatement) {
target = parent;
break;
}
} else if (LabeledStatement.class.isInstance(parent)) {
LabeledStatement ls = (LabeledStatement) parent;
if (ls.getLabel().getIdentifier().equals(label.getIdentifier())) {
target = ls;
break;
}
}
parent = parent.getParent();
}
if (target != null) {
int start = target.getStartPosition();
int end = new TokenScanner(unit.getTypeRoot()).getNextEndOffset(node.getStartPosition(), true) - start;
if (start >= 0 && end >= 0) {
return JDTUtils.toLocation((ICompilationUnit) typeRoot, start, end);
}
}
}
}
return null;
}
Aggregations