1 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 IDENTIFIER= "org.eclipse.jdt.ui.renameCompilationUnitProcessor"; 71 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 getIdentifier() { 85 return IDENTIFIER; 86 } 87 88 public boolean isApplicable() { 89 return RefactoringAvailabilityTester.isRenameAvailable(fCu); 90 } 91 92 public String getProcessorName() { 93 return RefactoringCoreMessages.RenameCompilationUnitRefactoring_name; 94 } 95 96 protected String [] getAffectedProjectNatures() throws CoreException { 97 return JavaProcessors.computeAffectedNatures(fCu); 98 } 99 100 public Object [] getElements() { 101 return new Object [] {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 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 132 public String getCurrentElementName() { 133 return getSimpleCUName(); 134 } 135 136 public String getCurrentElementQualifier() { 137 IPackageFragment pack= (IPackageFragment) fCu.getParent(); 138 return pack.getElementName(); 139 } 140 141 public RefactoringStatus checkNewElementName(String newName) throws CoreException { 142 Assert.isNotNull(newName, "new name"); String 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 newName) { 153 super.setNewElementName(newName); 154 if (fWillRenameType) 155 fRenameTypeProcessor.setNewElementName(removeFileNameExtension(newName)); 156 } 157 158 public Object getNewElement() { 159 IJavaElement parent= fCu.getParent(); 160 if (parent.getElementType() != IJavaElement.PACKAGE_FRAGMENT) 161 return fCu; IPackageFragment pack= (IPackageFragment)parent; 163 if (JavaConventions.validateCompilationUnitName(getNewElementName()).getSeverity() == IStatus.ERROR) 164 return fCu; return pack.getCompilationUnit(getNewElementName()); 166 } 167 168 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 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 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 getFilePatterns() { 227 if (fRenameTypeProcessor == null) 228 return null; 229 return fRenameTypeProcessor.getFilePatterns(); 230 } 231 232 public void setFilePatterns(String patterns) { 233 if (fRenameTypeProcessor == null) 234 return; 235 fRenameTypeProcessor.setFilePatterns(patterns); 236 } 237 238 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; 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 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 if (fWillRenameType && fRenameTypeProcessor != null && ! fRenameTypeProcessor.getType().exists()){ 301 fRenameTypeProcessor= null; 302 fWillRenameType= false; 303 return new RefactoringStatus(); 304 } 305 306 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) { 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 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 getSimpleCUName() { 364 return removeFileNameExtension(fCu.getElementName()); 365 } 366 367 370 private static String removeFileNameExtension(String fileName) { 371 if (fileName.lastIndexOf(".") == -1) return fileName; 373 return fileName.substring(0, fileName.lastIndexOf(".")); } 375 376 public Change createChange(IProgressMonitor pm) throws CoreException { 377 if (fWillRenameType) 379 return fRenameTypeProcessor.createChange(pm); 380 fRenameTypeProcessor= null; 381 final String newName= getNewElementName(); 382 final IResource resource= fCu.getResource(); 383 if (resource != null && resource.isLinked()) { 384 final IProject project= resource.getProject(); 385 final String name= project.getName(); 386 final String description= Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description_short, resource.getName()); 387 final String header= Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description, new String [] { resource.getFullPath().toString(), newName}); 388 final String 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 label= null; 400 final IPackageFragment fragment= (IPackageFragment) fCu.getParent(); 401 if (!fragment.isDefaultPackage()) 402 label= fragment.getElementName() + "." + fCu.getElementName(); else 404 label= fCu.getElementName(); 405 final String name= fCu.getJavaProject().getElementName(); 406 final String description= Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description_short, fCu.getElementName()); 407 final String header= Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description, new String [] { label, newName}); 408 final String 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 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 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 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 462 public RenameTypeProcessor getRenameTypeProcessor() { 463 return fRenameTypeProcessor; 464 } 465 466 public boolean isWillRenameType() { 467 return fWillRenameType; 468 } 469 } 470 | Popular Tags |