use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.
the class GenericBuilderAdapterBuilder method getMethodSwitch.
protected List getMethodSwitch(String type) {
List cases = switchMap.get(type);
if (cases == null) {
MethodDeclaration method = newMethodDeclaration("void", type, newSingleVariableDeclaration(newParameterizedType(EntityDescriptor.class.getName(), ast.newWildcardType()), "entityDesc"));
SwitchStatement switchStm = newSwitchStatement(newMethodInvocation("entityDesc", "getOrdinal"));
method.getBody().statements().add(switchStm);
addBodyDeclaration(method);
switchMap.put(type, cases = switchStm.statements());
}
return cases;
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.
the class GenericBuilderAdapterBuilder method getDataMethodSwitch.
protected List getDataMethodSwitch(String type, String valueType) {
List cases = (List) dataSwitchMap.get(valueType);
if (cases == null) {
MethodDeclaration method = newMethodDeclaration("void", "wEntity", newSingleVariableDeclaration(newParameterizedType(EntityDescriptor.class.getName(), ast.newWildcardType()), "entityDesc"), newSingleVariableDeclaration(valueType, "value"));
SwitchStatement switchStm = newSwitchStatement(newMethodInvocation("entityDesc", "getOrdinal"));
method.getBody().statements().add(switchStm);
addBodyDeclaration(method);
dataSwitchMap.put(valueType, cases = switchStm.statements());
}
return cases;
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.
the class CompilationUnitBuilder method newSwitchStatement.
public SwitchStatement newSwitchStatement(Expression exp) {
SwitchStatement stm = ast.newSwitchStatement();
stm.setExpression(exp);
return stm;
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project flux by eclipse.
the class VariableDeclarationRewrite method rewriteModifiers.
public static void rewriteModifiers(final VariableDeclarationStatement declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, ASTRewrite rewrite, final TextEditGroup group) {
final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
AST ast = declarationNode.getAST();
List<VariableDeclarationFragment> fragments = declarationNode.fragments();
Iterator<VariableDeclarationFragment> iter = fragments.iterator();
ListRewrite blockRewrite = null;
ASTNode parentStatement = declarationNode.getParent();
if (parentStatement instanceof SwitchStatement) {
blockRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
} else if (parentStatement instanceof Block) {
blockRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
} else {
// should not happen. VariableDeclaration's can not be in a control statement body
Assert.isTrue(false);
}
VariableDeclarationFragment lastFragment = iter.next();
ASTNode lastStatement = declarationNode;
boolean modifiersModified = false;
if (fragmentsToChange.contains(lastFragment)) {
ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
modifiersModified = true;
}
ListRewrite fragmentsRewrite = null;
while (iter.hasNext()) {
VariableDeclarationFragment currentFragment = iter.next();
if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) {
VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement((VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment));
newStatement.setType((Type) rewrite.createCopyTarget(declarationNode.getType()));
ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
if (fragmentsToChange.contains(currentFragment)) {
modifierRewrite.copyAllAnnotations(declarationNode, group);
int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
} else {
modifierRewrite.copyAllModifiers(declarationNode, group, modifiersModified);
}
blockRewrite.insertAfter(newStatement, lastStatement, group);
fragmentsRewrite = rewrite.getListRewrite(newStatement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
lastStatement = newStatement;
} else if (fragmentsRewrite != null) {
ASTNode fragment0 = rewrite.createMoveTarget(currentFragment);
fragmentsRewrite.insertLast(fragment0, group);
}
lastFragment = currentFragment;
}
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteIfRatherThanTwoSwitchCasesCleanUp method visit.
@SuppressWarnings("deprecation")
@Override
public boolean visit(final SwitchStatement visited) {
List<?> statements = visited.statements();
if (statements.isEmpty()) {
return true;
}
Set<SimpleName> previousVarIds = new HashSet<>();
Set<SimpleName> caseVarIds = new HashSet<>();
List<Pair<List<Expression>, List<Statement>>> switchStructure = new ArrayList<>();
List<Expression> caseExprs = new ArrayList<>();
List<Statement> caseStatements = new ArrayList<>();
boolean isPreviousStmtACase = true;
int caseIndexWithDefault = -1;
for (Object object : statements) {
Statement statement = (Statement) object;
if (statement instanceof SwitchCase) {
if (!isPreviousStmtACase) {
if (switchStructure.size() > 2) {
return true;
}
previousVarIds.addAll(caseVarIds);
caseVarIds.clear();
switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
caseExprs = new ArrayList<>();
caseStatements = new ArrayList<>();
}
if (((SwitchCase) statement).isDefault()) {
caseIndexWithDefault = switchStructure.size();
} else {
caseExprs.add(((SwitchCase) statement).getExpression());
}
isPreviousStmtACase = true;
} else {
VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(previousVarIds, false);
varOccurrenceVisitor.traverseNodeInterruptibly(statement);
if (varOccurrenceVisitor.isVarConflicting()) {
return true;
}
caseVarIds.addAll(ASTNodes.getLocalVariableIdentifiers(statement, false));
caseStatements.add(statement);
isPreviousStmtACase = false;
}
}
switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
if (caseIndexWithDefault != -1) {
Pair<List<Expression>, List<Statement>> caseWithDefault = switchStructure.remove(caseIndexWithDefault);
switchStructure.add(caseWithDefault);
}
if (switchStructure.size() > 2 || !switchStructure.get(switchStructure.size() - 1).getFirst().isEmpty() && !ASTNodes.isPassive(visited.getExpression())) {
return true;
}
List<BreakStatement> overBreaks = new ArrayList<>();
for (int i = 0; i < switchStructure.size(); i++) {
Pair<List<Expression>, List<Statement>> caseStructure = switchStructure.get(i);
if (!caseStructure.getSecond().isEmpty()) {
Statement lastStatement = caseStructure.getSecond().get(caseStructure.getSecond().size() - 1);
if (i < switchStructure.size() - 1 && !ASTNodes.fallsThrough(lastStatement)) {
return true;
}
BreakStatement breakStatement = ASTNodes.as(lastStatement, BreakStatement.class);
if (breakStatement != null && breakStatement.getLabel() == null) {
caseStructure.getSecond().remove(caseStructure.getSecond().size() - 1);
}
} else if (i < switchStructure.size() - 1) {
return true;
}
}
for (Pair<List<Expression>, List<Statement>> caseStructure : switchStructure) {
for (Statement oneStatement : caseStructure.getSecond()) {
BreakVisitor breakVisitor = new BreakVisitor();
breakVisitor.traverseNodeInterruptibly(oneStatement);
if (!breakVisitor.canBeRefactored()) {
return true;
}
overBreaks.addAll(breakVisitor.getBreaks());
}
}
replaceBySwitch(visited, switchStructure, caseIndexWithDefault, overBreaks);
return false;
}
Aggregations