KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > ImplementOccurrencesFinder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21
22 import org.eclipse.search.ui.text.Match;
23
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.dom.ASTNode;
26 import org.eclipse.jdt.core.dom.ASTVisitor;
27 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
28 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
29 import org.eclipse.jdt.core.dom.CompilationUnit;
30 import org.eclipse.jdt.core.dom.IMethodBinding;
31 import org.eclipse.jdt.core.dom.ITypeBinding;
32 import org.eclipse.jdt.core.dom.MethodDeclaration;
33 import org.eclipse.jdt.core.dom.Name;
34 import org.eclipse.jdt.core.dom.Type;
35 import org.eclipse.jdt.core.dom.TypeDeclarationStatement;
36
37 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
38 import org.eclipse.jdt.internal.corext.dom.Bindings;
39 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
40
41
42 /**
43  * Finds all implement occurrences of an extended class or an implemented interface.
44  *
45  * @since 3.1
46  */

47 public class ImplementOccurrencesFinder implements org.eclipse.jdt.internal.ui.search.IOccurrencesFinder {
48     
49     
50     private class MethodVisitor extends ASTVisitor {
51         
52         /*
53          * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodDeclaration)
54          */

55         public boolean visit(MethodDeclaration node) {
56             IMethodBinding binding= node.resolveBinding();
57             if (binding != null) {
58                 IMethodBinding method= Bindings.findOverriddenMethodInHierarchy(fSelectedType, binding);
59                 if (method != null)
60                     fResult.add(node.getName());
61             }
62             return super.visit(node);
63         }
64         
65         /*
66          * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.AnonymousClassDeclaration)
67          */

68         public boolean visit(AnonymousClassDeclaration node) {
69             // don't dive into anonymous type declarations.
70
return false;
71         }
72         
73         /*
74          * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.TypeDeclarationStatement)
75          */

76         public boolean visit(TypeDeclarationStatement node) {
77             // don't dive into local type declarations.
78
return false;
79         }
80     }
81     
82     
83     private ASTNode fStart;
84     private List JavaDoc fResult;
85     private ASTNode fSelectedNode;
86     private ITypeBinding fSelectedType;
87     
88     public ImplementOccurrencesFinder() {
89         fResult= new ArrayList JavaDoc();
90     }
91     
92     public String JavaDoc initialize(CompilationUnit root, int offset, int length) {
93         return initialize(root, NodeFinder.perform(root, offset, length));
94     }
95     
96     public String JavaDoc initialize(CompilationUnit root, ASTNode node) {
97         if (!(node instanceof Name))
98             return SearchMessages.ImplementOccurrencesFinder_invalidTarget;
99         
100         fSelectedNode= ASTNodes.getNormalizedNode(node);
101         if (!(fSelectedNode instanceof Type))
102             return SearchMessages.ImplementOccurrencesFinder_invalidTarget;
103         
104         ASTNode typeDeclaration= fSelectedNode.getParent();
105         if (!(typeDeclaration instanceof AbstractTypeDeclaration))
106             return SearchMessages.ImplementOccurrencesFinder_invalidTarget;
107         
108         fSelectedType= ((Type)fSelectedNode).resolveBinding();
109         if (fSelectedType == null)
110             return SearchMessages.ImplementOccurrencesFinder_invalidTarget;
111
112         fStart= typeDeclaration;
113         return null;
114     }
115     
116     /*
117      * @see org.eclipse.jdt.internal.ui.search.IOccurrencesFinder#perform()
118      */

119     public List JavaDoc perform() {
120         fStart.accept(new MethodVisitor());
121         if (fSelectedNode != null)
122             fResult.add(fSelectedNode);
123         
124         return fResult;
125     }
126     
127     public void collectOccurrenceMatches(IJavaElement element, IDocument document, Collection JavaDoc resultingMatches) {
128         for (Iterator JavaDoc iter= fResult.iterator(); iter.hasNext();) {
129             ASTNode node= (ASTNode) iter.next();
130             int startPosition= node.getStartPosition();
131             int length= node.getLength();
132             try {
133                 int line= document.getLineOfOffset(startPosition);
134                 IRegion region= document.getLineInformation(line);
135                 String JavaDoc lineContents= document.get(region.getOffset(), region.getLength()).trim();
136                 JavaElementLine groupKey= new JavaElementLine(element, line, lineContents);
137                 resultingMatches.add(new Match(groupKey, startPosition, length));
138             } catch (BadLocationException e) {
139                 //nothing
140
}
141         }
142     }
143     
144     /*
145      * @see org.eclipse.jdt.internal.ui.search.IOccurrencesFinder#getJobLabel()
146      */

147     public String JavaDoc getJobLabel() {
148         return SearchMessages.ImplementOccurrencesFinder_searchfor ;
149     }
150     
151     public String JavaDoc getElementName() {
152         if (fSelectedNode != null) {
153             return ASTNodes.asString(fSelectedNode);
154         }
155         return null;
156     }
157     
158     public String JavaDoc getUnformattedPluralLabel() {
159         return SearchMessages.ImplementOccurrencesFinder_label_plural;
160     }
161     
162     public String JavaDoc getUnformattedSingularLabel() {
163         return SearchMessages.ImplementOccurrencesFinder_label_singular;
164     }
165     
166     public void releaseAST() {
167         fStart= null;
168         fSelectedType= null;
169     }
170     
171 }
172
Popular Tags