1 11 package org.eclipse.jdt.internal.corext.refactoring.util; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 import java.util.StringTokenizer ; 16 import java.util.regex.Pattern ; 17 18 import org.eclipse.text.edits.ReplaceEdit; 19 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.NullProgressMonitor; 25 26 import org.eclipse.core.filebuffers.FileBuffers; 27 28 import org.eclipse.core.resources.IFile; 29 import org.eclipse.core.resources.IProject; 30 import org.eclipse.core.resources.IResource; 31 32 import org.eclipse.search.core.text.TextSearchEngine; 33 import org.eclipse.search.core.text.TextSearchMatchAccess; 34 import org.eclipse.search.core.text.TextSearchRequestor; 35 import org.eclipse.search.core.text.TextSearchScope; 36 37 import org.eclipse.ltk.core.refactoring.GroupCategory; 38 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 39 import org.eclipse.ltk.core.refactoring.TextChange; 40 41 import org.eclipse.jdt.core.IJavaElement; 42 import org.eclipse.jdt.core.JavaCore; 43 44 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 45 import org.eclipse.jdt.internal.corext.refactoring.changes.TextChangeCompatibility; 46 47 import org.eclipse.jdt.internal.ui.util.PatternConstructor; 48 49 public class QualifiedNameFinder { 50 51 private static final GroupCategorySet QUALIFIED_NAMES= new GroupCategorySet( 52 new GroupCategory("org.eclipse.jdt.internal.corext.qualifiedNames", RefactoringCoreMessages.QualifiedNameFinder_qualifiedNames_name, 54 RefactoringCoreMessages.QualifiedNameFinder_qualifiedNames_description)); 55 56 private static class ResultCollector extends TextSearchRequestor { 57 58 private String fNewValue; 59 private QualifiedNameSearchResult fResult; 60 61 public ResultCollector(QualifiedNameSearchResult result, String newValue) { 62 fResult= result; 63 fNewValue= newValue; 64 } 65 66 public boolean acceptFile(IFile file) throws CoreException { 67 IJavaElement element= JavaCore.create(file); 68 if ((element != null && element.exists())) 69 return false; 70 71 if (! FileBuffers.getTextFileBufferManager().isTextFileLocation(file.getFullPath(), false)) 73 return false; 74 75 IPath path= file.getProjectRelativePath(); 76 String segment= path.segment(0); 77 if (segment != null && (segment.startsWith(".refactorings") || segment.startsWith(".deprecations"))) return false; 79 80 return true; 81 } 82 83 public boolean acceptPatternMatch(TextSearchMatchAccess matchAccess) throws CoreException { 84 int start= matchAccess.getMatchOffset(); 85 int length= matchAccess.getMatchLength(); 86 87 if (start > 0) { 89 char before= matchAccess.getFileContentChar(start - 1); 90 if (before == '.' || Character.isJavaIdentifierPart(before)) 91 return true; 92 } 93 int fileContentLength= matchAccess.getFileContentLength(); 94 int end= start + length; 95 if (end < fileContentLength) { 96 char after= matchAccess.getFileContentChar(end); 97 if (Character.isJavaIdentifierPart(after)) 98 return true; 99 } 100 101 IFile file= matchAccess.getFile(); 102 TextChange change= fResult.getChange(file); 103 TextChangeCompatibility.addTextEdit( 104 change, 105 RefactoringCoreMessages.QualifiedNameFinder_update_name, 106 new ReplaceEdit(start, length, fNewValue), QUALIFIED_NAMES); 107 108 return true; 109 } 110 } 111 112 public QualifiedNameFinder() { 113 } 114 115 public static void process(QualifiedNameSearchResult result, String pattern, String newValue, String filePatterns, IProject root, IProgressMonitor monitor) { 116 Assert.isNotNull(pattern); 117 Assert.isNotNull(newValue); 118 Assert.isNotNull(root); 119 120 if (monitor == null) 121 monitor= new NullProgressMonitor(); 122 123 if (filePatterns == null || filePatterns.length() == 0) { 124 monitor.beginTask("", 1); monitor.worked(1); 127 return; 128 } 129 130 ResultCollector collector= new ResultCollector(result, newValue); 131 TextSearchEngine engine= TextSearchEngine.create(); 132 Pattern searchPattern= PatternConstructor.createPattern(pattern, true, false); 133 134 engine.search(createScope(filePatterns, root), collector, searchPattern, monitor); 135 } 136 137 private static TextSearchScope createScope(String filePatterns, IProject root) { 138 HashSet res= new HashSet (); 139 res.add(root); 140 addReferencingProjects(root, res); 141 IResource[] resArr= (IResource[]) res.toArray(new IResource[res.size()]); 142 Pattern filePattern= getFilePattern(filePatterns); 143 144 return TextSearchScope.newSearchScope(resArr, filePattern, false); 145 } 146 147 private static Pattern getFilePattern(String filePatterns) { 148 StringTokenizer tokenizer= new StringTokenizer (filePatterns, ","); String [] filePatternArray= new String [tokenizer.countTokens()]; 150 int i= 0; 151 while (tokenizer.hasMoreTokens()) { 152 filePatternArray[i++]= tokenizer.nextToken().trim(); 153 } 154 return PatternConstructor.createPattern(filePatternArray, true, false); 155 } 156 157 private static void addReferencingProjects(IProject root, Set res) { 158 IProject[] projects= root.getReferencingProjects(); 159 for (int i= 0; i < projects.length; i++) { 160 IProject project= projects[i]; 161 if (res.add(project)) { 162 addReferencingProjects(project, res); 163 } 164 } 165 } 166 } 167 | Popular Tags |