KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > callhierarchy > Implementors


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.corext.callhierarchy;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.SubProgressMonitor;
19
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IMember;
22 import org.eclipse.jdt.core.IMethod;
23 import org.eclipse.jdt.core.IType;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27
28 /**
29  * The main plugin class to be used in the desktop.
30  */

31 public class Implementors {
32     private static IImplementorFinder[] IMPLEMENTOR_FINDERS= new IImplementorFinder[] { new JavaImplementorFinder() };
33     private static Implementors fgInstance;
34
35     /**
36      * Returns the shared instance.
37      */

38     public static Implementors getInstance() {
39         if (fgInstance == null) {
40             fgInstance = new Implementors();
41         }
42
43         return fgInstance;
44     }
45
46     /**
47      * Searches for implementors of the specified Java elements. Currently, only IMethod
48      * instances are searched for. Also, only the first element of the elements
49      * parameter is taken into consideration.
50      *
51      * @param elements
52      *
53      * @return An array of found implementing Java elements (currently only IMethod
54      * instances)
55      */

56     public IJavaElement[] searchForImplementors(IJavaElement[] elements,
57         IProgressMonitor progressMonitor) {
58         if ((elements != null) && (elements.length > 0)) {
59             IJavaElement element = elements[0];
60
61             try {
62                 if (element instanceof IMember) {
63                     IMember member = (IMember) element;
64                     IType type = member.getDeclaringType();
65
66                     if (type.isInterface()) {
67                         IType[] implementingTypes = findImplementingTypes(type,
68                                 progressMonitor);
69
70                         if (member.getElementType() == IJavaElement.METHOD) {
71                             return findMethods((IMethod)member, implementingTypes, progressMonitor);
72                         } else {
73                             return implementingTypes;
74                         }
75                     }
76                 }
77             } catch (JavaModelException e) {
78                 JavaPlugin.log(e);
79             }
80         }
81
82         return null;
83     }
84
85     /**
86      * Searches for interfaces which are implemented by the declaring classes of the
87      * specified Java elements. Currently, only IMethod instances are searched for.
88      * Also, only the first element of the elements parameter is taken into
89      * consideration.
90      *
91      * @param elements
92      *
93      * @return An array of found interfaces implemented by the declaring classes of the
94      * specified Java elements (currently only IMethod instances)
95      */

96     public IJavaElement[] searchForInterfaces(IJavaElement[] elements,
97         IProgressMonitor progressMonitor) {
98         if ((elements != null) && (elements.length > 0)) {
99             IJavaElement element = elements[0];
100
101             if (element instanceof IMember) {
102                 IMember member = (IMember) element;
103                 IType type = member.getDeclaringType();
104
105                 IType[] implementingTypes = findInterfaces(type, progressMonitor);
106
107                 if (!progressMonitor.isCanceled()) {
108                     if (member.getElementType() == IJavaElement.METHOD) {
109                         return findMethods((IMethod)member, implementingTypes, progressMonitor);
110                     } else {
111                         return implementingTypes;
112                     }
113                 }
114             }
115         }
116
117         return null;
118     }
119
120     private IImplementorFinder[] getImplementorFinders() {
121         return IMPLEMENTOR_FINDERS;
122     }
123
124     private IType[] findImplementingTypes(IType type, IProgressMonitor progressMonitor) {
125         Collection JavaDoc implementingTypes = new ArrayList JavaDoc();
126
127         IImplementorFinder[] finders = getImplementorFinders();
128
129         for (int i = 0; (i < finders.length) && !progressMonitor.isCanceled(); i++) {
130             Collection JavaDoc types = finders[i].findImplementingTypes(type,
131                     new SubProgressMonitor(progressMonitor, 10,
132                         SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
133
134             if (types != null) {
135                 implementingTypes.addAll(types);
136             }
137         }
138
139         return (IType[]) implementingTypes.toArray(new IType[implementingTypes.size()]);
140     }
141
142     private IType[] findInterfaces(IType type, IProgressMonitor progressMonitor) {
143         Collection JavaDoc interfaces = new ArrayList JavaDoc();
144
145         IImplementorFinder[] finders = getImplementorFinders();
146
147         for (int i = 0; (i < finders.length) && !progressMonitor.isCanceled(); i++) {
148             Collection JavaDoc types = finders[i].findInterfaces(type,
149                     new SubProgressMonitor(progressMonitor, 10,
150                         SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
151
152             if (types != null) {
153                 interfaces.addAll(types);
154             }
155         }
156
157         return (IType[]) interfaces.toArray(new IType[interfaces.size()]);
158     }
159
160     /**
161      * Finds IMethod instances on the specified IType instances with identical signatures
162      * as the specified IMethod parameter.
163      *
164      * @param method The method to find "equals" of.
165      * @param types The types in which the search is performed.
166      *
167      * @return An array of methods which match the method parameter.
168      */

169     private IJavaElement[] findMethods(IMethod method, IType[] types,
170         IProgressMonitor progressMonitor) {
171         Collection JavaDoc foundMethods = new ArrayList JavaDoc();
172
173         SubProgressMonitor subProgressMonitor = new SubProgressMonitor(progressMonitor,
174                 10, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
175         subProgressMonitor.beginTask("", types.length); //$NON-NLS-1$
176

177         try {
178             for (int i = 0; i < types.length; i++) {
179                 IType type = types[i];
180                 IMethod[] methods = type.findMethods(method);
181
182                 if (methods != null) {
183                     for (int j = 0; j < methods.length; j++) {
184                         foundMethods.add(methods[j]);
185                     }
186                 }
187
188                 subProgressMonitor.worked(1);
189             }
190         } finally {
191             subProgressMonitor.done();
192         }
193
194         return (IJavaElement[]) foundMethods.toArray(new IJavaElement[foundMethods.size()]);
195     }
196 }
197
Popular Tags