1 11 package org.eclipse.jdt.internal.corext.refactoring.rename; 12 13 import org.eclipse.core.runtime.CoreException; 14 15 import org.eclipse.jdt.core.ICompilationUnit; 16 import org.eclipse.jdt.core.IType; 17 import org.eclipse.jdt.core.compiler.IScanner; 18 import org.eclipse.jdt.core.compiler.ITerminalSymbols; 19 import org.eclipse.jdt.core.compiler.InvalidInputException; 20 import org.eclipse.jdt.core.search.SearchMatch; 21 22 import org.eclipse.jdt.internal.corext.refactoring.CuCollectingSearchRequestor; 23 24 public class TypeOccurrenceCollector extends CuCollectingSearchRequestor { 25 26 final String fOldName; 27 final String fOldQualifiedName; 28 29 public TypeOccurrenceCollector(IType type) { 30 fOldName= type.getElementName(); 31 fOldQualifiedName= type.getFullyQualifiedName('.'); 32 } 33 34 public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException { 35 collectMatch(acceptSearchMatch2(unit, match)); 36 } 37 38 public SearchMatch acceptSearchMatch2(ICompilationUnit unit, SearchMatch match) throws CoreException { 39 int start= match.getOffset(); 40 int length= match.getLength(); 41 42 String matchText= unit.getBuffer().getText(start, length); 44 if (fOldName.equals(matchText)) { 45 return match; 46 } 47 48 if (fOldQualifiedName.endsWith(matchText)) { 50 int simpleNameLenght= fOldName.length(); 52 match.setOffset(start + length - simpleNameLenght); 53 match.setLength(simpleNameLenght); 54 return match; 55 } 56 57 IScanner scanner= getScanner(unit); 59 scanner.setSource(matchText.toCharArray()); 60 int simpleNameStart= -1; 61 int simpleNameEnd= -1; 62 try { 63 int token = scanner.getNextToken(); 64 while (token != ITerminalSymbols.TokenNameEOF) { 65 if (token == ITerminalSymbols.TokenNameIdentifier) { 66 simpleNameStart= scanner.getCurrentTokenStartPosition(); 67 simpleNameEnd= scanner.getCurrentTokenEndPosition(); 68 } 69 token = scanner.getNextToken(); 70 } 71 } catch (InvalidInputException e){ 72 } 74 if (simpleNameStart != -1) { 75 match.setOffset(start + simpleNameStart); 76 match.setLength(simpleNameEnd + 1 - simpleNameStart); 77 } 78 return match; 79 } 80 } | Popular Tags |