]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2007, 2011 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 | import org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext; | |
24 | ||
25 | public class TypeOccurrenceCollector extends CuCollectingSearchRequestor { | |
26 | ||
27 | private final String fOldName; | |
28 | private final String fOldQualifiedName; | |
29 | ||
30 | public TypeOccurrenceCollector(IType type) { | |
31 | this(type, null); | |
32 | } | |
33 | ||
34 | public TypeOccurrenceCollector(IType type, ReferencesInBinaryContext binaryRefs) { | |
35 | super(binaryRefs); | |
36 | fOldName= type.getElementName(); | |
37 | fOldQualifiedName= type.getFullyQualifiedName('.'); | |
38 | } | |
39 | ||
40 | @Override | |
41 | public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException { | |
42 | collectMatch(acceptSearchMatch2(unit, match)); | |
43 | } | |
44 | ||
45 | public SearchMatch acceptSearchMatch2(ICompilationUnit unit, SearchMatch match) throws CoreException { | |
46 | int start= match.getOffset(); | |
47 | int length= match.getLength(); | |
48 | ||
49 | //unqualified: | |
50 | String matchText= unit.getBuffer().getText(start, length); | |
51 | if (fOldName.equals(matchText)) { | |
52 | return match; | |
53 | } | |
54 | ||
55 | //(partially) qualified: | |
56 | if (fOldQualifiedName.endsWith(matchText)) { | |
57 | //e.g. rename B and p.A.B ends with match A.B | |
58 | int simpleNameLenght= fOldName.length(); | |
59 | match.setOffset(start + length - simpleNameLenght); | |
60 | match.setLength(simpleNameLenght); | |
61 | return match; | |
62 | } | |
63 | ||
64 | //Not a standard reference -- use scanner to find last identifier token: | |
65 | IScanner scanner= getScanner(unit); | |
66 | scanner.setSource(matchText.toCharArray()); | |
67 | int simpleNameStart= -1; | |
68 | int simpleNameEnd= -1; | |
69 | try { | |
70 | int token = scanner.getNextToken(); | |
71 | while (token != ITerminalSymbols.TokenNameEOF) { | |
72 | if (token == ITerminalSymbols.TokenNameIdentifier) { | |
73 | simpleNameStart= scanner.getCurrentTokenStartPosition(); | |
74 | simpleNameEnd= scanner.getCurrentTokenEndPosition(); | |
75 | } | |
76 | token = scanner.getNextToken(); | |
77 | } | |
78 | } catch (InvalidInputException e){ | |
79 | //ignore | |
80 | } | |
81 | if (simpleNameStart != -1) { | |
82 | match.setOffset(start + simpleNameStart); | |
83 | match.setLength(simpleNameEnd + 1 - simpleNameStart); | |
84 | } | |
85 | return match; | |
86 | } | |
87 | } |