KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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.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 JavaDoc fOldName;
27     final String JavaDoc 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         //unqualified:
43
String JavaDoc matchText= unit.getBuffer().getText(start, length);
44         if (fOldName.equals(matchText)) {
45             return match;
46         }
47         
48         //(partially) qualified:
49
if (fOldQualifiedName.endsWith(matchText)) {
50             //e.g. rename B and p.A.B ends with match A.B
51
int simpleNameLenght= fOldName.length();
52             match.setOffset(start + length - simpleNameLenght);
53             match.setLength(simpleNameLenght);
54             return match;
55         }
56         
57         //Not a standard reference -- use scanner to find last identifier token:
58
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             //ignore
73
}
74         if (simpleNameStart != -1) {
75             match.setOffset(start + simpleNameStart);
76             match.setLength(simpleNameEnd + 1 - simpleNameStart);
77         }
78         return match;
79     }
80 }
Popular Tags