1 11 package org.eclipse.jdt.internal.corext.refactoring.rename; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Set ; 18 19 import org.eclipse.text.edits.MalformedTreeException; 20 import org.eclipse.text.edits.ReplaceEdit; 21 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.OperationCanceledException; 27 import org.eclipse.core.runtime.SubProgressMonitor; 28 29 import org.eclipse.core.resources.IContainer; 30 import org.eclipse.core.resources.IFile; 31 import org.eclipse.core.resources.IProject; 32 import org.eclipse.core.resources.IResource; 33 import org.eclipse.core.resources.ResourcesPlugin; 34 35 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 36 import org.eclipse.ltk.core.refactoring.GroupCategory; 37 38 import org.eclipse.jdt.core.ICompilationUnit; 39 import org.eclipse.jdt.core.IJavaElement; 40 import org.eclipse.jdt.core.JavaCore; 41 import org.eclipse.jdt.core.JavaModelException; 42 import org.eclipse.jdt.core.search.IJavaSearchScope; 43 import org.eclipse.jdt.core.search.SearchMatch; 44 45 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 46 import org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup; 47 import org.eclipse.jdt.internal.corext.refactoring.changes.TextChangeCompatibility; 48 import org.eclipse.jdt.internal.corext.refactoring.rename.RefactoringScanner.TextMatch; 49 import org.eclipse.jdt.internal.corext.refactoring.tagging.ITextUpdating; 50 import org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager; 51 52 class TextMatchUpdater { 53 54 private static final String TEXT_EDIT_LABEL= RefactoringCoreMessages.TextMatchUpdater_update; 55 56 private static final GroupCategorySet TEXTUAL_MATCHES= new GroupCategorySet( 57 new GroupCategory("org.eclipse.jdt.internal.corext.textualMatches", RefactoringCoreMessages.TextMatchUpdater_textualMatches_name, 59 RefactoringCoreMessages.TextMatchUpdater_textualMatches_description)); 60 61 private final IJavaSearchScope fScope; 62 private final TextChangeManager fManager; 63 private final SearchResultGroup[] fReferences; 64 private final boolean fOnlyQualified; 65 66 private final RefactoringScanner fScanner; 67 private final String fNewName; 68 private final int fCurrentNameLength; 69 70 private TextMatchUpdater(TextChangeManager manager, IJavaSearchScope scope, String currentName, String currentQualifier, String newName, SearchResultGroup[] references, boolean onlyQualified){ 71 Assert.isNotNull(manager); 72 Assert.isNotNull(scope); 73 Assert.isNotNull(references); 74 fManager= manager; 75 fScope= scope; 76 fReferences= references; 77 fOnlyQualified= onlyQualified; 78 79 fNewName= newName; 80 fCurrentNameLength= currentName.length(); 81 fScanner= new RefactoringScanner(currentName, currentQualifier); 82 } 83 84 static void perform(IProgressMonitor pm, IJavaSearchScope scope, String currentName, String currentQualifier, String newName, TextChangeManager manager, SearchResultGroup[] references, boolean onlyQualified) throws JavaModelException{ 85 new TextMatchUpdater(manager, scope, currentName, currentQualifier, newName, references, onlyQualified).updateTextMatches(pm); 86 } 87 88 static void perform(IProgressMonitor pm, IJavaSearchScope scope, ITextUpdating processor, TextChangeManager manager, SearchResultGroup[] references) throws JavaModelException{ 89 new TextMatchUpdater(manager, scope, processor.getCurrentElementName(), processor.getCurrentElementQualifier(), processor.getNewElementName(), references, false).updateTextMatches(pm); 90 } 91 92 private void updateTextMatches(IProgressMonitor pm) throws JavaModelException { 93 try{ 94 IProject[] projectsInScope= getProjectsInScope(); 95 96 pm.beginTask("", projectsInScope.length); 98 for (int i =0 ; i < projectsInScope.length; i++){ 99 if (pm.isCanceled()) 100 throw new OperationCanceledException(); 101 addTextMatches(projectsInScope[i], new SubProgressMonitor(pm, 1)); 102 } 103 } finally{ 104 pm.done(); 105 } 106 } 107 108 private IProject[] getProjectsInScope() { 109 IPath[] enclosingProjects= fScope.enclosingProjectsAndJars(); 110 Set enclosingProjectSet= new HashSet (); 111 enclosingProjectSet.addAll(Arrays.asList(enclosingProjects)); 112 113 ArrayList projectsInScope= new ArrayList (); 114 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 115 for (int i =0 ; i < projects.length; i++){ 116 if (enclosingProjectSet.contains(projects[i].getFullPath())) 117 projectsInScope.add(projects[i]); 118 } 119 120 return (IProject[]) projectsInScope.toArray(new IProject[projectsInScope.size()]); 121 } 122 123 private void addTextMatches(IResource resource, IProgressMonitor pm) throws JavaModelException{ 124 try{ 125 String task= RefactoringCoreMessages.TextMatchUpdater_searching + resource.getFullPath(); 126 if (resource instanceof IFile){ 127 IJavaElement element= JavaCore.create(resource); 128 if (!(element instanceof ICompilationUnit)) 130 return; 131 if (! element.exists()) 132 return; 133 if (! fScope.encloses(element)) 134 return; 135 addCuTextMatches((ICompilationUnit) element); 136 137 } else if (resource instanceof IContainer){ 138 IResource[] members= ((IContainer) resource).members(); 139 pm.beginTask(task, members.length); 140 pm.subTask(task); 141 for (int i = 0; i < members.length; i++) { 142 if (pm.isCanceled()) 143 throw new OperationCanceledException(); 144 145 addTextMatches(members[i], new SubProgressMonitor(pm, 1)); 146 } 147 } 148 } catch (JavaModelException e){ 149 throw e; 150 } catch (CoreException e){ 151 throw new JavaModelException(e); 152 } finally{ 153 pm.done(); 154 } 155 } 156 157 private void addCuTextMatches(ICompilationUnit cu) throws JavaModelException{ 158 fScanner.scan(cu); 159 Set matches= fScanner.getMatches(); if (matches.size() == 0) 161 return; 162 163 removeReferences(cu, matches); 164 if (matches.size() != 0) 165 addTextUpdates(cu, matches); 166 } 167 168 private void removeReferences(ICompilationUnit cu, Set matches) { 169 for (int i= 0; i < fReferences.length; i++) { 170 SearchResultGroup group= fReferences[i]; 171 if (cu.equals(group.getCompilationUnit())) { 172 removeReferences(matches, group); 173 } 174 } 175 } 176 177 private void removeReferences(Set matches, SearchResultGroup group) { 178 SearchMatch[] searchResults= group.getSearchResults(); 179 for (int r= 0; r < searchResults.length; r++) { 180 int unqualifiedStart= searchResults[r].getOffset() + searchResults[r].getLength() - fCurrentNameLength; 182 for (Iterator iter= matches.iterator(); iter.hasNext();) { 183 TextMatch element= (TextMatch) iter.next(); 184 if (element.getStartPosition() == unqualifiedStart) 185 iter.remove(); 186 } 187 } 188 } 189 190 private void addTextUpdates(ICompilationUnit cu, Set matches) { 191 for (Iterator resultIter= matches.iterator(); resultIter.hasNext();){ 192 TextMatch match= (TextMatch) resultIter.next(); 193 if (!match.isQualified() && fOnlyQualified) 194 continue; 195 int matchStart= match.getStartPosition(); 196 ReplaceEdit edit= new ReplaceEdit(matchStart, fCurrentNameLength, fNewName); 197 try { 198 TextChangeCompatibility.addTextEdit(fManager.get(cu), TEXT_EDIT_LABEL, edit, TEXTUAL_MATCHES); 199 } catch (MalformedTreeException e) { 200 } 202 } 203 } 204 } 205 | Popular Tags |