Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2009 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 11-18
Link Here
|
11 |
package org.eclipse.jdt.internal.corext.dom; |
11 |
package org.eclipse.jdt.internal.corext.dom; |
12 |
|
12 |
|
13 |
import java.util.Arrays; |
13 |
import java.util.Arrays; |
|
|
14 |
import java.util.HashMap; |
14 |
import java.util.Iterator; |
15 |
import java.util.Iterator; |
15 |
import java.util.List; |
16 |
import java.util.List; |
|
|
17 |
import java.util.Map; |
16 |
|
18 |
|
17 |
import org.eclipse.core.runtime.Assert; |
19 |
import org.eclipse.core.runtime.Assert; |
18 |
|
20 |
|
Lines 24-29
Link Here
|
24 |
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; |
26 |
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; |
25 |
import org.eclipse.jdt.core.dom.Block; |
27 |
import org.eclipse.jdt.core.dom.Block; |
26 |
import org.eclipse.jdt.core.dom.FieldDeclaration; |
28 |
import org.eclipse.jdt.core.dom.FieldDeclaration; |
|
|
29 |
import org.eclipse.jdt.core.dom.Modifier; |
27 |
import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
30 |
import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
28 |
import org.eclipse.jdt.core.dom.SwitchStatement; |
31 |
import org.eclipse.jdt.core.dom.SwitchStatement; |
29 |
import org.eclipse.jdt.core.dom.Type; |
32 |
import org.eclipse.jdt.core.dom.Type; |
Lines 49-55
Link Here
|
49 |
public static void rewriteModifiers(final FieldDeclaration declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) { |
52 |
public static void rewriteModifiers(final FieldDeclaration declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) { |
50 |
final List fragmentsToChange= Arrays.asList(toChange); |
53 |
final List fragmentsToChange= Arrays.asList(toChange); |
51 |
AST ast= declarationNode.getAST(); |
54 |
AST ast= declarationNode.getAST(); |
52 |
|
55 |
/* |
|
|
56 |
* Problem: Same declarationNode can be the subject of multiple calls to this method. |
57 |
* For the 2nd++ calls, the original declarationNode has already been rewritten, and this has to be taken into account. |
58 |
* |
59 |
* Assumption: |
60 |
* - Modifiers for each VariableDeclarationFragment are modified at most once. |
61 |
* |
62 |
* Solution: |
63 |
* - Maintain a map from original VariableDeclarationFragments to their new FieldDeclaration. |
64 |
* - Original modifiers in declarationNode belong to the first fragment. |
65 |
* - When a later fragment needs different modifiers, we create a new FieldDeclaration and move all successive fragments into that declaration |
66 |
* - When a fragment has been moved to a new declaration, make sure we don't create a new move target again, but instead use the already created one |
67 |
*/ |
53 |
List fragments= declarationNode.fragments(); |
68 |
List fragments= declarationNode.fragments(); |
54 |
Iterator iter= fragments.iterator(); |
69 |
Iterator iter= fragments.iterator(); |
55 |
|
70 |
|
Lines 63-103
Link Here
|
63 |
VariableDeclarationFragment lastFragment= (VariableDeclarationFragment)iter.next(); |
78 |
VariableDeclarationFragment lastFragment= (VariableDeclarationFragment)iter.next(); |
64 |
ASTNode lastStatement= declarationNode; |
79 |
ASTNode lastStatement= declarationNode; |
65 |
|
80 |
|
66 |
boolean modifiersModified= false; |
|
|
67 |
if (fragmentsToChange.contains(lastFragment)) { |
81 |
if (fragmentsToChange.contains(lastFragment)) { |
68 |
ModifierRewrite modifierRewrite= ModifierRewrite.create(rewrite, declarationNode); |
82 |
ModifierRewrite modifierRewrite= ModifierRewrite.create(rewrite, declarationNode); |
69 |
modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group); |
83 |
modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group); |
70 |
modifiersModified= true; |
|
|
71 |
} |
84 |
} |
72 |
|
85 |
|
73 |
ListRewrite fragmentsRewrite= null; |
86 |
ListRewrite fragmentsRewrite= null; |
74 |
while (iter.hasNext()) { |
87 |
while (iter.hasNext()) { |
75 |
VariableDeclarationFragment currentFragment= (VariableDeclarationFragment)iter.next(); |
88 |
VariableDeclarationFragment currentFragment= (VariableDeclarationFragment)iter.next(); |
76 |
|
89 |
|
77 |
if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) { |
90 |
Map/*<VariableDeclarationFragment, MovedFragment>*/ lookup= (Map) rewrite.getProperty(MovedFragment.class.getName()); |
78 |
|
91 |
if (lookup == null) { |
79 |
FieldDeclaration newStatement= ast.newFieldDeclaration((VariableDeclarationFragment)rewrite.createMoveTarget(currentFragment)); |
92 |
lookup= new HashMap(); |
80 |
newStatement.setType((Type)rewrite.createCopyTarget(declarationNode.getType())); |
93 |
rewrite.setProperty(MovedFragment.class.getName(), lookup); |
81 |
|
94 |
} |
82 |
ModifierRewrite modifierRewrite= ModifierRewrite.create(rewrite, newStatement); |
95 |
MovedFragment currentMovedFragment= (MovedFragment)lookup.get(currentFragment); |
83 |
if (fragmentsToChange.contains(currentFragment)) { |
96 |
|
84 |
modifierRewrite.copyAllAnnotations(declarationNode, group); |
97 |
boolean changeLast= fragmentsToChange.contains(lastFragment); |
85 |
int newModifiers= (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers; |
98 |
boolean changeCurrent= fragmentsToChange.contains(currentFragment); |
86 |
modifierRewrite.setModifiers(newModifiers, excludedModifiers, group); |
99 |
if (changeLast != changeCurrent || lookup.containsKey(lastFragment)) { |
87 |
} else { |
100 |
ModifierRewrite modifierRewrite; |
88 |
modifierRewrite.copyAllModifiers(declarationNode, group, modifiersModified); |
101 |
if (currentMovedFragment != null) { |
|
|
102 |
// Current fragment has already been moved. Need to put in the right modifiers (removing any existing ones). |
103 |
modifierRewrite= ModifierRewrite.create(rewrite, currentMovedFragment.fDeclaration); |
104 |
ListRewrite listRewrite= rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY); |
105 |
List extendedList= listRewrite.getRewrittenList(); |
106 |
for (int i= 0; i < extendedList.size(); i++) { |
107 |
ASTNode curr= (ASTNode)extendedList.get(i); |
108 |
if (curr instanceof Modifier) |
109 |
rewrite.remove(curr, group); |
89 |
} |
110 |
} |
|
|
111 |
|
112 |
} else { // need to split an existing field declaration |
113 |
VariableDeclarationFragment moveTarget; |
114 |
moveTarget= (VariableDeclarationFragment)rewrite.createMoveTarget(currentFragment); |
115 |
|
116 |
FieldDeclaration newStatement= (FieldDeclaration)ast.createInstance(FieldDeclaration.class); |
117 |
rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY).insertLast(moveTarget, group); |
118 |
lookup.put(currentFragment, new MovedFragment(moveTarget, newStatement)); |
119 |
rewrite.set(newStatement, FieldDeclaration.TYPE_PROPERTY, rewrite.createCopyTarget(declarationNode.getType()), group); |
120 |
|
121 |
modifierRewrite= ModifierRewrite.create(rewrite, newStatement); |
122 |
modifierRewrite.copyAllAnnotations(declarationNode, group); |
90 |
blockRewrite.insertAfter(newStatement, lastStatement, group); |
123 |
blockRewrite.insertAfter(newStatement, lastStatement, group); |
91 |
|
124 |
|
92 |
fragmentsRewrite= rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY); |
125 |
fragmentsRewrite= rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY); |
93 |
lastStatement= newStatement; |
126 |
lastStatement= newStatement; |
|
|
127 |
} |
128 |
|
129 |
if (changeCurrent) { |
130 |
int newModifiers= (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers; |
131 |
modifierRewrite.setModifiers(newModifiers, excludedModifiers, group); |
132 |
} else { |
133 |
int newModifiers= declarationNode.getModifiers(); |
134 |
modifierRewrite.setModifiers(newModifiers, Modifier.NONE, group); |
135 |
} |
136 |
|
94 |
} else if (fragmentsRewrite != null) { |
137 |
} else if (fragmentsRewrite != null) { |
95 |
ASTNode fragment0= rewrite.createMoveTarget(currentFragment); |
138 |
VariableDeclarationFragment fragment0; |
|
|
139 |
if (currentMovedFragment != null) { |
140 |
fragment0= currentMovedFragment.fMoveTarget; |
141 |
rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY).remove(fragment0, group); |
142 |
} else { |
143 |
fragment0= (VariableDeclarationFragment)rewrite.createMoveTarget(currentFragment); |
144 |
} |
145 |
lookup.put(currentFragment, new MovedFragment(fragment0, lastStatement)); |
96 |
fragmentsRewrite.insertLast(fragment0, group); |
146 |
fragmentsRewrite.insertLast(fragment0, group); |
97 |
} |
147 |
} |
98 |
lastFragment= currentFragment; |
148 |
lastFragment= currentFragment; |
99 |
} |
149 |
} |
100 |
} |
150 |
} |
|
|
151 |
|
152 |
private static class MovedFragment { |
153 |
final VariableDeclarationFragment fMoveTarget; |
154 |
final ASTNode fDeclaration; |
155 |
|
156 |
public MovedFragment(VariableDeclarationFragment moveTarget, ASTNode declaration) { |
157 |
fMoveTarget= moveTarget; |
158 |
fDeclaration= declaration; |
159 |
} |
160 |
} |
101 |
|
161 |
|
102 |
public static void rewriteModifiers(final VariableDeclarationStatement declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, ASTRewrite rewrite, final TextEditGroup group) { |
162 |
public static void rewriteModifiers(final VariableDeclarationStatement declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, ASTRewrite rewrite, final TextEditGroup group) { |
103 |
final List fragmentsToChange= Arrays.asList(toChange); |
163 |
final List fragmentsToChange= Arrays.asList(toChange); |