1 11 package org.eclipse.jdt.internal.corext.refactoring.rename; 12 13 import org.eclipse.text.edits.ReplaceEdit; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.SubProgressMonitor; 18 19 import org.eclipse.core.resources.IResource; 20 21 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 22 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 23 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext; 24 import org.eclipse.ltk.core.refactoring.TextChange; 25 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; 26 27 import org.eclipse.jdt.core.ICompilationUnit; 28 import org.eclipse.jdt.core.IMethod; 29 import org.eclipse.jdt.core.ISourceRange; 30 import org.eclipse.jdt.core.IType; 31 import org.eclipse.jdt.core.dom.MethodDeclaration; 32 import org.eclipse.jdt.core.search.IJavaSearchConstants; 33 import org.eclipse.jdt.core.search.MethodDeclarationMatch; 34 import org.eclipse.jdt.core.search.SearchEngine; 35 import org.eclipse.jdt.core.search.SearchMatch; 36 import org.eclipse.jdt.core.search.SearchPattern; 37 38 import org.eclipse.jdt.internal.corext.refactoring.Checks; 39 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 40 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 41 import org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine; 42 import org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup; 43 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext; 44 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; 45 import org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator; 46 import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil; 47 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; 48 import org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager; 49 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 50 import org.eclipse.jdt.internal.corext.util.Messages; 51 import org.eclipse.jdt.internal.corext.util.SearchUtils; 52 53 public class RenameNonVirtualMethodProcessor extends RenameMethodProcessor { 54 55 65 RenameNonVirtualMethodProcessor(IMethod method, TextChangeManager manager, GroupCategorySet categorySet) { 66 super(method, manager, categorySet); 67 } 68 69 73 public RenameNonVirtualMethodProcessor(IMethod method) { 74 super(method); 75 } 76 77 public boolean isApplicable() throws CoreException { 78 return RefactoringAvailabilityTester.isRenameNonVirtualMethodAvailable(getMethod()); 79 } 80 81 83 protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext checkContext) throws CoreException { 84 try{ 85 pm.beginTask("", 3); RefactoringStatus result= new RefactoringStatus(); 87 result.merge(super.doCheckFinalConditions(new SubProgressMonitor(pm, 1), checkContext)); 88 if (result.hasFatalError()) 89 return result; 90 91 final IMethod method= getMethod(); 92 final IType declaring= method.getDeclaringType(); 93 final String name= getNewElementName(); 94 IMethod[] hierarchyMethods= hierarchyDeclaresMethodName( 95 new SubProgressMonitor(pm, 1), declaring.newTypeHierarchy(new SubProgressMonitor(pm, 1)), method, name); 96 97 for (int i= 0; i < hierarchyMethods.length; i++) { 98 IMethod hierarchyMethod= hierarchyMethods[i]; 99 RefactoringStatusContext context= JavaStatusContext.create(hierarchyMethod); 100 if (Checks.compareParamTypes(method.getParameterTypes(), hierarchyMethod.getParameterTypes())) { 101 String message= Messages.format( 102 RefactoringCoreMessages.RenamePrivateMethodRefactoring_hierarchy_defines, 103 new String []{JavaModelUtil.getFullyQualifiedName( 104 declaring), name}); 105 result.addError(message, context); 106 }else { 107 String message= Messages.format( 108 RefactoringCoreMessages.RenamePrivateMethodRefactoring_hierarchy_defines2, 109 new String []{JavaModelUtil.getFullyQualifiedName( 110 declaring), name}); 111 result.addWarning(message, context); 112 } 113 } 114 return result; 115 } finally{ 116 pm.done(); 117 } 118 } 119 120 124 125 128 protected SearchResultGroup[] getOccurrences(IProgressMonitor pm, RefactoringStatus status) throws CoreException { 129 pm.beginTask("", 2); SearchPattern pattern= createReferenceSearchPattern(); 131 SearchResultGroup[] groups= RefactoringSearchEngine.search(pattern, createRefactoringScope(), 132 new MethodOccurenceCollector(getMethod().getElementName()), new SubProgressMonitor(pm, 1), status); 133 for (int i= 0; i < groups.length; i++) { 135 SearchResultGroup group= groups[i]; 136 ICompilationUnit cu= group.getCompilationUnit(); 137 if (cu.equals(getDeclaringCU())) { 138 IResource resource= group.getResource(); 139 int start= getMethod().getNameRange().getOffset(); 140 int length= getMethod().getNameRange().getLength(); 141 MethodDeclarationMatch declarationMatch= new MethodDeclarationMatch(getMethod(), SearchMatch.A_ACCURATE, start, length, SearchEngine.getDefaultSearchParticipant(), resource); 142 group.add(declarationMatch); 143 break; } 145 } 146 return groups; 147 } 148 149 152 void addOccurrences(TextChangeManager manager, IProgressMonitor pm, RefactoringStatus status) throws CoreException { 153 pm.beginTask("", 1); addDeclarationUpdate(manager); 156 if (getUpdateReferences()) 157 addReferenceUpdates(manager, pm, status); 158 pm.worked(1); 159 } 160 161 private ICompilationUnit getDeclaringCU() { 162 return getMethod().getCompilationUnit(); 163 } 164 165 168 SearchPattern createOccurrenceSearchPattern(IProgressMonitor pm) { 169 pm.beginTask("", 1); SearchPattern pattern= SearchPattern.createPattern(getMethod(), IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE); 171 pm.done(); 172 return pattern; 173 } 174 175 private SearchPattern createReferenceSearchPattern() { 176 return SearchPattern.createPattern(getMethod(), IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE); 177 } 178 179 final void addDeclarationUpdate(TextChangeManager manager) throws CoreException { 180 181 if (getDelegateUpdating()) { 182 CompilationUnitRewrite rewrite= new CompilationUnitRewrite(getDeclaringCU()); 184 rewrite.setResolveBindings(true); 185 MethodDeclaration methodDeclaration= ASTNodeSearchUtil.getMethodDeclarationNode(getMethod(), rewrite.getRoot()); 186 DelegateMethodCreator creator= new DelegateMethodCreator(); 187 creator.setDeclaration(methodDeclaration); 188 creator.setDeclareDeprecated(getDeprecateDelegates()); 189 creator.setSourceRewrite(rewrite); 190 creator.setCopy(true); 191 creator.setNewElementName(getNewElementName()); 192 creator.prepareDelegate(); 193 creator.createEdit(); 194 CompilationUnitChange cuChange= rewrite.createChange(); 195 if (cuChange != null) { 196 cuChange.setKeepPreviewEdits(true); 197 manager.manage(getDeclaringCU(), cuChange); 198 } 199 } 200 201 String editName= RefactoringCoreMessages.RenameMethodRefactoring_update_declaration; 202 ISourceRange nameRange= getMethod().getNameRange(); 203 ReplaceEdit replaceEdit= new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName()); 204 addTextEdit(manager.get(getDeclaringCU()), editName, replaceEdit); 205 } 206 207 private void addReferenceUpdates(TextChangeManager manager, IProgressMonitor pm, RefactoringStatus status) throws CoreException { 208 SearchResultGroup[] grouped= getReferences(pm, status); 209 for (int i= 0; i < grouped.length; i++) { 210 SearchResultGroup group= grouped[i]; 211 SearchMatch[] results= group.getSearchResults(); 212 ICompilationUnit cu= group.getCompilationUnit(); 213 TextChange change= manager.get(cu); 214 for (int j= 0; j < results.length; j++){ 215 String editName= RefactoringCoreMessages.RenamePrivateMethodRefactoring_update; 216 ReplaceEdit replaceEdit= createReplaceEdit(results[j], cu); 217 addTextEdit(change, editName, replaceEdit); 218 219 } 220 } 221 } 222 223 private SearchResultGroup[] getReferences(IProgressMonitor pm, RefactoringStatus status) throws CoreException { 224 pm.beginTask("", 2); SearchPattern pattern= createReferenceSearchPattern(); 227 return RefactoringSearchEngine.search(pattern, createRefactoringScope(), 228 new MethodOccurenceCollector(getMethod().getElementName()), new SubProgressMonitor(pm, 1), status); 229 } 230 231 public String getDelegateUpdatingTitle(boolean plural) { 232 if (plural) 233 return RefactoringCoreMessages.DelegateMethodCreator_keep_original_renamed_plural; 234 else 235 return RefactoringCoreMessages.DelegateMethodCreator_keep_original_renamed_singular; 236 } 237 } 238 | Popular Tags |