KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.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.MethodReferenceMatch;
20 import org.eclipse.jdt.core.search.SearchMatch;
21
22 import org.eclipse.jdt.internal.corext.refactoring.CuCollectingSearchRequestor;
23
24 class MethodOccurenceCollector extends CuCollectingSearchRequestor {
25
26     private final String JavaDoc fName;
27
28     public MethodOccurenceCollector(String JavaDoc methodName) {
29         fName= methodName;
30     }
31
32     public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
33         if (match instanceof MethodReferenceMatch
34                 && ((MethodReferenceMatch) match).isSuperInvocation()
35                 && match.getAccuracy() == SearchMatch.A_INACCURATE) {
36             return; // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=156491
37
}
38         
39         if (match.isImplicit()) { // see bug 94062
40
collectMatch(match);
41             return;
42         }
43         
44         int start= match.getOffset();
45         int length= match.getLength();
46         String JavaDoc matchText= unit.getBuffer().getText(start, length);
47         
48         //direct match:
49
if (fName.equals(matchText)) {
50             collectMatch(match);
51             return;
52         }
53                     
54         //Not a standard reference -- use scanner to find last identifier token before left parenthesis:
55
IScanner scanner= getScanner(unit);
56         scanner.setSource(matchText.toCharArray());
57         int simpleNameStart= -1;
58         int simpleNameEnd= -1;
59         try {
60             int token = scanner.getNextToken();
61             while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLPAREN) { // reference in code includes arguments in parentheses
62
if (token == ITerminalSymbols.TokenNameIdentifier) {
63                     simpleNameStart= scanner.getCurrentTokenStartPosition();
64                     simpleNameEnd= scanner.getCurrentTokenEndPosition();
65                 }
66                 token = scanner.getNextToken();
67             }
68         } catch (InvalidInputException e){
69             //ignore
70
}
71         if (simpleNameStart != -1) {
72             match.setOffset(start + simpleNameStart);
73             match.setLength(simpleNameEnd + 1 - simpleNameStart);
74         }
75         collectMatch(match);
76     }
77 }
78
Popular Tags