View | Details | Raw Unified | Return to bug 319069 | Differences between
and this patch

Collapse All | Expand All

(-)core extension/org/eclipse/jdt/internal/corext/dom/VariableDeclarationRewrite.java (-18 / +78 lines)
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);
(-)core refactoring/org/eclipse/jdt/internal/corext/refactoring/structure/MemberVisibilityAdjustor.java (-34 / +6 lines)
Lines 12-17 Link Here
12
12
13
import java.util.HashMap;
13
import java.util.HashMap;
14
import java.util.Iterator;
14
import java.util.Iterator;
15
import java.util.LinkedHashMap;
15
import java.util.Map;
16
import java.util.Map;
16
17
17
import org.eclipse.core.runtime.Assert;
18
import org.eclipse.core.runtime.Assert;
Lines 38-62 Link Here
38
import org.eclipse.jdt.core.JavaCore;
39
import org.eclipse.jdt.core.JavaCore;
39
import org.eclipse.jdt.core.JavaModelException;
40
import org.eclipse.jdt.core.JavaModelException;
40
import org.eclipse.jdt.core.WorkingCopyOwner;
41
import org.eclipse.jdt.core.WorkingCopyOwner;
41
import org.eclipse.jdt.core.dom.ASTNode;
42
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
43
import org.eclipse.jdt.core.dom.BodyDeclaration;
42
import org.eclipse.jdt.core.dom.BodyDeclaration;
44
import org.eclipse.jdt.core.dom.CompilationUnit;
43
import org.eclipse.jdt.core.dom.CompilationUnit;
45
import org.eclipse.jdt.core.dom.FieldDeclaration;
44
import org.eclipse.jdt.core.dom.FieldDeclaration;
46
import org.eclipse.jdt.core.dom.IExtendedModifier;
47
import org.eclipse.jdt.core.dom.Modifier;
45
import org.eclipse.jdt.core.dom.Modifier;
48
import org.eclipse.jdt.core.dom.SimpleName;
49
import org.eclipse.jdt.core.dom.Type;
50
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
51
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
46
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
47
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
52
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
48
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
53
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
54
import org.eclipse.jdt.core.search.IJavaSearchConstants;
49
import org.eclipse.jdt.core.search.IJavaSearchConstants;
55
import org.eclipse.jdt.core.search.IJavaSearchScope;
50
import org.eclipse.jdt.core.search.IJavaSearchScope;
56
import org.eclipse.jdt.core.search.SearchMatch;
51
import org.eclipse.jdt.core.search.SearchMatch;
57
import org.eclipse.jdt.core.search.SearchPattern;
52
import org.eclipse.jdt.core.search.SearchPattern;
58
53
59
import org.eclipse.jdt.internal.corext.dom.ModifierRewrite;
54
import org.eclipse.jdt.internal.corext.dom.ModifierRewrite;
55
import org.eclipse.jdt.internal.corext.dom.VariableDeclarationRewrite;
60
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
56
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
61
import org.eclipse.jdt.internal.corext.refactoring.RefactoringScopeFactory;
57
import org.eclipse.jdt.internal.corext.refactoring.RefactoringScopeFactory;
62
import org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2;
58
import org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2;
Lines 168-199 Link Here
168
			if (fMember instanceof IField && !Flags.isEnum(fMember.getFlags())) {
164
			if (fMember instanceof IField && !Flags.isEnum(fMember.getFlags())) {
169
				final VariableDeclarationFragment fragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) fMember, root);
165
				final VariableDeclarationFragment fragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) fMember, root);
170
				final FieldDeclaration declaration= (FieldDeclaration) fragment.getParent();
166
				final FieldDeclaration declaration= (FieldDeclaration) fragment.getParent();
171
				if (declaration.fragments().size() == 1)
167
				VariableDeclarationFragment[] fragmentsToChange= new VariableDeclarationFragment[] { fragment };
172
					ModifierRewrite.create(rewrite, declaration).setVisibility(visibility, group);
168
				VariableDeclarationRewrite.rewriteModifiers(declaration, fragmentsToChange, visibility, ModifierRewrite.VISIBILITY_MODIFIERS, rewrite, group);
173
				else {
174
					final VariableDeclarationFragment newFragment= rewrite.getAST().newVariableDeclarationFragment();
175
					newFragment.setName((SimpleName) rewrite.createCopyTarget(fragment.getName()));
176
					final FieldDeclaration newDeclaration= rewrite.getAST().newFieldDeclaration(newFragment);
177
					newDeclaration.setType((Type) rewrite.createCopyTarget(declaration.getType()));
178
					IExtendedModifier extended= null;
179
					for (final Iterator iterator= declaration.modifiers().iterator(); iterator.hasNext();) {
180
						extended= (IExtendedModifier) iterator.next();
181
						if (extended.isModifier()) {
182
							final Modifier modifier= (Modifier) extended;
183
							final int flag= modifier.getKeyword().toFlagValue();
184
							if ((flag & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE)) != 0)
185
								continue;
186
						}
187
						newDeclaration.modifiers().add(rewrite.createCopyTarget((ASTNode) extended));
188
					}
189
					ModifierRewrite.create(rewrite, newDeclaration).setVisibility(visibility, group);
190
					final AbstractTypeDeclaration type= (AbstractTypeDeclaration) declaration.getParent();
191
					rewrite.getListRewrite(type, type.getBodyDeclarationsProperty()).insertAfter(newDeclaration, declaration, null);
192
					final ListRewrite list= rewrite.getListRewrite(declaration, FieldDeclaration.FRAGMENTS_PROPERTY);
193
					list.remove(fragment, group);
194
					if (list.getRewrittenList().isEmpty())
195
						rewrite.remove(declaration, null);
196
				}
197
				if (status != null)
169
				if (status != null)
198
					adjustor.fStatus.merge(status);
170
					adjustor.fStatus.merge(status);
199
			} else if (fMember != null) {
171
			} else if (fMember != null) {
Lines 448-454 Link Here
448
	}
420
	}
449
421
450
	/** The map of members to visibility adjustments */
422
	/** The map of members to visibility adjustments */
451
	private Map fAdjustments= new HashMap();
423
	private Map fAdjustments= new LinkedHashMap(); // LinkedHashMap to preserve order of generated warnings
452
424
453
	/** Should incoming references be adjusted? */
425
	/** Should incoming references be adjusted? */
454
	private boolean fIncoming= true;
426
	private boolean fIncoming= true;
(-)resources/MoveInnerToTopLevel/test39/in/A.java (+9 lines)
Added Link Here
1
package p;
2
3
public class A {
4
	class B {
5
		private static final String TAG1= "tag1", TAG2= "tag2";
6
	}
7
8
	String X= B.TAG1, Y= B.TAG2;
9
}
(-)resources/MoveInnerToTopLevel/test39/out/A.java (+5 lines)
Added Link Here
1
package p;
2
3
public class A {
4
	String X= B.TAG1, Y= B.TAG2;
5
}
(-)resources/MoveInnerToTopLevel/test39/out/B.java (+5 lines)
Added Link Here
1
package p;
2
class B {
3
	static final String TAG1= "tag1";
4
	static final String TAG2= "tag2";
5
}
(-)resources/MoveInnerToTopLevel/test40/in/A.java (+10 lines)
Added Link Here
1
package p;
2
3
public class A {
4
	class B {
5
		@Deprecated
6
		private static final String TAG1= "tag1", TAG_m= null, TAG2= "tag2";
7
	}
8
9
	String X= B.TAG1, Y= B.TAG2;
10
}
(-)resources/MoveInnerToTopLevel/test40/out/A.java (+5 lines)
Added Link Here
1
package p;
2
3
public class A {
4
	String X= B.TAG1, Y= B.TAG2;
5
}
(-)resources/MoveInnerToTopLevel/test40/out/B.java (+9 lines)
Added Link Here
1
package p;
2
class B {
3
	@Deprecated
4
	static final String TAG1= "tag1";
5
	@Deprecated
6
	private static final String TAG_m= null;
7
	@Deprecated
8
	static final String TAG2= "tag2";
9
}
(-)resources/MoveInnerToTopLevel/test41/in/A.java (+10 lines)
Added Link Here
1
package p;
2
3
public class A {
4
	class B {
5
		@Deprecated
6
		private static @Stuff final String TAG1= "tag1", TAG2, TAG_END= "z" + 9;
7
	}
8
9
	String X= B.TAG1, Y= B.TAG2;
10
}
(-)resources/MoveInnerToTopLevel/test41/out/A.java (+5 lines)
Added Link Here
1
package p;
2
3
public class A {
4
	String X= B.TAG1, Y= B.TAG2;
5
}
(-)resources/MoveInnerToTopLevel/test41/out/B.java (+11 lines)
Added Link Here
1
package p;
2
class B {
3
	@Deprecated
4
	static @Stuff final String TAG1= "tag1";
5
	@Deprecated
6
	@Stuff
7
	static final String TAG2;
8
	@Deprecated
9
	@Stuff
10
	private static final String TAG_END= "z" + 9;
11
}
(-)test cases/org/eclipse/jdt/ui/tests/refactoring/MoveInnerToTopLevelTests.java (+15 lines)
Lines 357-362 Link Here
357
		validatePassingTest("A", "B", "p", new String[] { "A"}, new String[] { "p"}, null, false, false, false, false);
357
		validatePassingTest("A", "B", "p", new String[] { "A"}, new String[] { "p"}, null, false, false, false, false);
358
	}
358
	}
359
359
360
	// change visibility: https://bugs.eclipse.org/319069
361
	public void test39() throws Exception {
362
		validatePassingTest("A", "B", "p", new String[] { "A"}, new String[] { "p"}, null, false, true, false, false);
363
	}
364
	
365
	// change visibility: https://bugs.eclipse.org/319069
366
	public void test40() throws Exception {
367
		validatePassingTest("A", "B", "p", new String[] { "A"}, new String[] { "p"}, null, false, true, false, false);
368
	}
369
	
370
	// change visibility: https://bugs.eclipse.org/319069
371
	public void test41() throws Exception {
372
		validatePassingTest("A", "B", "p", new String[] { "A"}, new String[] { "p"}, null, false, true, false, false);
373
	}
374
	
360
	// --- Non static
375
	// --- Non static
361
376
362
	public void test_nonstatic_0() throws Exception{
377
	public void test_nonstatic_0() throws Exception{

Return to bug 319069