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

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/PromoteTempToFieldRefactoring.java (-7 / +26 lines)
Lines 6-11 Link Here
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Timo Kinnunen - Contribution for bug 432147 - [refactoring] Extract Constant displays error message on name of local variable
9
 *     IBM Corporation - initial API and implementation
10
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.corext.refactoring.code;
12
package org.eclipse.jdt.internal.corext.refactoring.code;
Lines 56-61 Link Here
56
import org.eclipse.jdt.core.dom.IMethodBinding;
57
import org.eclipse.jdt.core.dom.IMethodBinding;
57
import org.eclipse.jdt.core.dom.ITypeBinding;
58
import org.eclipse.jdt.core.dom.ITypeBinding;
58
import org.eclipse.jdt.core.dom.IVariableBinding;
59
import org.eclipse.jdt.core.dom.IVariableBinding;
60
import org.eclipse.jdt.core.dom.Initializer;
59
import org.eclipse.jdt.core.dom.Javadoc;
61
import org.eclipse.jdt.core.dom.Javadoc;
60
import org.eclipse.jdt.core.dom.MethodDeclaration;
62
import org.eclipse.jdt.core.dom.MethodDeclaration;
61
import org.eclipse.jdt.core.dom.Modifier;
63
import org.eclipse.jdt.core.dom.Modifier;
Lines 257-263 Link Here
257
			return  canEnableSettingDeclareInConstructors() && ! tempHasAssignmentsOtherThanInitialization();
259
			return  canEnableSettingDeclareInConstructors() && ! tempHasAssignmentsOtherThanInitialization();
258
		else if (fInitializeIn == INITIALIZE_IN_FIELD)
260
		else if (fInitializeIn == INITIALIZE_IN_FIELD)
259
			return  canEnableSettingDeclareInFieldDeclaration() && ! tempHasAssignmentsOtherThanInitialization();
261
			return  canEnableSettingDeclareInFieldDeclaration() && ! tempHasAssignmentsOtherThanInitialization();
260
		else	if (getMethodDeclaration().isConstructor())
262
		else	if (isDeclaredInConstructor())
261
			return  !tempHasAssignmentsOtherThanInitialization();
263
			return  !tempHasAssignmentsOtherThanInitialization();
262
		else
264
		else
263
			return false;
265
			return false;
Lines 272-281 Link Here
272
	public boolean canEnableSettingDeclareInConstructors(){
274
	public boolean canEnableSettingDeclareInConstructors(){
273
		return ! fDeclareStatic &&
275
		return ! fDeclareStatic &&
274
				! fInitializerUsesLocalTypes &&
276
				! fInitializerUsesLocalTypes &&
275
				! getMethodDeclaration().isConstructor() &&
277
				! isDeclaredInConstructor() &&
276
				! isDeclaredInAnonymousClass() &&
278
				! isDeclaredInAnonymousClass() &&
277
				! isTempDeclaredInStaticMethod() &&
279
				! isTempDeclaredInStaticMethod() &&
278
				tempHasInitializer();
280
				tempHasInitializer();
281
	}
282
283
	private boolean isDeclaredInConstructor() {
284
		BodyDeclaration methodDeclaration= getMethodDeclaration();
285
		if(methodDeclaration instanceof MethodDeclaration) {
286
			return ((MethodDeclaration) methodDeclaration).isConstructor();
287
		}
288
		return false;
279
	}
289
	}
280
290
281
	public boolean canEnableSettingDeclareInMethod(){
291
	public boolean canEnableSettingDeclareInMethod(){
Lines 298-305 Link Here
298
    	return Modifier.isStatic(getMethodDeclaration().getModifiers());
308
    	return Modifier.isStatic(getMethodDeclaration().getModifiers());
299
    }
309
    }
300
310
301
    private MethodDeclaration getMethodDeclaration(){
311
    private BodyDeclaration getMethodDeclaration(){
302
    	return (MethodDeclaration)ASTNodes.getParent(fTempDeclarationNode, MethodDeclaration.class);
312
    	return (BodyDeclaration)ASTNodes.getParent(fTempDeclarationNode, BodyDeclaration.class);
303
    }
313
    }
304
314
305
    private boolean isDeclaredInAnonymousClass() {
315
    private boolean isDeclaredInAnonymousClass() {
Lines 322-328 Link Here
322
		if (fTempDeclarationNode == null)
332
		if (fTempDeclarationNode == null)
323
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_select_declaration);
333
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_select_declaration);
324
334
325
		if (! Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class))
335
		if (! Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class) && !Checks.isDeclaredIn(fTempDeclarationNode, Initializer.class) )
326
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_only_declared_in_methods);
336
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_only_declared_in_methods);
327
337
328
		if (isMethodParameter())
338
		if (isMethodParameter())
Lines 401-407 Link Here
401
    	if (initializer == null)
411
    	if (initializer == null)
402
	        return;
412
	        return;
403
413
404
		IMethodBinding declaringMethodBinding= getMethodDeclaration().resolveBinding();
414
		IMethodBinding declaringMethodBinding= getMethodBinding();
405
		ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
415
		ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
406
	    LocalTypeAndVariableUsageAnalyzer localTypeAnalyer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
416
	    LocalTypeAndVariableUsageAnalyzer localTypeAnalyer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
407
	    initializer.accept(localTypeAnalyer);
417
	    initializer.accept(localTypeAnalyer);
Lines 417-423 Link Here
417
    	if (binding == null)
427
    	if (binding == null)
418
    		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
428
    		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
419
429
420
		IMethodBinding declaringMethodBinding= getMethodDeclaration().resolveBinding();
430
		IMethodBinding declaringMethodBinding= getMethodBinding();
421
		ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
431
		ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
422
		LocalTypeAndVariableUsageAnalyzer analyzer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
432
		LocalTypeAndVariableUsageAnalyzer analyzer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
423
		type.accept(analyzer);
433
		type.accept(analyzer);
Lines 427-432 Link Here
427
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_uses_type_declared_locally);
437
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_uses_type_declared_locally);
428
		return null;
438
		return null;
429
    }
439
    }
440
441
	private IMethodBinding getMethodBinding() {
442
		BodyDeclaration methodDeclaration= getMethodDeclaration();
443
		return methodDeclaration != null && methodDeclaration instanceof MethodDeclaration ? ((MethodDeclaration) methodDeclaration).resolveBinding() : null;
444
	}
430
445
431
    private VariableDeclarationStatement getTempDeclarationStatement() {
446
    private VariableDeclarationStatement getTempDeclarationStatement() {
432
        return (VariableDeclarationStatement) ASTNodes.getParent(fTempDeclarationNode, VariableDeclarationStatement.class);
447
        return (VariableDeclarationStatement) ASTNodes.getParent(fTempDeclarationNode, VariableDeclarationStatement.class);
Lines 983-986 Link Here
983
	public void setLinkedProposalModel(LinkedProposalModel model) {
998
	public void setLinkedProposalModel(LinkedProposalModel model) {
984
		fLinkedProposalModel= model;
999
		fLinkedProposalModel= model;
985
	}
1000
	}
1001
1002
	public void setSelfInitializing(boolean value) {
1003
		fSelfInitializing = value;
1004
	}
986
}
1005
}
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/ExtractConstantAction.java (-1 / +50 lines)
Lines 6-30 Link Here
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Timo Kinnunen - Contribution for bug 432147 - [refactoring] Extract Constant displays error message on name of local variable
9
 *     IBM Corporation - initial API and implementation
10
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.ui.actions;
12
package org.eclipse.jdt.ui.actions;
13
14
import org.eclipse.core.runtime.CoreException;
15
import org.eclipse.core.runtime.NullProgressMonitor;
12
16
13
import org.eclipse.jface.text.ITextSelection;
17
import org.eclipse.jface.text.ITextSelection;
14
18
15
import org.eclipse.ui.PlatformUI;
19
import org.eclipse.ui.PlatformUI;
16
20
21
import org.eclipse.jdt.core.ICompilationUnit;
22
import org.eclipse.jdt.core.JavaModelException;
23
import org.eclipse.jdt.core.SourceRange;
24
import org.eclipse.jdt.core.dom.ASTNode;
25
import org.eclipse.jdt.core.dom.CompilationUnit;
26
import org.eclipse.jdt.core.dom.SimpleName;
27
import org.eclipse.jdt.core.dom.VariableDeclaration;
28
29
import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory;
30
import org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment;
17
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
31
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
18
import org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring;
32
import org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring;
33
import org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring;
34
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
35
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
19
36
20
import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper;
37
import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper;
21
38
22
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
40
import org.eclipse.jdt.internal.ui.JavaPlugin;
23
import org.eclipse.jdt.internal.ui.actions.ActionUtil;
41
import org.eclipse.jdt.internal.ui.actions.ActionUtil;
24
import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
42
import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
25
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
43
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
26
import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
44
import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
27
import org.eclipse.jdt.internal.ui.refactoring.ExtractConstantWizard;
45
import org.eclipse.jdt.internal.ui.refactoring.ExtractConstantWizard;
46
import org.eclipse.jdt.internal.ui.refactoring.PromoteTempWizard;
28
import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
47
import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
29
import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
48
import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
30
49
Lines 84-90 Link Here
84
	public void run(ITextSelection selection) {
103
	public void run(ITextSelection selection) {
85
		if (!ActionUtil.isEditable(fEditor))
104
		if (!ActionUtil.isEditable(fEditor))
86
			return;
105
			return;
87
		ExtractConstantRefactoring refactoring= new ExtractConstantRefactoring(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength());
106
		ICompilationUnit unit= SelectionConverter.getInputAsCompilationUnit(fEditor);
107
		CompilationUnit cuNode= RefactoringASTParser.parseWithASTProvider(unit, true, null);
108
		int selectionStart= selection.getOffset();
109
		int selectionLength= selection.getLength();
110
		ExtractConstantRefactoring refactoring= new ExtractConstantRefactoring(cuNode, selectionStart, selectionLength);
111
		try {
112
			CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(unit, cuNode);
113
			SourceRange range= new SourceRange(selectionStart, selectionLength);
114
			IASTFragment ast= ASTFragmentFactory.createFragmentForSourceRange(range, cuRewrite.getRoot(), unit);
115
			ASTNode node= ast.getAssociatedNode();
116
			if (node instanceof SimpleName && node.getParent() instanceof VariableDeclaration) {
117
				ICompilationUnit cunit= SelectionConverter.getInputAsCompilationUnit(fEditor);
118
				PromoteTempToFieldRefactoring refactoring2= new PromoteTempToFieldRefactoring(cunit, selection.getOffset(), selection.getLength());
119
				if (refactoring2.checkInitialConditions(new NullProgressMonitor()).isOK() && refactoring2.canEnableSettingDeclareInFieldDeclaration()) {
120
					refactoring2.setInitializeIn(PromoteTempToFieldRefactoring.INITIALIZE_IN_FIELD);
121
					if (refactoring2.canEnableSettingFinal() && refactoring2.canEnableSettingStatic()) {
122
						refactoring2.setDeclareFinal(true);
123
						refactoring2.setDeclareStatic(true);
124
						refactoring2.setFieldName(refactoring.guessConstantName());
125
						refactoring2.setSelfInitializing(true);
126
					}
127
128
				}
129
				new RefactoringStarter().activate(new PromoteTempWizard(refactoring2), getShell(), RefactoringMessages.ConvertLocalToField_title, RefactoringSaveHelper.SAVE_NOTHING);
130
				return;
131
			}
132
		} catch (JavaModelException e) {
133
			JavaPlugin.log(e);
134
		} catch (CoreException e) {
135
			JavaPlugin.log(e);
136
		}
88
		new RefactoringStarter().activate(new ExtractConstantWizard(refactoring), getShell(), RefactoringMessages.ExtractConstantAction_extract_constant, RefactoringSaveHelper.SAVE_NOTHING);
137
		new RefactoringStarter().activate(new ExtractConstantWizard(refactoring), getShell(), RefactoringMessages.ExtractConstantAction_extract_constant, RefactoringSaveHelper.SAVE_NOTHING);
89
	}
138
	}
90
}
139
}

Return to bug 432147