use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class ASTNodeFactory method newSwitchStatement.
/**
* Builds a new {@link SwitchStatement} instance.
*
* @param expression the switch expression
* @return a new switch statement
*/
public SwitchStatement newSwitchStatement(final Expression expression) {
SwitchStatement ss = ast.newSwitchStatement();
ss.setExpression(expression);
return ss;
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class ASTNodes method canHaveSiblings.
/**
* Returns true if a sibling may exist.
*
* @param node the start node
* @return true if a sibling may exist
*/
public static boolean canHaveSiblings(final Statement node) {
ASTNode statementAtLevel = statementAtLevel(node);
ASTNode parent = statementAtLevel.getParent();
return parent instanceof Block || parent instanceof SwitchStatement && statementAtLevel.getLocationInParent() == SwitchStatement.STATEMENTS_PROPERTY;
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class ASTNodes method getSiblings.
private static List<Statement> getSiblings(final Statement startNode, final boolean isForward) {
Statement statementAtLevel = statementAtLevel(startNode);
if (canHaveSiblings(statementAtLevel)) {
List<Statement> statements;
if (statementAtLevel.getParent() instanceof SwitchStatement) {
statements = ((SwitchStatement) statementAtLevel.getParent()).statements();
} else {
statements = asList((Statement) statementAtLevel.getParent());
}
int indexOfNode = statements.indexOf(statementAtLevel);
int siblingIndex = indexOfNode + (isForward ? 1 : -1);
if (0 <= siblingIndex && siblingIndex < statements.size()) {
if (isForward) {
return statements.subList(siblingIndex, statements.size());
}
return statements.subList(0, siblingIndex + 1);
}
}
return Collections.emptyList();
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final ReturnStatement node) {
this.appendToBuffer("return");
Expression _expression = node.getExpression();
boolean _tripleNotEquals = (_expression != null);
if (_tripleNotEquals) {
this.appendSpaceToBuffer();
node.getExpression().accept(this);
this.appendSpaceToBuffer();
} else {
final ASTNode parent = node.getParent();
final boolean isIfElse = ((parent instanceof IfStatement) && (((IfStatement) parent).getElseStatement() != null));
if (((!isIfElse) && (!(parent instanceof SwitchStatement)))) {
this.appendToBuffer(";");
}
}
return false;
}
use of org.eclipse.jdt.core.dom.SwitchStatement 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;
}
Aggregations