KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > PromoteTempToFieldRefactoring


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.code;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.eclipse.text.edits.TextEdit;
22
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26
27 import org.eclipse.ltk.core.refactoring.Change;
28 import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
29 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
30 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
31 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
32 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
33
34 import org.eclipse.jdt.core.ICompilationUnit;
35 import org.eclipse.jdt.core.IJavaElement;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.JavaModelException;
38 import org.eclipse.jdt.core.dom.AST;
39 import org.eclipse.jdt.core.dom.ASTNode;
40 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
41 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
42 import org.eclipse.jdt.core.dom.ArrayCreation;
43 import org.eclipse.jdt.core.dom.ArrayInitializer;
44 import org.eclipse.jdt.core.dom.ArrayType;
45 import org.eclipse.jdt.core.dom.Assignment;
46 import org.eclipse.jdt.core.dom.Block;
47 import org.eclipse.jdt.core.dom.CatchClause;
48 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
49 import org.eclipse.jdt.core.dom.CompilationUnit;
50 import org.eclipse.jdt.core.dom.ConstructorInvocation;
51 import org.eclipse.jdt.core.dom.Expression;
52 import org.eclipse.jdt.core.dom.ExpressionStatement;
53 import org.eclipse.jdt.core.dom.FieldDeclaration;
54 import org.eclipse.jdt.core.dom.IBinding;
55 import org.eclipse.jdt.core.dom.IMethodBinding;
56 import org.eclipse.jdt.core.dom.ITypeBinding;
57 import org.eclipse.jdt.core.dom.IVariableBinding;
58 import org.eclipse.jdt.core.dom.Javadoc;
59 import org.eclipse.jdt.core.dom.MethodDeclaration;
60 import org.eclipse.jdt.core.dom.Modifier;
61 import org.eclipse.jdt.core.dom.SimpleName;
62 import org.eclipse.jdt.core.dom.Type;
63 import org.eclipse.jdt.core.dom.TypeDeclaration;
64 import org.eclipse.jdt.core.dom.VariableDeclaration;
65 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
66 import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
67 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
68 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
69 import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
70
71 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
72 import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
73 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
74 import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;
75 import org.eclipse.jdt.internal.corext.dom.ModifierRewrite;
76 import org.eclipse.jdt.internal.corext.fix.LinkedProposalModel;
77 import org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup;
78 import org.eclipse.jdt.internal.corext.refactoring.Checks;
79 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptor;
80 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
81 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
82 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
83 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
84 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
85 import org.eclipse.jdt.internal.corext.refactoring.changes.TextChangeCompatibility;
86 import org.eclipse.jdt.internal.corext.refactoring.rename.TempDeclarationFinder;
87 import org.eclipse.jdt.internal.corext.refactoring.rename.TempOccurrenceAnalyzer;
88 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
89 import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil;
90 import org.eclipse.jdt.internal.corext.util.JdtFlags;
91 import org.eclipse.jdt.internal.corext.util.Messages;
92
93 import org.eclipse.jdt.ui.CodeGeneration;
94 import org.eclipse.jdt.ui.JavaElementLabels;
95
96 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
97
98 public class PromoteTempToFieldRefactoring extends ScriptableRefactoring {
99
100     private static final String JavaDoc ATTRIBUTE_STATIC= "static"; //$NON-NLS-1$
101
private static final String JavaDoc ATTRIBUTE_FINAL= "final"; //$NON-NLS-1$
102
private static final String JavaDoc ATTRIBUTE_VISIBILITY= "visibility"; //$NON-NLS-1$
103
private static final String JavaDoc ATTRIBUTE_INITIALIZE= "initialize"; //$NON-NLS-1$
104

105     private int fSelectionStart;
106     private int fSelectionLength;
107     private ICompilationUnit fCu;
108     
109     public static final int INITIALIZE_IN_FIELD= 0;
110     public static final int INITIALIZE_IN_METHOD= 1;
111     public static final int INITIALIZE_IN_CONSTRUCTOR= 2;
112     
113     private static final String JavaDoc LINKED_NAME= "name"; //$NON-NLS-1$
114

115     //------ settings ---------//
116
private String JavaDoc fFieldName;
117     private int fVisibility; /*see Modifier*/
118     private boolean fDeclareStatic;
119     private boolean fDeclareFinal;
120     private int fInitializeIn; /*see INITIALIZE_IN_* constraints */
121
122     //------ fields used for computations ---------//
123
private CompilationUnit fCompilationUnitNode;
124     private VariableDeclaration fTempDeclarationNode;
125     //------ analysis ---------//
126
private boolean fInitializerUsesLocalTypes;
127     private boolean fTempTypeUsesClassTypeVariables;
128     //------ scripting --------//
129
private boolean fSelfInitializing= false;
130     private LinkedProposalModel fLinkedProposalModel;
131
132     /**
133      * Creates a new promote temp to field refactoring.
134      * @param unit the compilation unit, or <code>null</code> if invoked by scripting
135      * @param selectionStart
136      * @param selectionLength
137      */

138     public PromoteTempToFieldRefactoring(ICompilationUnit unit, int selectionStart, int selectionLength){
139         Assert.isTrue(selectionStart >= 0);
140         Assert.isTrue(selectionLength >= 0);
141         fSelectionStart= selectionStart;
142         fSelectionLength= selectionLength;
143         fCu= unit;
144         
145         fFieldName= ""; //$NON-NLS-1$
146
fVisibility= Modifier.PRIVATE;
147         fDeclareStatic= false;
148         fDeclareFinal= false;
149         fInitializeIn= INITIALIZE_IN_METHOD;
150         fLinkedProposalModel= null;
151     }
152     
153     /**
154      * Creates a new promote temp to field refactoring.
155      * @param declaration the variable declaration node to convert to a field
156      */

157     public PromoteTempToFieldRefactoring(VariableDeclaration declaration) {
158         Assert.isTrue(declaration != null);
159         fTempDeclarationNode= declaration;
160         IVariableBinding resolveBinding= declaration.resolveBinding();
161         Assert.isTrue(resolveBinding != null && !resolveBinding.isParameter() && !resolveBinding.isField());
162
163         ASTNode root= declaration.getRoot();
164         Assert.isTrue(root instanceof CompilationUnit);
165         fCompilationUnitNode= (CompilationUnit) root;
166         
167         IJavaElement input= fCompilationUnitNode.getJavaElement();
168         Assert.isTrue(input instanceof ICompilationUnit);
169         fCu= (ICompilationUnit) input;
170
171         fSelectionStart= declaration.getStartPosition();
172         fSelectionLength= declaration.getLength();
173         
174         fFieldName= ""; //$NON-NLS-1$
175
fVisibility= Modifier.PRIVATE;
176         fDeclareStatic= false;
177         fDeclareFinal= false;
178         fInitializeIn= INITIALIZE_IN_METHOD;
179         fLinkedProposalModel= null;
180     }
181
182     public String JavaDoc getName() {
183         return RefactoringCoreMessages.PromoteTempToFieldRefactoring_name;
184     }
185     
186     public int[] getAvailableVisibilities(){
187         return new int[]{Modifier.PUBLIC, Modifier.PROTECTED, Modifier.NONE, Modifier.PRIVATE};
188     }
189     
190     public int getVisibility() {
191         return fVisibility;
192     }
193
194     public boolean getDeclareFinal() {
195         return fDeclareFinal;
196     }
197
198     public boolean getDeclareStatic() {
199         return fDeclareStatic;
200     }
201
202     public int getInitializeIn() {
203         return fInitializeIn;
204     }
205
206     public void setVisibility(int accessModifier) {
207         Assert.isTrue(accessModifier == Modifier.PRIVATE ||
208                         accessModifier == Modifier.NONE ||
209                         accessModifier == Modifier.PROTECTED ||
210                         accessModifier == Modifier.PUBLIC);
211         fVisibility= accessModifier;
212     }
213
214     public void setDeclareFinal(boolean declareFinal) {
215         fDeclareFinal= declareFinal;
216     }
217
218     public void setDeclareStatic(boolean declareStatic) {
219         fDeclareStatic= declareStatic;
220     }
221
222     public void setFieldName(String JavaDoc fieldName) {
223         Assert.isNotNull(fieldName);
224         fFieldName= fieldName;
225     }
226
227     public void setInitializeIn(int initializeIn) {
228         Assert.isTrue( initializeIn == INITIALIZE_IN_CONSTRUCTOR ||
229                         initializeIn == INITIALIZE_IN_FIELD ||
230                         initializeIn == INITIALIZE_IN_METHOD);
231         fInitializeIn= initializeIn;
232     }
233     
234     public boolean canEnableSettingStatic(){
235         return fInitializeIn != INITIALIZE_IN_CONSTRUCTOR &&
236                 ! isTempDeclaredInStaticMethod() &&
237                 ! fTempTypeUsesClassTypeVariables;
238     }
239     
240     public boolean canEnableSettingFinal(){
241         if (fInitializeIn == INITIALIZE_IN_CONSTRUCTOR)
242             return canEnableSettingDeclareInConstructors() && ! tempHasAssignmentsOtherThanInitialization();
243         else if (fInitializeIn == INITIALIZE_IN_FIELD)
244             return canEnableSettingDeclareInFieldDeclaration() && ! tempHasAssignmentsOtherThanInitialization();
245         else if (getMethodDeclaration().isConstructor())
246             return !tempHasAssignmentsOtherThanInitialization();
247         else
248             return false;
249     }
250     
251     private boolean tempHasAssignmentsOtherThanInitialization() {
252         TempAssignmentFinder assignmentFinder= new TempAssignmentFinder(fTempDeclarationNode);
253         fCompilationUnitNode.accept(assignmentFinder);
254         return assignmentFinder.hasAssignments();
255     }
256     
257     public boolean canEnableSettingDeclareInConstructors(){
258         return ! fDeclareStatic &&
259                 ! fInitializerUsesLocalTypes &&
260                 ! getMethodDeclaration().isConstructor() &&
261                 ! isDeclaredInAnonymousClass() &&
262                 ! isTempDeclaredInStaticMethod() &&
263                 tempHasInitializer();
264     }
265     
266     public boolean canEnableSettingDeclareInMethod(){
267         return ! fDeclareFinal &&
268                 tempHasInitializer();
269     }
270     private boolean tempHasInitializer() {
271         return getTempInitializer() != null;
272     }
273
274     public boolean canEnableSettingDeclareInFieldDeclaration(){
275         return ! fInitializerUsesLocalTypes && tempHasInitializer();
276     }
277
278     private Expression getTempInitializer() {
279         return fTempDeclarationNode.getInitializer();
280     }
281
282     private boolean isTempDeclaredInStaticMethod() {
283         return Modifier.isStatic(getMethodDeclaration().getModifiers());
284     }
285     
286     private MethodDeclaration getMethodDeclaration(){
287         return (MethodDeclaration)ASTNodes.getParent(fTempDeclarationNode, MethodDeclaration.class);
288     }
289
290     private boolean isDeclaredInAnonymousClass() {
291         return null != ASTNodes.getParent(fTempDeclarationNode, AnonymousClassDeclaration.class);
292     }
293     
294     /*
295      * @see org.eclipse.jdt.internal.corext.refactoring.base.Refactoring#checkActivation(org.eclipse.core.runtime.IProgressMonitor)
296      */

297     public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
298         RefactoringStatus result= Checks.validateModifiesFiles(
299             ResourceUtil.getFiles(new ICompilationUnit[]{fCu}),
300             getValidationContext());
301         if (result.hasFatalError())
302             return result;
303
304         initAST(pm);
305
306         if (fTempDeclarationNode == null)
307             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_select_declaration);
308         
309         if (! Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class))
310             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_only_declared_in_methods);
311         
312         if (isMethodParameter())
313             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_method_parameters);
314
315         if (isTempAnExceptionInCatchBlock())
316             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_exceptions);
317
318         result.merge(checkTempTypeForLocalTypeUsage());
319         if (result.hasFatalError())
320             return result;
321         
322         checkTempInitializerForLocalTypeUsage();
323         
324         if (!fSelfInitializing)
325             initializeDefaults();
326         return result;
327     }
328     
329     private void initializeDefaults() {
330         fVisibility= Modifier.PRIVATE;
331         fDeclareStatic= Modifier.isStatic(getMethodDeclaration().getModifiers());
332         fDeclareFinal= false;
333         if (canEnableSettingDeclareInMethod())
334             fInitializeIn= INITIALIZE_IN_METHOD;
335         else if (canEnableSettingDeclareInFieldDeclaration())
336             fInitializeIn= INITIALIZE_IN_FIELD;
337         else if (canEnableSettingDeclareInConstructors())
338             fInitializeIn= INITIALIZE_IN_CONSTRUCTOR;
339     }
340     
341     public String JavaDoc[] guessFieldNames() {
342         String JavaDoc rawTempName= StubUtility.removePrefixAndSuffixForVariable(fCu.getJavaProject(), fTempDeclarationNode.resolveBinding());
343         String JavaDoc[] excludedNames= getNamesOfFieldsInDeclaringType();
344         int dim= ASTNodes.getDimensions(fTempDeclarationNode);
345         return StubUtility.getFieldNameSuggestions(fCu.getJavaProject(), rawTempName, dim, getModifiers(), excludedNames);
346     }
347
348     private String JavaDoc getInitialFieldName() {
349         String JavaDoc[] suggestedNames= guessFieldNames();
350         if (suggestedNames.length > 0) {
351             if (fLinkedProposalModel != null) {
352                 LinkedProposalPositionGroup nameGroup= fLinkedProposalModel.getPositionGroup(LINKED_NAME, true);
353                 for (int i= 0; i < suggestedNames.length; i++) {
354                     nameGroup.addProposal(suggestedNames[i], null, suggestedNames.length - i);
355                 }
356             }
357             return suggestedNames[0];
358         } else {
359             return fTempDeclarationNode.getName().getIdentifier();
360         }
361     }
362
363     private String JavaDoc[] getNamesOfFieldsInDeclaringType() {
364         final AbstractTypeDeclaration type= getEnclosingType();
365         if (type instanceof TypeDeclaration) {
366             FieldDeclaration[] fields= ((TypeDeclaration) type).getFields();
367             List JavaDoc result= new ArrayList JavaDoc(fields.length);
368             for (int i= 0; i < fields.length; i++) {
369                 for (Iterator JavaDoc iter= fields[i].fragments().iterator(); iter.hasNext();) {
370                     VariableDeclarationFragment field= (VariableDeclarationFragment) iter.next();
371                     result.add(field.getName().getIdentifier());
372                 }
373             }
374             return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
375         }
376         return new String JavaDoc[] {};
377     }
378         
379     private void checkTempInitializerForLocalTypeUsage() {
380         Expression initializer= fTempDeclarationNode.getInitializer();
381         if (initializer == null)
382             return;
383         
384         IMethodBinding declaringMethodBinding= getMethodDeclaration().resolveBinding();
385         ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
386         LocalTypeAndVariableUsageAnalyzer localTypeAnalyer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
387         initializer.accept(localTypeAnalyer);
388         fInitializerUsesLocalTypes= ! localTypeAnalyer.getUsageOfEnclosingNodes().isEmpty();
389     }
390     
391     private RefactoringStatus checkTempTypeForLocalTypeUsage(){
392         VariableDeclarationStatement vds= getTempDeclarationStatement();
393         if (vds == null)
394             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
395         Type type= vds.getType();
396         ITypeBinding binding= type.resolveBinding();
397         if (binding == null)
398             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
399         
400         IMethodBinding declaringMethodBinding= getMethodDeclaration().resolveBinding();
401         ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
402         LocalTypeAndVariableUsageAnalyzer analyzer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
403         type.accept(analyzer);
404         boolean usesLocalTypes= ! analyzer.getUsageOfEnclosingNodes().isEmpty();
405         fTempTypeUsesClassTypeVariables= analyzer.getClassTypeVariablesUsed();
406         if (usesLocalTypes)
407             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_uses_type_declared_locally);
408         return null;
409     }
410     
411     private VariableDeclarationStatement getTempDeclarationStatement() {
412         return (VariableDeclarationStatement) ASTNodes.getParent(fTempDeclarationNode, VariableDeclarationStatement.class);
413     }
414     
415     private boolean isTempAnExceptionInCatchBlock() {
416         return (fTempDeclarationNode.getParent() instanceof CatchClause);
417     }
418     
419     private boolean isMethodParameter() {
420         return (fTempDeclarationNode.getParent() instanceof MethodDeclaration);
421     }
422     
423     private void initAST(IProgressMonitor pm){
424         if (fCompilationUnitNode == null) {
425             fCompilationUnitNode= RefactoringASTParser.parseWithASTProvider(fCu, true, pm);
426             fTempDeclarationNode= TempDeclarationFinder.findTempDeclaration(fCompilationUnitNode, fSelectionStart, fSelectionLength);
427         }
428     }
429
430     public RefactoringStatus validateInput(){
431         return Checks.checkFieldName(fFieldName);
432     }
433     
434     /*
435      * @see org.eclipse.jdt.internal.corext.refactoring.base.Refactoring#checkInput(org.eclipse.core.runtime.IProgressMonitor)
436      */

437     public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
438         try{
439             RefactoringStatus result= new RefactoringStatus();
440             result.merge(checkClashesWithExistingFields());
441             if (fInitializeIn == INITIALIZE_IN_CONSTRUCTOR)
442                 result.merge(checkClashesInConstructors());
443             return result;
444         } finally {
445             pm.done();
446         }
447     }
448
449     private RefactoringStatus checkClashesInConstructors() {
450         Assert.isTrue(fInitializeIn == INITIALIZE_IN_CONSTRUCTOR);
451         Assert.isTrue(!isDeclaredInAnonymousClass());
452         final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) getMethodDeclaration().getParent();
453         if (declaration instanceof TypeDeclaration) {
454             MethodDeclaration[] methods= ((TypeDeclaration) declaration).getMethods();
455             for (int i= 0; i < methods.length; i++) {
456                 MethodDeclaration method= methods[i];
457                 if (!method.isConstructor())
458                     continue;
459                 NameCollector nameCollector= new NameCollector(method) {
460                     protected boolean visitNode(ASTNode node) {
461                         return true;
462                     }
463                 };
464                 method.accept(nameCollector);
465                 List JavaDoc names= nameCollector.getNames();
466                 if (names.contains(fFieldName)) {
467                     String JavaDoc[] keys= { fFieldName, BindingLabelProvider.getBindingLabel(method.resolveBinding(), JavaElementLabels.ALL_FULLY_QUALIFIED)};
468                     String JavaDoc msg= Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict, keys);
469                     return RefactoringStatus.createFatalErrorStatus(msg);
470                 }
471             }
472         }
473         return null;
474     }
475
476     private RefactoringStatus checkClashesWithExistingFields(){
477         FieldDeclaration[] existingFields= getFieldDeclarations(getBodyDeclarationListOfDeclaringType());
478         for (int i= 0; i < existingFields.length; i++) {
479             FieldDeclaration declaration= existingFields[i];
480             VariableDeclarationFragment[] fragments= (VariableDeclarationFragment[]) declaration.fragments().toArray(new VariableDeclarationFragment[declaration.fragments().size()]);
481             for (int j= 0; j < fragments.length; j++) {
482                 VariableDeclarationFragment fragment= fragments[j];
483                 if (fFieldName.equals(fragment.getName().getIdentifier())){
484                     //cannot conflict with more than 1 name
485
RefactoringStatusContext context= JavaStatusContext.create(fCu, fragment);
486                     return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict_with_field, context);
487                 }
488             }
489         }
490         return null;
491     }
492     
493     private ChildListPropertyDescriptor getBodyDeclarationListOfDeclaringType(){
494         ASTNode methodParent= getMethodDeclaration().getParent();
495         if (methodParent instanceof AbstractTypeDeclaration)
496             return ((AbstractTypeDeclaration) methodParent).getBodyDeclarationsProperty();
497         if (methodParent instanceof AnonymousClassDeclaration)
498             return AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY;
499         Assert.isTrue(false);
500         return null;
501     }
502     
503     private FieldDeclaration[] getFieldDeclarations(ChildListPropertyDescriptor descriptor) {
504         final List JavaDoc bodyDeclarations= (List JavaDoc) getMethodDeclaration().getParent().getStructuralProperty(descriptor);
505         List JavaDoc fields= new ArrayList JavaDoc(1);
506         for (Iterator JavaDoc iter= bodyDeclarations.iterator(); iter.hasNext();) {
507             Object JavaDoc each= iter.next();
508             if (each instanceof FieldDeclaration)
509                 fields.add(each);
510         }
511         return (FieldDeclaration[]) fields.toArray(new FieldDeclaration[fields.size()]);
512     }
513
514     /*
515      * @see org.eclipse.jdt.internal.corext.refactoring.base.IRefactoring#createChange(org.eclipse.core.runtime.IProgressMonitor)
516      */

517     public Change createChange(IProgressMonitor pm) throws CoreException {
518         pm.beginTask("", 1); //$NON-NLS-1$
519
try {
520             if (fFieldName.length() == 0) {
521                 fFieldName= getInitialFieldName();
522             }
523             
524             ASTRewrite rewrite= ASTRewrite.create(fCompilationUnitNode.getAST());
525             if (fInitializeIn == INITIALIZE_IN_METHOD && tempHasInitializer())
526                 addLocalDeclarationSplit(rewrite);
527             else
528                 addLocalDeclarationRemoval(rewrite);
529             if (fInitializeIn == INITIALIZE_IN_CONSTRUCTOR)
530                 addInitializersToConstructors(rewrite);
531             addTempRenames(rewrite);
532             addFieldDeclaration(rewrite);
533             return createChange(rewrite);
534         } finally {
535             pm.done();
536         }
537     }
538     
539     private void addTempRenames(ASTRewrite rewrite) {
540         boolean noNameChange= fFieldName.equals(fTempDeclarationNode.getName().getIdentifier());
541         if (fLinkedProposalModel == null && noNameChange) {
542             return; // no changes needed
543
}
544         TempOccurrenceAnalyzer analyzer= new TempOccurrenceAnalyzer(fTempDeclarationNode, false);
545         analyzer.perform();
546         SimpleName[] tempRefs= analyzer.getReferenceNodes(); // no javadocs (refactoring not for parameters)
547

548         
549         for (int j= 0; j < tempRefs.length; j++) {
550             SimpleName occurence= tempRefs[j];
551             if (noNameChange) {
552                 addLinkedName(rewrite, occurence, false);
553             } else {
554                 SimpleName newName= getAST().newSimpleName(fFieldName);
555                 addLinkedName(rewrite, newName, false);
556                 rewrite.replace(occurence, newName, null);
557             }
558         }
559     }
560     
561     private void addInitializersToConstructors(ASTRewrite rewrite) throws CoreException {
562         Assert.isTrue(! isDeclaredInAnonymousClass());
563         final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration)getMethodDeclaration().getParent();
564         final MethodDeclaration[] constructors= getAllConstructors(declaration);
565         if (constructors.length == 0) {
566             AST ast= rewrite.getAST();
567             MethodDeclaration newConstructor= ast.newMethodDeclaration();
568             newConstructor.setConstructor(true);
569             newConstructor.modifiers().addAll(ast.newModifiers(declaration.getModifiers() & ModifierRewrite.VISIBILITY_MODIFIERS));
570             newConstructor.setName(ast.newSimpleName(declaration.getName().getIdentifier()));
571             newConstructor.setJavadoc(getNewConstructorComment(rewrite));
572             newConstructor.setBody(ast.newBlock());
573             
574             addFieldInitializationToConstructor(rewrite, newConstructor);
575             
576             int insertionIndex= computeInsertIndexForNewConstructor(declaration);
577             rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newConstructor, insertionIndex, null);
578         } else {
579             for (int index= 0; index < constructors.length; index++) {
580                 if (shouldInsertTempInitialization(constructors[index]))
581                     addFieldInitializationToConstructor(rewrite, constructors[index]);
582             }
583         }
584     }
585
586     private String JavaDoc getEnclosingTypeName() {
587         return getEnclosingType().getName().getIdentifier();
588     }
589     
590     private AbstractTypeDeclaration getEnclosingType() {
591         return (AbstractTypeDeclaration)ASTNodes.getParent(getTempDeclarationStatement(), AbstractTypeDeclaration.class);
592     }
593
594     private Javadoc getNewConstructorComment(ASTRewrite rewrite) throws CoreException {
595         if (StubUtility.doAddComments(fCu.getJavaProject())){
596             String JavaDoc comment= CodeGeneration.getMethodComment(fCu, getEnclosingTypeName(), getEnclosingTypeName(), new String JavaDoc[0], new String JavaDoc[0], null, null, StubUtility.getLineDelimiterUsed(fCu));
597             if (comment != null && comment.length() > 0) {
598                 return (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
599             }
600         }
601         return null;
602     }
603
604     private int computeInsertIndexForNewConstructor(AbstractTypeDeclaration declaration) {
605         List JavaDoc declarations= declaration.bodyDeclarations();
606         if (declarations.isEmpty())
607             return 0;
608         int index= findFirstMethodIndex(declaration);
609         if (index == -1)
610             return declarations.size();
611         else
612             return index;
613     }
614     
615     private int findFirstMethodIndex(AbstractTypeDeclaration typeDeclaration) {
616         for (int i= 0, n= typeDeclaration.bodyDeclarations().size(); i < n; i++) {
617             if (typeDeclaration.bodyDeclarations().get(i) instanceof MethodDeclaration)
618                 return i;
619         }
620         return -1;
621     }
622     
623     private void addFieldInitializationToConstructor(ASTRewrite rewrite, MethodDeclaration constructor) throws JavaModelException {
624         if (constructor.getBody() == null)
625             constructor.setBody(getAST().newBlock());
626         ExpressionStatement newStatement= createExpressionStatementThatInitializesField(rewrite);
627         rewrite.getListRewrite(constructor.getBody(), Block.STATEMENTS_PROPERTY).insertLast(newStatement, null);
628     }
629     
630     private static boolean shouldInsertTempInitialization(MethodDeclaration constructor){
631         Assert.isTrue(constructor.isConstructor());
632         if (constructor.getBody() == null)
633             return false;
634         List JavaDoc statements= constructor.getBody().statements();
635         if (statements == null)
636             return false;
637         if (statements.size() > 0 && statements.get(0) instanceof ConstructorInvocation)
638             return false;
639         return true;
640     }
641
642     private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) {
643         if (typeDeclaration instanceof TypeDeclaration) {
644             MethodDeclaration[] allMethods= ((TypeDeclaration) typeDeclaration).getMethods();
645             List JavaDoc result= new ArrayList JavaDoc(Math.min(allMethods.length, 1));
646             for (int i= 0; i < allMethods.length; i++) {
647                 MethodDeclaration declaration= allMethods[i];
648                 if (declaration.isConstructor())
649                     result.add(declaration);
650             }
651             return (MethodDeclaration[]) result.toArray(new MethodDeclaration[result.size()]);
652         }
653         return new MethodDeclaration[] {};
654     }
655
656     private Change createChange(ASTRewrite rewrite) throws CoreException {
657         final Map JavaDoc arguments= new HashMap JavaDoc();
658         String JavaDoc project= null;
659         IJavaProject javaProject= fCu.getJavaProject();
660         if (javaProject != null)
661             project= javaProject.getElementName();
662         final IVariableBinding binding= fTempDeclarationNode.resolveBinding();
663         final String JavaDoc description= Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_descriptor_description_short, binding.getName());
664         final String JavaDoc header= Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_descriptor_description, new String JavaDoc[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(binding.getDeclaringMethod(), JavaElementLabels.ALL_FULLY_QUALIFIED)});
665         final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header);
666         comment.addSetting(Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
667         comment.addSetting(Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_field_pattern, fFieldName));
668         switch (fInitializeIn) {
669             case INITIALIZE_IN_CONSTRUCTOR:
670                 comment.addSetting(RefactoringCoreMessages.PromoteTempToFieldRefactoring_initialize_constructor);
671                 break;
672             case INITIALIZE_IN_FIELD:
673                 comment.addSetting(RefactoringCoreMessages.PromoteTempToFieldRefactoring_initialize_declaration);
674                 break;
675             case INITIALIZE_IN_METHOD:
676                 comment.addSetting(RefactoringCoreMessages.PromoteTempToFieldRefactoring_initialize_method);
677                 break;
678         }
679         String JavaDoc visibility= JdtFlags.getVisibilityString(fVisibility);
680         if ("".equals(visibility)) //$NON-NLS-1$
681
visibility= RefactoringCoreMessages.PromoteTempToFieldRefactoring_default_visibility;
682         comment.addSetting(Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_visibility_pattern, visibility));
683         if (fDeclareFinal && fDeclareStatic)
684             comment.addSetting(RefactoringCoreMessages.PromoteTempToFieldRefactoring_declare_final_static);
685         else if (fDeclareFinal)
686             comment.addSetting(RefactoringCoreMessages.PromoteTempToFieldRefactoring_declare_final);
687         else if (fDeclareStatic)
688             comment.addSetting(RefactoringCoreMessages.PromoteTempToFieldRefactoring_declare_static);
689         final JDTRefactoringDescriptor descriptor= new JDTRefactoringDescriptor(IJavaRefactorings.CONVERT_LOCAL_VARIABLE, project, description, comment.asString(), arguments, RefactoringDescriptor.STRUCTURAL_CHANGE);
690         arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_INPUT, descriptor.elementToHandle(fCu));
691         arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_NAME, fFieldName);
692         arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_SELECTION, new Integer JavaDoc(fSelectionStart).toString() + " " + new Integer JavaDoc(fSelectionLength).toString()); //$NON-NLS-1$
693
arguments.put(ATTRIBUTE_STATIC, Boolean.valueOf(fDeclareStatic).toString());
694         arguments.put(ATTRIBUTE_FINAL, Boolean.valueOf(fDeclareFinal).toString());
695         arguments.put(ATTRIBUTE_VISIBILITY, new Integer JavaDoc(fVisibility).toString());
696         arguments.put(ATTRIBUTE_INITIALIZE, new Integer JavaDoc(fInitializeIn).toString());
697         final CompilationUnitChange result= new CompilationUnitChange(RefactoringCoreMessages.PromoteTempToFieldRefactoring_name, fCu);
698         result.setDescriptor(new RefactoringChangeDescriptor(descriptor));
699         TextEdit resultingEdits= rewrite.rewriteAST();
700         TextChangeCompatibility.addTextEdit(result, RefactoringCoreMessages.PromoteTempToFieldRefactoring_editName, resultingEdits);
701         return result;
702     }
703
704     private void addLocalDeclarationSplit(ASTRewrite rewrite) throws JavaModelException {
705         VariableDeclarationStatement tempDeclarationStatement= getTempDeclarationStatement();
706         Block block= (Block)tempDeclarationStatement.getParent();//XXX can it be anything else?
707
int statementIndex= block.statements().indexOf(tempDeclarationStatement);
708         Assert.isTrue(statementIndex != -1);
709         List JavaDoc fragments= tempDeclarationStatement.fragments();
710         int fragmentIndex= fragments.indexOf(fTempDeclarationNode);
711         Assert.isTrue(fragmentIndex != -1);
712
713         for (int i1= fragmentIndex, n = fragments.size(); i1 < n; i1++) {
714             VariableDeclarationFragment fragment= (VariableDeclarationFragment)fragments.get(i1);
715             rewrite.remove(fragment, null);
716         }
717         if (fragmentIndex == 0)
718             rewrite.remove(tempDeclarationStatement, null);
719         
720         Assert.isTrue(tempHasInitializer());
721         rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertAt(createExpressionStatementThatInitializesField(rewrite), statementIndex + 1, null);
722         
723         if (fragmentIndex + 1 < fragments.size()){
724             VariableDeclarationFragment firstFragmentAfter= (VariableDeclarationFragment)fragments.get(fragmentIndex + 1);
725             VariableDeclarationFragment copyfirstFragmentAfter= (VariableDeclarationFragment)rewrite.createCopyTarget(firstFragmentAfter);
726             VariableDeclarationStatement statement= getAST().newVariableDeclarationStatement(copyfirstFragmentAfter);
727             Type type= (Type)rewrite.createCopyTarget(tempDeclarationStatement.getType());
728             statement.setType(type);
729             List JavaDoc modifiers= tempDeclarationStatement.modifiers();
730             if (modifiers.size() > 0) {
731                 ListRewrite modifiersRewrite= rewrite.getListRewrite(tempDeclarationStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
732                 ASTNode firstModifier= (ASTNode) modifiers.get(0);
733                 ASTNode lastModifier= (ASTNode) modifiers.get(modifiers.size() - 1);
734                 ASTNode modifiersCopy= modifiersRewrite.createCopyTarget(firstModifier, lastModifier);
735                 statement.modifiers().add(modifiersCopy);
736             }
737             for (int i= fragmentIndex + 2; i < fragments.size(); i++) {
738                 VariableDeclarationFragment fragment= (VariableDeclarationFragment)fragments.get(i);
739                 VariableDeclarationFragment fragmentCopy= (VariableDeclarationFragment)rewrite.createCopyTarget(fragment);
740                 statement.fragments().add(fragmentCopy);
741             }
742             rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertAt(statement, statementIndex + 2, null);
743         }
744     }
745     
746     private ExpressionStatement createExpressionStatementThatInitializesField(ASTRewrite rewrite) throws JavaModelException{
747         AST ast= getAST();
748         Assignment assignment= ast.newAssignment();
749         SimpleName fieldName= ast.newSimpleName(fFieldName);
750         addLinkedName(rewrite, fieldName, true);
751         assignment.setLeftHandSide(fieldName);
752         assignment.setRightHandSide(getTempInitializerCopy(rewrite));
753         return ast.newExpressionStatement(assignment);
754     }
755
756     private void addLinkedName(ASTRewrite rewrite, SimpleName fieldName, boolean isFirst) {
757         if (fLinkedProposalModel != null) {
758             fLinkedProposalModel.getPositionGroup(LINKED_NAME, true).addPosition(rewrite.track(fieldName), isFirst);
759         }
760     }
761     
762     private Expression getTempInitializerCopy(ASTRewrite rewrite) throws JavaModelException {
763         final Expression initializer= (Expression) rewrite.createCopyTarget(getTempInitializer());
764         if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(fTempDeclarationNode) > 0) {
765             ArrayCreation arrayCreation= rewrite.getAST().newArrayCreation();
766             arrayCreation.setType((ArrayType) ASTNodeFactory.newType(rewrite.getAST(), fTempDeclarationNode));
767             arrayCreation.setInitializer((ArrayInitializer) initializer);
768             return arrayCreation;
769         }
770         return initializer;
771     }
772
773     private void addLocalDeclarationRemoval(ASTRewrite rewrite) {
774         VariableDeclarationStatement tempDeclarationStatement= getTempDeclarationStatement();
775         List JavaDoc fragments= tempDeclarationStatement.fragments();
776
777         int fragmentIndex= fragments.indexOf(fTempDeclarationNode);
778         Assert.isTrue(fragmentIndex != -1);
779         VariableDeclarationFragment fragment= (VariableDeclarationFragment)fragments.get(fragmentIndex);
780         rewrite.remove(fragment, null);
781         if (fragments.size() == 1)
782             rewrite.remove(tempDeclarationStatement, null);
783     }
784
785     private void addFieldDeclaration(ASTRewrite rewrite) {
786         final ChildListPropertyDescriptor descriptor= getBodyDeclarationListOfDeclaringType();
787         FieldDeclaration[] fields= getFieldDeclarations(getBodyDeclarationListOfDeclaringType());
788         final ASTNode parent= getMethodDeclaration().getParent();
789         int insertIndex;
790         if (fields.length == 0)
791             insertIndex= 0;
792         else
793             insertIndex= ((List JavaDoc) parent.getStructuralProperty(descriptor)).indexOf(fields[fields.length - 1]) + 1;
794         
795         final FieldDeclaration declaration= createNewFieldDeclaration(rewrite);
796         rewrite.getListRewrite(parent, descriptor).insertAt(declaration, insertIndex, null);
797     }
798     
799     private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) {
800         AST ast= getAST();
801         VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment();
802         SimpleName variableName= ast.newSimpleName(fFieldName);
803         fragment.setName(variableName);
804         addLinkedName(rewrite, variableName, false);
805         fragment.setExtraDimensions(fTempDeclarationNode.getExtraDimensions());
806         if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()){
807             Expression initializer= (Expression)rewrite.createCopyTarget(getTempInitializer());
808             fragment.setInitializer(initializer);
809         }
810         FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(fragment);
811         
812         VariableDeclarationStatement vds= getTempDeclarationStatement();
813         Type type= (Type)rewrite.createCopyTarget(vds.getType());
814         fieldDeclaration.setType(type);
815         fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers()));
816         return fieldDeclaration;
817     }
818     
819     private int getModifiers() {
820         int flags= fVisibility;
821         if (fDeclareFinal)
822             flags |= Modifier.FINAL;
823         if (fDeclareStatic)
824             flags |= Modifier.STATIC;
825         return flags;
826     }
827     
828     private AST getAST(){
829         return fTempDeclarationNode.getAST();
830     }
831
832     private static class LocalTypeAndVariableUsageAnalyzer extends HierarchicalASTVisitor{
833         private final List JavaDoc fLocalDefinitions= new ArrayList JavaDoc(0); // List of IBinding (Variable and Type)
834
private final List JavaDoc fLocalReferencesToEnclosing= new ArrayList JavaDoc(0); // List of ASTNodes
835
private final List JavaDoc fMethodTypeVariables;
836         private boolean fClassTypeVariablesUsed= false;
837         public LocalTypeAndVariableUsageAnalyzer(ITypeBinding[] methodTypeVariables) {
838             fMethodTypeVariables= Arrays.asList(methodTypeVariables);
839         }
840         public List JavaDoc getUsageOfEnclosingNodes(){
841             return fLocalReferencesToEnclosing;
842         }
843         public boolean getClassTypeVariablesUsed() {
844             return fClassTypeVariablesUsed;
845         }
846         public boolean visit(SimpleName node) {
847             ITypeBinding typeBinding= node.resolveTypeBinding();
848             if (typeBinding != null && typeBinding.isLocal()) {
849                 if (node.isDeclaration()) {
850                     fLocalDefinitions.add(typeBinding);
851                 } else if (! fLocalDefinitions.contains(typeBinding)) {
852                     fLocalReferencesToEnclosing.add(node);
853                 }
854             }
855             if (typeBinding != null && typeBinding.isTypeVariable()) {
856                 if (node.isDeclaration()) {
857                     fLocalDefinitions.add(typeBinding);
858                 } else if (! fLocalDefinitions.contains(typeBinding)) {
859                     if (fMethodTypeVariables.contains(typeBinding)) {
860                         fLocalReferencesToEnclosing.add(node);
861                     } else {
862                         fClassTypeVariablesUsed= true;
863                     }
864                 }
865             }
866             IBinding binding= node.resolveBinding();
867             if (binding != null && binding.getKind() == IBinding.VARIABLE && ! ((IVariableBinding)binding).isField()) {
868                 if (node.isDeclaration()) {
869                     fLocalDefinitions.add(binding);
870                 } else if (! fLocalDefinitions.contains(binding)) {
871                     fLocalReferencesToEnclosing.add(node);
872                 }
873             }
874             return super.visit(node);
875         }
876     }
877
878     public RefactoringStatus initialize(final RefactoringArguments arguments) {
879         fSelfInitializing= true;
880         if (arguments instanceof JavaRefactoringArguments) {
881             final JavaRefactoringArguments extended= (JavaRefactoringArguments) arguments;
882             final String JavaDoc selection= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_SELECTION);
883             if (selection != null) {
884                 int offset= -1;
885                 int length= -1;
886                 final StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(selection);
887                 if (tokenizer.hasMoreTokens())
888                     offset= Integer.valueOf(tokenizer.nextToken()).intValue();
889                 if (tokenizer.hasMoreTokens())
890                     length= Integer.valueOf(tokenizer.nextToken()).intValue();
891                 if (offset >= 0 && length >= 0) {
892                     fSelectionStart= offset;
893                     fSelectionLength= length;
894                 } else
895                     return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object JavaDoc[] { selection, JDTRefactoringDescriptor.ATTRIBUTE_SELECTION}));
896             } else
897                 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_SELECTION));
898             final String JavaDoc handle= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_INPUT);
899             if (handle != null) {
900                 final IJavaElement element= JDTRefactoringDescriptor.handleToElement(extended.getProject(), handle, false);
901                 if (element == null || !element.exists() || element.getElementType() != IJavaElement.COMPILATION_UNIT)
902                     return createInputFatalStatus(element, IJavaRefactorings.CONVERT_LOCAL_VARIABLE);
903                 else
904                     fCu= (ICompilationUnit) element;
905             } else
906                 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_INPUT));
907             final String JavaDoc visibility= extended.getAttribute(ATTRIBUTE_VISIBILITY);
908             if (visibility != null && !"".equals(visibility)) {//$NON-NLS-1$
909
int flag= 0;
910                 try {
911                     flag= Integer.parseInt(visibility);
912                 } catch (NumberFormatException JavaDoc exception) {
913                     return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_VISIBILITY));
914                 }
915                 fVisibility= flag;
916             }
917             final String JavaDoc initialize= extended.getAttribute(ATTRIBUTE_INITIALIZE);
918             if (initialize != null && !"".equals(initialize)) {//$NON-NLS-1$
919
int value= 0;
920                 try {
921                     value= Integer.parseInt(initialize);
922                 } catch (NumberFormatException JavaDoc exception) {
923                     return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_INITIALIZE));
924                 }
925                 fInitializeIn= value;
926             }
927             final String JavaDoc name= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_NAME);
928             if (name != null && !"".equals(name)) //$NON-NLS-1$
929
fFieldName= name;
930             else
931                 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_NAME));
932             final String JavaDoc declareStatic= extended.getAttribute(ATTRIBUTE_STATIC);
933             if (declareStatic != null) {
934                 fDeclareStatic= Boolean.valueOf(declareStatic).booleanValue();
935             } else
936                 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_STATIC));
937             final String JavaDoc declareFinal= extended.getAttribute(ATTRIBUTE_FINAL);
938             if (declareFinal != null) {
939                 fDeclareFinal= Boolean.valueOf(declareFinal).booleanValue();
940             } else
941                 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_FINAL));
942         } else
943             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
944         return new RefactoringStatus();
945     }
946
947
948     public void setLinkedProposalModel(LinkedProposalModel model) {
949         fLinkedProposalModel= model;
950     }
951 }
952
Popular Tags