01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.rename;
11:
12: import org.eclipse.core.runtime.CoreException;
13:
14: import org.eclipse.jdt.core.ICompilationUnit;
15: import org.eclipse.jdt.core.IType;
16: import org.eclipse.jdt.core.compiler.IScanner;
17: import org.eclipse.jdt.core.compiler.ITerminalSymbols;
18: import org.eclipse.jdt.core.compiler.InvalidInputException;
19: import org.eclipse.jdt.core.search.SearchMatch;
20:
21: import org.eclipse.jdt.internal.corext.refactoring.CuCollectingSearchRequestor;
22:
23: public class TypeOccurrenceCollector extends
24: 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,
35: SearchMatch match) throws CoreException {
36: collectMatch(acceptSearchMatch2(unit, match));
37: }
38:
39: public SearchMatch acceptSearchMatch2(ICompilationUnit unit,
40: SearchMatch match) throws CoreException {
41: int start = match.getOffset();
42: int length = match.getLength();
43:
44: //unqualified:
45: String matchText = unit.getBuffer().getText(start, length);
46: if (fOldName.equals(matchText)) {
47: return match;
48: }
49:
50: //(partially) qualified:
51: if (fOldQualifiedName.endsWith(matchText)) {
52: //e.g. rename B and p.A.B ends with match A.B
53: int simpleNameLenght = fOldName.length();
54: match.setOffset(start + length - simpleNameLenght);
55: match.setLength(simpleNameLenght);
56: return match;
57: }
58:
59: //Not a standard reference -- use scanner to find last identifier token:
60: IScanner scanner = getScanner(unit);
61: scanner.setSource(matchText.toCharArray());
62: int simpleNameStart = -1;
63: int simpleNameEnd = -1;
64: try {
65: int token = scanner.getNextToken();
66: while (token != ITerminalSymbols.TokenNameEOF) {
67: if (token == ITerminalSymbols.TokenNameIdentifier) {
68: simpleNameStart = scanner
69: .getCurrentTokenStartPosition();
70: simpleNameEnd = scanner
71: .getCurrentTokenEndPosition();
72: }
73: token = scanner.getNextToken();
74: }
75: } catch (InvalidInputException e) {
76: //ignore
77: }
78: if (simpleNameStart != -1) {
79: match.setOffset(start + simpleNameStart);
80: match.setLength(simpleNameEnd + 1 - simpleNameStart);
81: }
82: return match;
83: }
84: }
|