KickJava   Java API By Example, From Geeks To Geeks.

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


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.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     /**
56      * Creates a new rename method processor.
57      * <p>
58      * This constructor is only invoked by <code>RenameTypeProcessor</code>.
59      * </p>
60      *
61      * @param method the method
62      * @param manager the change manager
63      * @param categorySet the group category set
64      */

65     RenameNonVirtualMethodProcessor(IMethod method, TextChangeManager manager, GroupCategorySet categorySet) {
66         super(method, manager, categorySet);
67     }
68
69     /**
70      * Creates a new rename method processor.
71      * @param method the method, or <code>null</code> if invoked by scripting
72      */

73     public RenameNonVirtualMethodProcessor(IMethod method) {
74         super(method);
75     }
76     
77     public boolean isApplicable() throws CoreException {
78         return RefactoringAvailabilityTester.isRenameNonVirtualMethodAvailable(getMethod());
79     }
80     
81     //----------- preconditions --------------
82

83     protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext checkContext) throws CoreException {
84         try{
85             pm.beginTask("", 3); //$NON-NLS-1$
86
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 JavaDoc 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 JavaDoc message= Messages.format(
102                         RefactoringCoreMessages.RenamePrivateMethodRefactoring_hierarchy_defines,
103                         new String JavaDoc[]{JavaModelUtil.getFullyQualifiedName(
104                             declaring), name});
105                     result.addError(message, context);
106                 }else {
107                     String JavaDoc message= Messages.format(
108                         RefactoringCoreMessages.RenamePrivateMethodRefactoring_hierarchy_defines2,
109                         new String JavaDoc[]{JavaModelUtil.getFullyQualifiedName(
110                             declaring), name});
111                     result.addWarning(message, context);
112                 }
113             }
114             return result;
115         } finally{
116             pm.done();
117         }
118     }
119     
120     /*
121      * The code below is needed to due bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=39700.
122      * Declaration in hierarchy doesn't take visibility into account.
123      */

124
125     /*
126      * XXX working around bug 39700
127      */

128     protected SearchResultGroup[] getOccurrences(IProgressMonitor pm, RefactoringStatus status) throws CoreException {
129         pm.beginTask("", 2); //$NON-NLS-1$
130
SearchPattern pattern= createReferenceSearchPattern();
131         SearchResultGroup[] groups= RefactoringSearchEngine.search(pattern, createRefactoringScope(),
132             new MethodOccurenceCollector(getMethod().getElementName()), new SubProgressMonitor(pm, 1), status);
133         //Workaround bug 39700. Manually add declaration match:
134
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;//no need to go further
144
}
145         }
146         return groups;
147     }
148         
149     /*
150      * @see RenameMethodProcessor#addOccurrences(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager, org.eclipse.core.runtime.IProgressMonitor, RefactoringStatus)
151      */

152     void addOccurrences(TextChangeManager manager, IProgressMonitor pm, RefactoringStatus status) throws CoreException {
153         pm.beginTask("", 1); //$NON-NLS-1$
154
// declaration update must be registered first
155
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     /*
166      * @see RenameMethodProcessor#createOccurrenceSearchPattern(org.eclipse.core.runtime.IProgressMonitor)
167      */

168     SearchPattern createOccurrenceSearchPattern(IProgressMonitor pm) {
169         pm.beginTask("", 1); //$NON-NLS-1$
170
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             // create the delegate
183
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 JavaDoc 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 JavaDoc 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         //TODO: should not do the search again!
225
pm.beginTask("", 2); //$NON-NLS-1$
226
SearchPattern pattern= createReferenceSearchPattern();
227         return RefactoringSearchEngine.search(pattern, createRefactoringScope(),
228             new MethodOccurenceCollector(getMethod().getElementName()), new SubProgressMonitor(pm, 1), status);
229     }
230
231     public String JavaDoc 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