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 |
} |