use of org.eclipse.jdt.core.dom.SwitchCase in project flux by eclipse.
the class StatementAnalyzer method endVisit.
/* (non-Javadoc)
* Method declared in ASTVisitor
*/
@Override
public void endVisit(SwitchStatement node) {
ASTNode[] selectedNodes = getSelectedNodes();
if (doAfterValidation(node, selectedNodes)) {
List<SwitchCase> cases = getSwitchCases(node);
for (int i = 0; i < selectedNodes.length; i++) {
ASTNode topNode = selectedNodes[i];
if (cases.contains(topNode)) {
invalidSelection(RefactoringCoreMessages.StatementAnalyzer_switch_statement);
break;
}
}
}
super.endVisit(node);
}
use of org.eclipse.jdt.core.dom.SwitchCase in project AutoRefactor by JnRouvignac.
the class ObsoleteSwitchCleanUp method mergeCases.
private void mergeCases(final Merge merge, final SwitchCaseSection sectionToKeep, final SwitchCaseSection sectionToRemove) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteSwitchCleanUp_description);
Statement caseKept;
if (merge == Merge.BEFORE_SWITCH_CASES) {
caseKept = sectionToKeep.existingCases.get(0);
} else {
// move == Move.AFTER_SWITCH_CASES
caseKept = sectionToKeep.statements.get(0);
}
for (SwitchCase caseToMove : sectionToRemove.existingCases) {
rewrite.insertBefore(ASTNodes.createMoveTarget(rewrite, caseToMove), caseKept, group);
}
rewrite.remove(sectionToRemove.statements, group);
}
use of org.eclipse.jdt.core.dom.SwitchCase in project AutoRefactor by JnRouvignac.
the class ObsoleteSwitchCleanUp method getSwitchStructure.
private List<SwitchCaseSection> getSwitchStructure(final SwitchStatement node) {
List<SwitchCaseSection> switchStructure = new ArrayList<>();
SwitchCaseSection currentCase = new SwitchCaseSection();
for (Statement statement : (List<Statement>) node.statements()) {
if (statement instanceof SwitchCase) {
if (!currentCase.statements.isEmpty()) {
switchStructure.add(currentCase);
currentCase = new SwitchCaseSection();
}
SwitchCase swithCase = (SwitchCase) statement;
currentCase.existingCases.add(swithCase);
} else {
currentCase.statements.add(statement);
}
}
if (!currentCase.existingCases.isEmpty()) {
switchStructure.add(currentCase);
}
return switchStructure;
}
use of org.eclipse.jdt.core.dom.SwitchCase in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final SwitchStatement node) {
this.appendLineWrapToBuffer();
this.appendToBuffer("switch (");
node.getExpression().accept(this);
this.appendToBuffer(") ");
this.appendToBuffer("{");
this.increaseIndent();
final Function2<Map<SwitchCase, ArrayList<Statement>>, Statement, Map<SwitchCase, ArrayList<Statement>>> _function = (Map<SwitchCase, ArrayList<Statement>> map, Statement currStatement) -> {
if ((currStatement instanceof SwitchCase)) {
map.put(((SwitchCase) currStatement), CollectionLiterals.<Statement>newArrayList());
} else {
map.get(IterableExtensions.<SwitchCase>last(map.keySet())).add(currStatement);
}
return map;
};
final Map<SwitchCase, ArrayList<Statement>> foldedCases = IterableExtensions.<Statement, Map<SwitchCase, ArrayList<Statement>>>fold(node.statements(), CollectionLiterals.<SwitchCase, ArrayList<Statement>>newLinkedHashMap(), _function);
final BiConsumer<SwitchCase, ArrayList<Statement>> _function_1 = (SwitchCase switchCase, ArrayList<Statement> statements) -> {
switchCase.accept(this);
final boolean isLastCase = switchCase.equals(IterableExtensions.<SwitchCase>last(foldedCases.keySet()));
if ((statements.isEmpty() && (!isLastCase))) {
this.appendToBuffer(",");
} else {
this.appendToBuffer(":");
final boolean probablyReturns = ((IterableExtensions.<Statement>last(statements) instanceof ReturnStatement) || ((IterableExtensions.<Statement>last(statements) instanceof Block) && (IterableExtensions.<Object>last(((Block) IterableExtensions.<Statement>last(statements)).statements()) instanceof ReturnStatement)));
if (((!isLastCase) && (!probablyReturns))) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("/* FIXME unsupported fall-through */");
this.appendToBuffer(_builder.toString());
this.addProblem(node, "Unsupported fall-through case in switch expression");
}
}
final boolean surround = ((isLastCase && statements.isEmpty()) || ((!statements.isEmpty()) && (!(statements.get(0) instanceof Block))));
if (surround) {
this.appendToBuffer("{");
this.increaseIndent();
this.appendLineWrapToBuffer();
}
this.visitAll(statements);
if (surround) {
this.decreaseIndent();
this.appendLineWrapToBuffer();
this.appendToBuffer("}");
}
};
foldedCases.forEach(_function_1);
this.decreaseIndent();
this.appendLineWrapToBuffer();
this.appendToBuffer("}");
return false;
}
use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse-cs by checkstyle.
the class DefaultComesLastQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(SwitchCase node) {
if (containsPosition(lineInfo, node.getStartPosition())) {
if (node.isDefault() && !isLastSwitchCase(node)) {
SwitchStatement switchStatement = (SwitchStatement) node.getParent();
List<ASTNode> defaultCaseStatements = new ArrayList<>();
defaultCaseStatements.add(node);
// collect all statements belonging to the default case
int defaultStatementIndex = switchStatement.statements().indexOf(node);
for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
if (!(tmpNode instanceof SwitchCase)) {
defaultCaseStatements.add(tmpNode);
} else {
break;
}
}
// move the statements to the end of the statement list
switchStatement.statements().removeAll(defaultCaseStatements);
switchStatement.statements().addAll(defaultCaseStatements);
}
}
return true;
}
private boolean isLastSwitchCase(SwitchCase switchCase) {
SwitchStatement switchStatement = (SwitchStatement) switchCase.getParent();
// collect all statements belonging to the default case
int defaultStatementIndex = switchStatement.statements().indexOf(switchCase);
for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
if (tmpNode instanceof SwitchCase) {
return false;
}
}
return true;
}
};
}
Aggregations