KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > rename > RenameCompilationUnitProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.rename;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21
22 import org.eclipse.ltk.core.refactoring.Change;
23 import org.eclipse.ltk.core.refactoring.IResourceMapper;
24 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
25 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
27 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
28 import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
29
30 import org.eclipse.jdt.core.ICompilationUnit;
31 import org.eclipse.jdt.core.IJavaElement;
32 import org.eclipse.jdt.core.IPackageFragment;
33 import org.eclipse.jdt.core.IType;
34 import org.eclipse.jdt.core.JavaConventions;
35 import org.eclipse.jdt.core.refactoring.IJavaElementMapper;
36 import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
37 import org.eclipse.jdt.core.refactoring.RenameTypeArguments;
38 import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor;
39 import org.eclipse.jdt.core.refactoring.descriptors.RenameResourceDescriptor;
40
41 import org.eclipse.jdt.internal.corext.refactoring.Checks;
42 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptor;
43 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
44 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
45 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
46 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
47 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange;
48 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
49 import org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange;
50 import org.eclipse.jdt.internal.corext.refactoring.changes.RenameResourceChange;
51 import org.eclipse.jdt.internal.corext.refactoring.code.ScriptableRefactoring;
52 import org.eclipse.jdt.internal.corext.refactoring.participants.JavaProcessors;
53 import org.eclipse.jdt.internal.corext.refactoring.tagging.IQualifiedNameUpdating;
54 import org.eclipse.jdt.internal.corext.refactoring.tagging.IReferenceUpdating;
55 import org.eclipse.jdt.internal.corext.refactoring.tagging.ISimilarDeclarationUpdating;
56 import org.eclipse.jdt.internal.corext.refactoring.tagging.ITextUpdating;
57 import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil;
58 import org.eclipse.jdt.internal.corext.util.Messages;
59
60 import org.eclipse.jdt.internal.ui.JavaPlugin;
61 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
62
63 public final class RenameCompilationUnitProcessor extends JavaRenameProcessor implements IReferenceUpdating, ITextUpdating, IQualifiedNameUpdating, ISimilarDeclarationUpdating, IResourceMapper, IJavaElementMapper {
64     
65     private RenameTypeProcessor fRenameTypeProcessor= null;
66     private boolean fWillRenameType= false;
67     private ICompilationUnit fCu;
68
69     public static final String JavaDoc IDENTIFIER= "org.eclipse.jdt.ui.renameCompilationUnitProcessor"; //$NON-NLS-1$
70

71     /**
72      * Creates a new rename compilation unit processor.
73      * @param unit the compilation unit, or <code>null</code> if invoked by scripting
74      * @throws CoreException
75      */

76     public RenameCompilationUnitProcessor(ICompilationUnit unit) throws CoreException {
77         fCu= unit;
78         if (fCu != null) {
79             computeRenameTypeRefactoring();
80             setNewElementName(fCu.getElementName());
81         }
82     }
83
84     public String JavaDoc getIdentifier() {
85         return IDENTIFIER;
86     }
87
88     public boolean isApplicable() {
89         return RefactoringAvailabilityTester.isRenameAvailable(fCu);
90     }
91     
92     public String JavaDoc getProcessorName() {
93         return RefactoringCoreMessages.RenameCompilationUnitRefactoring_name;
94     }
95
96     protected String JavaDoc[] getAffectedProjectNatures() throws CoreException {
97         return JavaProcessors.computeAffectedNatures(fCu);
98     }
99
100     public Object JavaDoc[] getElements() {
101         return new Object JavaDoc[] {fCu};
102     }
103
104     protected RenameModifications computeRenameModifications() {
105         RenameModifications result= new RenameModifications();
106         result.rename(fCu, new RenameArguments(getNewElementName(), getUpdateReferences()));
107         if (fRenameTypeProcessor != null) {
108             String JavaDoc newTypeName= removeFileNameExtension(getNewElementName());
109             RenameTypeArguments arguments= new RenameTypeArguments(newTypeName, getUpdateReferences(), getUpdateSimilarDeclarations(), getSimilarElements());
110             result.rename(fRenameTypeProcessor.getType(), arguments, getUpdateSimilarDeclarations()
111                 ? new RenameTypeProcessor.ParticipantDescriptorFilter()
112                 : null);
113         }
114         return result;
115     }
116     
117     protected IFile[] getChangedFiles() throws CoreException {
118         if (!fWillRenameType) {
119             IFile file= ResourceUtil.getFile(fCu);
120             if (file != null)
121                 return new IFile[] {file};
122         }
123         return new IFile[0];
124     }
125     
126     public int getSaveMode() {
127         return RefactoringSaveHelper.SAVE_NON_JAVA_UPDATES;
128     }
129     
130     //---- IRenameProcessor -------------------------------------
131

132     public String JavaDoc getCurrentElementName() {
133         return getSimpleCUName();
134     }
135     
136     public String JavaDoc getCurrentElementQualifier() {
137         IPackageFragment pack= (IPackageFragment) fCu.getParent();
138         return pack.getElementName();
139     }
140     
141     public RefactoringStatus checkNewElementName(String JavaDoc newName) throws CoreException {
142         Assert.isNotNull(newName, "new name"); //$NON-NLS-1$
143
String JavaDoc typeName= removeFileNameExtension(newName);
144         RefactoringStatus result= Checks.checkCompilationUnitName(newName);
145         if (fWillRenameType)
146             result.merge(fRenameTypeProcessor.checkNewElementName(typeName));
147         if (Checks.isAlreadyNamed(fCu, newName))
148             result.addFatalError(RefactoringCoreMessages.RenameCompilationUnitRefactoring_same_name);
149         return result;
150     }
151     
152     public void setNewElementName(String JavaDoc newName) {
153         super.setNewElementName(newName);
154         if (fWillRenameType)
155             fRenameTypeProcessor.setNewElementName(removeFileNameExtension(newName));
156     }
157     
158     public Object JavaDoc getNewElement() {
159         IJavaElement parent= fCu.getParent();
160         if (parent.getElementType() != IJavaElement.PACKAGE_FRAGMENT)
161             return fCu; //??
162
IPackageFragment pack= (IPackageFragment)parent;
163         if (JavaConventions.validateCompilationUnitName(getNewElementName()).getSeverity() == IStatus.ERROR)
164             return fCu; //??
165
return pack.getCompilationUnit(getNewElementName());
166     }
167     
168     //---- ITextUpdating ---------------------------------------------
169

170     public boolean canEnableTextUpdating() {
171         if (fRenameTypeProcessor == null)
172             return false;
173         return fRenameTypeProcessor.canEnableUpdateReferences();
174     }
175
176     public boolean getUpdateTextualMatches() {
177         if (fRenameTypeProcessor == null)
178             return false;
179         return fRenameTypeProcessor.getUpdateTextualMatches();
180     }
181
182     public void setUpdateTextualMatches(boolean update) {
183         if (fRenameTypeProcessor != null)
184             fRenameTypeProcessor.setUpdateTextualMatches(update);
185     }
186     
187     //---- IReferenceUpdating -----------------------------------
188

189     public boolean canEnableUpdateReferences() {
190         if (fRenameTypeProcessor == null)
191             return false;
192         return fRenameTypeProcessor.canEnableUpdateReferences();
193     }
194
195     public void setUpdateReferences(boolean update) {
196         if (fRenameTypeProcessor != null)
197             fRenameTypeProcessor.setUpdateReferences(update);
198     }
199
200     public boolean getUpdateReferences(){
201         if (fRenameTypeProcessor == null)
202             return false;
203         return fRenameTypeProcessor.getUpdateReferences();
204     }
205     
206     //---- IQualifiedNameUpdating -------------------------------
207

208     public boolean canEnableQualifiedNameUpdating() {
209         if (fRenameTypeProcessor == null)
210             return false;
211         return fRenameTypeProcessor.canEnableQualifiedNameUpdating();
212     }
213     
214     public boolean getUpdateQualifiedNames() {
215         if (fRenameTypeProcessor == null)
216             return false;
217         return fRenameTypeProcessor.getUpdateQualifiedNames();
218     }
219     
220     public void setUpdateQualifiedNames(boolean update) {
221         if (fRenameTypeProcessor == null)
222             return;
223         fRenameTypeProcessor.setUpdateQualifiedNames(update);
224     }
225     
226     public String JavaDoc getFilePatterns() {
227         if (fRenameTypeProcessor == null)
228             return null;
229         return fRenameTypeProcessor.getFilePatterns();
230     }
231     
232     public void setFilePatterns(String JavaDoc patterns) {
233         if (fRenameTypeProcessor == null)
234             return;
235         fRenameTypeProcessor.setFilePatterns(patterns);
236     }
237     
238     // ---- ISimilarDeclarationUpdating ------------------------------
239

240     public boolean canEnableSimilarDeclarationUpdating() {
241         if (fRenameTypeProcessor == null)
242             return false;
243         else
244             return fRenameTypeProcessor.canEnableSimilarDeclarationUpdating();
245     }
246
247     public void setUpdateSimilarDeclarations(boolean update) {
248         if (fRenameTypeProcessor == null)
249             return;
250         fRenameTypeProcessor.setUpdateSimilarDeclarations(update);
251     }
252
253     public boolean getUpdateSimilarDeclarations() {
254         if (fRenameTypeProcessor == null)
255             return false;
256         return fRenameTypeProcessor.getUpdateSimilarDeclarations();
257     }
258
259     public int getMatchStrategy() {
260         if (fRenameTypeProcessor == null)
261             return RenamingNameSuggestor.STRATEGY_EXACT; // method should not be called in this case anyway ...
262
return fRenameTypeProcessor.getMatchStrategy();
263     }
264
265     public void setMatchStrategy(int selectedStrategy) {
266         if (fRenameTypeProcessor == null)
267             return;
268         fRenameTypeProcessor.setMatchStrategy(selectedStrategy);
269     }
270
271     public IJavaElement[] getSimilarElements() {
272         if (fRenameTypeProcessor == null)
273             return null;
274         return fRenameTypeProcessor.getSimilarElements();
275     }
276
277     public IResource getRefactoredResource(IResource element) {
278         if (fRenameTypeProcessor == null)
279             return element;
280         return fRenameTypeProcessor.getRefactoredResource(element);
281     }
282     
283     public IJavaElement getRefactoredJavaElement(IJavaElement element) {
284         if (fRenameTypeProcessor == null)
285             return element;
286         return fRenameTypeProcessor.getRefactoredJavaElement(element);
287     }
288     
289     // --- preconditions ----------------------------------
290

291     public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
292         if (fRenameTypeProcessor != null && ! fCu.isStructureKnown()){
293             fRenameTypeProcessor= null;
294             fWillRenameType= false;
295             return new RefactoringStatus();
296         }
297         
298         //for a test case what it's needed, see bug 24248
299
//(the type might be gone from the editor by now)
300
if (fWillRenameType && fRenameTypeProcessor != null && ! fRenameTypeProcessor.getType().exists()){
301             fRenameTypeProcessor= null;
302             fWillRenameType= false;
303             return new RefactoringStatus();
304         }
305          
306         // we purposely do not check activation of the renameTypeRefactoring here.
307
return new RefactoringStatus();
308     }
309     
310     protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
311         try{
312             if (fWillRenameType && (!fCu.isStructureKnown())){
313                 RefactoringStatus result1= new RefactoringStatus();
314                 
315                 RefactoringStatus result2= new RefactoringStatus();
316                 result2.merge(Checks.checkCompilationUnitNewName(fCu, getNewElementName()));
317                 if (result2.hasFatalError())
318                     result1.addError(Messages.format(RefactoringCoreMessages.RenameCompilationUnitRefactoring_not_parsed_1, fCu.getElementName()));
319                 else
320                     result1.addError(Messages.format(RefactoringCoreMessages.RenameCompilationUnitRefactoring_not_parsed, fCu.getElementName()));
321                 result1.merge(result2);
322             }
323         
324             if (fWillRenameType) {
325                 return fRenameTypeProcessor.checkFinalConditions(pm, context);
326             } else {
327                 return Checks.checkCompilationUnitNewName(fCu, getNewElementName());
328             }
329         } finally{
330             pm.done();
331         }
332     }
333     
334     private void computeRenameTypeRefactoring() throws CoreException{
335         if (getSimpleCUName().indexOf(".") != -1) { //$NON-NLS-1$
336
fRenameTypeProcessor= null;
337             fWillRenameType= false;
338             return;
339         }
340         IType type= getTypeWithTheSameName();
341         if (type != null) {
342             fRenameTypeProcessor= new RenameTypeProcessor(type);
343         } else {
344             fRenameTypeProcessor= null;
345         }
346         fWillRenameType= fRenameTypeProcessor != null && fCu.isStructureKnown();
347     }
348
349     private IType getTypeWithTheSameName() {
350         try {
351             IType[] topLevelTypes= fCu.getTypes();
352             String JavaDoc name= getSimpleCUName();
353             for (int i = 0; i < topLevelTypes.length; i++) {
354                 if (name.equals(topLevelTypes[i].getElementName()))
355                     return topLevelTypes[i];
356             }
357             return null;
358         } catch (CoreException e) {
359             return null;
360         }
361     }
362     
363     private String JavaDoc getSimpleCUName() {
364         return removeFileNameExtension(fCu.getElementName());
365     }
366     
367     /**
368      * Removes the extension (whatever comes after the last '.') from the given file name.
369      */

370     private static String JavaDoc removeFileNameExtension(String JavaDoc fileName) {
371         if (fileName.lastIndexOf(".") == -1) //$NON-NLS-1$
372
return fileName;
373         return fileName.substring(0, fileName.lastIndexOf(".")); //$NON-NLS-1$
374
}
375
376     public Change createChange(IProgressMonitor pm) throws CoreException {
377         // renaming the file is taken care of in renameTypeRefactoring
378
if (fWillRenameType)
379             return fRenameTypeProcessor.createChange(pm);
380         fRenameTypeProcessor= null;
381         final String JavaDoc newName= getNewElementName();
382         final IResource resource= fCu.getResource();
383         if (resource != null && resource.isLinked()) {
384             final IProject project= resource.getProject();
385             final String JavaDoc name= project.getName();
386             final String JavaDoc description= Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description_short, resource.getName());
387             final String JavaDoc header= Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description, new String JavaDoc[] { resource.getFullPath().toString(), newName});
388             final String JavaDoc comment= new JDTRefactoringDescriptorComment(name, this, header).asString();
389             final int flags= RefactoringDescriptor.STRUCTURAL_CHANGE;
390             final RenameResourceDescriptor descriptor= new RenameResourceDescriptor();
391             descriptor.setProject(name);
392             descriptor.setDescription(description);
393             descriptor.setComment(comment);
394             descriptor.setFlags(flags);
395             descriptor.setResource(resource);
396             descriptor.setNewName(newName);
397             return new DynamicValidationStateChange(new RenameResourceChange(descriptor, resource, newName, comment));
398         }
399         String JavaDoc label= null;
400         final IPackageFragment fragment= (IPackageFragment) fCu.getParent();
401         if (!fragment.isDefaultPackage())
402             label= fragment.getElementName() + "." + fCu.getElementName(); //$NON-NLS-1$
403
else
404             label= fCu.getElementName();
405         final String JavaDoc name= fCu.getJavaProject().getElementName();
406         final String JavaDoc description= Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description_short, fCu.getElementName());
407         final String JavaDoc header= Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description, new String JavaDoc[] { label, newName});
408         final String JavaDoc comment= new JDTRefactoringDescriptorComment(name, this, header).asString();
409         final int flags= RefactoringDescriptor.STRUCTURAL_CHANGE;
410         final RenameJavaElementDescriptor descriptor= new RenameJavaElementDescriptor(IJavaRefactorings.RENAME_COMPILATION_UNIT);
411         descriptor.setProject(name);
412         descriptor.setDescription(description);
413         descriptor.setComment(comment);
414         descriptor.setFlags(flags);
415         descriptor.setJavaElement(fCu);
416         descriptor.setNewName(newName);
417         return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameCompilationUnitRefactoring_name, new Change[] { new RenameCompilationUnitChange(fCu, newName)});
418     }
419     
420     /**
421      * {@inheritDoc}
422      */

423     public Change postCreateChange(Change[] participantChanges, IProgressMonitor pm) throws CoreException {
424         if (fWillRenameType)
425             return fRenameTypeProcessor.postCreateChange(participantChanges, pm);
426         return super.postCreateChange(participantChanges, pm);
427     }
428
429     public RefactoringStatus initialize(RefactoringArguments arguments) {
430         if (!(arguments instanceof JavaRefactoringArguments)) {
431             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
432         }
433         
434         final JavaRefactoringArguments extended= (JavaRefactoringArguments) arguments;
435         final String JavaDoc handle= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_INPUT);
436         if (handle == null) {
437             return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_INPUT));
438         }
439             
440         final IJavaElement element= JDTRefactoringDescriptor.handleToElement(extended.getProject(), handle, false);
441         if (element == null || !element.exists() || element.getElementType() != IJavaElement.COMPILATION_UNIT)
442             return ScriptableRefactoring.createInputFatalStatus(element, getRefactoring().getName(), IJavaRefactorings.RENAME_COMPILATION_UNIT);
443         
444         final String JavaDoc name= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_NAME);
445         if (name == null || name.length() == 0)
446             return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_NAME));
447
448         fCu= (ICompilationUnit) element;
449         try {
450             computeRenameTypeRefactoring();
451             setNewElementName(name);
452         } catch (CoreException exception) {
453             JavaPlugin.log(exception);
454             return ScriptableRefactoring.createInputFatalStatus(element, getRefactoring().getName(), IJavaRefactorings.RENAME_COMPILATION_UNIT);
455         }
456         return new RefactoringStatus();
457     }
458
459     /**
460      * @return the RenameTypeProcessor or <code>null</code> if no type will be renamed
461      */

462     public RenameTypeProcessor getRenameTypeProcessor() {
463         return fRenameTypeProcessor;
464     }
465
466     public boolean isWillRenameType() {
467         return fWillRenameType;
468     }
469 }
470
Popular Tags