use of org.eclipse.jdt.core.dom.SwitchStatement in project che 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 che by eclipse.
the class CallInliner method initializeInsertionPoint.
private void initializeInsertionPoint(int nos) {
fInsertionIndex = -1;
fNeedsStatement = false;
// if we have a constructor invocation the invocation itself is already a statement
ASTNode parentStatement = fInvocation instanceof Statement ? fInvocation : ASTNodes.getParent(fInvocation, Statement.class);
if (parentStatement == null)
return;
ASTNode container = parentStatement.getParent();
int type = container.getNodeType();
if (type == ASTNode.BLOCK) {
Block block = (Block) container;
fListRewrite = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
fInsertionIndex = fListRewrite.getRewrittenList().indexOf(parentStatement);
} else if (type == ASTNode.SWITCH_STATEMENT) {
SwitchStatement switchStatement = (SwitchStatement) container;
fListRewrite = fRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
fInsertionIndex = fListRewrite.getRewrittenList().indexOf(parentStatement);
} else if (isControlStatement(container) || type == ASTNode.LABELED_STATEMENT) {
fNeedsStatement = true;
if (nos > 1 || needsBlockAroundDanglingIf()) {
Block block = fInvocation.getAST().newBlock();
fInsertionIndex = 0;
Statement currentStatement = null;
switch(type) {
case ASTNode.LABELED_STATEMENT:
currentStatement = ((LabeledStatement) container).getBody();
break;
case ASTNode.FOR_STATEMENT:
currentStatement = ((ForStatement) container).getBody();
break;
case ASTNode.ENHANCED_FOR_STATEMENT:
currentStatement = ((EnhancedForStatement) container).getBody();
break;
case ASTNode.WHILE_STATEMENT:
currentStatement = ((WhileStatement) container).getBody();
break;
case ASTNode.DO_STATEMENT:
currentStatement = ((DoStatement) container).getBody();
break;
case ASTNode.IF_STATEMENT:
IfStatement node = (IfStatement) container;
Statement thenPart = node.getThenStatement();
if (fTargetNode == thenPart || ASTNodes.isParent(fTargetNode, thenPart)) {
currentStatement = thenPart;
} else {
currentStatement = node.getElseStatement();
}
break;
}
Assert.isNotNull(currentStatement);
fRewrite.replace(currentStatement, block, null);
fListRewrite = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
// The method to be inlined is not the body of the control statement.
if (currentStatement != fTargetNode) {
fListRewrite.insertLast(fRewrite.createCopyTarget(currentStatement), null);
} else {
// We can't replace a copy with something else. So we
// have to insert all statements to be inlined.
fTargetNode = null;
}
}
}
// We only insert one new statement or we delete the existing call.
// So there is no need to have an insertion index.
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project che by eclipse.
the class PromoteTempToFieldRefactoring method addLocalDeclarationSplit.
private void addLocalDeclarationSplit(ASTRewrite rewrite) {
VariableDeclarationStatement tempDeclarationStatement = getTempDeclarationStatement();
ASTNode parentStatement = tempDeclarationStatement.getParent();
ListRewrite listRewrite;
if (parentStatement instanceof SwitchStatement) {
listRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
} else if (parentStatement instanceof Block) {
listRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
} else {
// should not happen. VariableDeclaration's can not be in a control statement body
listRewrite = null;
Assert.isTrue(false);
}
int statementIndex = listRewrite.getOriginalList().indexOf(tempDeclarationStatement);
Assert.isTrue(statementIndex != -1);
Statement newStatement = createNewAssignmentStatement(rewrite);
List<VariableDeclarationFragment> fragments = tempDeclarationStatement.fragments();
int fragmentIndex = fragments.indexOf(fTempDeclarationNode);
Assert.isTrue(fragmentIndex != -1);
if (fragments.size() == 1) {
rewrite.replace(tempDeclarationStatement, newStatement, null);
return;
}
for (int i1 = fragmentIndex, n = fragments.size(); i1 < n; i1++) {
VariableDeclarationFragment fragment = fragments.get(i1);
rewrite.remove(fragment, null);
}
if (fragmentIndex == 0)
rewrite.remove(tempDeclarationStatement, null);
Assert.isTrue(tempHasInitializer());
listRewrite.insertAt(newStatement, statementIndex + 1, null);
if (fragmentIndex + 1 < fragments.size()) {
VariableDeclarationFragment firstFragmentAfter = fragments.get(fragmentIndex + 1);
VariableDeclarationFragment copyfirstFragmentAfter = (VariableDeclarationFragment) rewrite.createCopyTarget(firstFragmentAfter);
VariableDeclarationStatement statement = getAST().newVariableDeclarationStatement(copyfirstFragmentAfter);
Type type = (Type) rewrite.createCopyTarget(tempDeclarationStatement.getType());
statement.setType(type);
List<IExtendedModifier> modifiers = tempDeclarationStatement.modifiers();
if (modifiers.size() > 0) {
ListRewrite modifiersRewrite = rewrite.getListRewrite(tempDeclarationStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
ASTNode firstModifier = (ASTNode) modifiers.get(0);
ASTNode lastModifier = (ASTNode) modifiers.get(modifiers.size() - 1);
ASTNode modifiersCopy = modifiersRewrite.createCopyTarget(firstModifier, lastModifier);
statement.modifiers().add(modifiersCopy);
}
for (int i = fragmentIndex + 2; i < fragments.size(); i++) {
VariableDeclarationFragment fragment = fragments.get(i);
VariableDeclarationFragment fragmentCopy = (VariableDeclarationFragment) rewrite.createCopyTarget(fragment);
statement.fragments().add(fragmentCopy);
}
listRewrite.insertAt(statement, statementIndex + 2, null);
}
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class SwitchRefactoring method replaceWithSwitchStmt.
private void replaceWithSwitchStmt(final IfStatement node, final Expression switchExpr, final List<SwitchCaseSection> cases, final Statement remainingStmt) {
final ASTBuilder b = ctx.getASTBuilder();
final SwitchStatement switchStmt = b.switch0(b.move(switchExpr));
for (final SwitchCaseSection aCase : cases) {
addCaseWithStmts(switchStmt, aCase.constantExprs, aCase.stmts);
}
if (remainingStmt != null) {
addDefaultWithStmts(switchStmt, asList(remainingStmt));
}
ctx.getRefactorings().replace(node, switchStmt);
}
use of org.eclipse.jdt.core.dom.SwitchStatement in project AutoRefactor by JnRouvignac.
the class SwitchRefactoring method getSwitchStructure.
private List<SwitchCaseSection> getSwitchStructure(final SwitchStatement node) {
final List<SwitchCaseSection> switchStructure = new ArrayList<SwitchCaseSection>();
SwitchCaseSection currentCase = new SwitchCaseSection();
for (final Statement stmt : statements(node)) {
if (stmt instanceof SwitchCase) {
final SwitchCase swithCase = (SwitchCase) stmt;
if (!currentCase.stmts.isEmpty()) {
switchStructure.add(currentCase);
currentCase = new SwitchCaseSection();
}
currentCase.existingCases.add(swithCase);
} else {
currentCase.stmts.add(stmt);
}
}
if (!currentCase.existingCases.isEmpty()) {
switchStructure.add(currentCase);
}
return switchStructure;
}
Aggregations