KickJava   Java API By Example, From Geeks To Geeks.

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


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  * 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.Arrays JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.eclipse.core.runtime.NullProgressMonitor;
21
22 import org.eclipse.jface.preference.IPreferenceStore;
23
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IMember;
26 import org.eclipse.jdt.core.IMethod;
27 import org.eclipse.jdt.core.ITypeRoot;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.dom.AST;
30 import org.eclipse.jdt.core.dom.ASTParser;
31 import org.eclipse.jdt.core.dom.CompilationUnit;
32 import org.eclipse.jdt.core.search.IJavaSearchScope;
33 import org.eclipse.jdt.core.search.SearchEngine;
34
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
36 import org.eclipse.jdt.internal.ui.util.StringMatcher;
37
38 public class CallHierarchy {
39     private static final String JavaDoc PREF_USE_IMPLEMENTORS= "PREF_USE_IMPLEMENTORS"; //$NON-NLS-1$
40
private static final String JavaDoc PREF_USE_FILTERS = "PREF_USE_FILTERS"; //$NON-NLS-1$
41
private static final String JavaDoc PREF_FILTERS_LIST = "PREF_FILTERS_LIST"; //$NON-NLS-1$
42

43     private static final String JavaDoc DEFAULT_IGNORE_FILTERS = "java.*,javax.*"; //$NON-NLS-1$
44
private static CallHierarchy fgInstance;
45     private IJavaSearchScope fSearchScope;
46     private StringMatcher[] fFilters;
47
48     public static CallHierarchy getDefault() {
49         if (fgInstance == null) {
50             fgInstance = new CallHierarchy();
51         }
52
53         return fgInstance;
54     }
55
56     public boolean isSearchUsingImplementorsEnabled() {
57         IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore();
58
59         return settings.getBoolean(PREF_USE_IMPLEMENTORS);
60     }
61
62     public void setSearchUsingImplementorsEnabled(boolean enabled) {
63         IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore();
64
65         settings.setValue(PREF_USE_IMPLEMENTORS, enabled);
66     }
67
68     public Collection JavaDoc getImplementingMethods(IMethod method) {
69         if (isSearchUsingImplementorsEnabled()) {
70             IJavaElement[] result = Implementors.getInstance().searchForImplementors(new IJavaElement[] {
71                         method
72                     }, new NullProgressMonitor());
73
74             if ((result != null) && (result.length > 0)) {
75                 return Arrays.asList(result);
76             }
77         }
78
79         return new ArrayList JavaDoc(0);
80     }
81
82     public Collection JavaDoc getInterfaceMethods(IMethod method) {
83         if (isSearchUsingImplementorsEnabled()) {
84             IJavaElement[] result = Implementors.getInstance().searchForInterfaces(new IJavaElement[] {
85                         method
86                     }, new NullProgressMonitor());
87
88             if ((result != null) && (result.length > 0)) {
89                 return Arrays.asList(result);
90             }
91         }
92
93         return new ArrayList JavaDoc(0);
94     }
95
96     public MethodWrapper getCallerRoot(IMethod method) {
97         return new CallerMethodWrapper(null, new MethodCall(method));
98     }
99
100     public MethodWrapper getCalleeRoot(IMethod method) {
101         return new CalleeMethodWrapper(null, new MethodCall(method));
102     }
103
104     public static CallLocation getCallLocation(Object JavaDoc element) {
105         CallLocation callLocation = null;
106
107         if (element instanceof MethodWrapper) {
108             MethodWrapper methodWrapper = (MethodWrapper) element;
109             MethodCall methodCall = methodWrapper.getMethodCall();
110
111             if (methodCall != null) {
112                 callLocation = methodCall.getFirstCallLocation();
113             }
114         } else if (element instanceof CallLocation) {
115             callLocation = (CallLocation) element;
116         }
117
118         return callLocation;
119     }
120
121     public IJavaSearchScope getSearchScope() {
122         if (fSearchScope == null) {
123             fSearchScope= SearchEngine.createWorkspaceScope();
124         }
125
126         return fSearchScope;
127     }
128
129     public void setSearchScope(IJavaSearchScope searchScope) {
130         this.fSearchScope = searchScope;
131     }
132
133     /**
134      * Checks whether the fully qualified name is ignored by the set filters.
135      *
136      * @param fullyQualifiedName
137      *
138      * @return True if the fully qualified name is ignored.
139      */

140     public boolean isIgnored(String JavaDoc fullyQualifiedName) {
141         if ((getIgnoreFilters() != null) && (getIgnoreFilters().length > 0)) {
142             for (int i = 0; i < getIgnoreFilters().length; i++) {
143                 String JavaDoc fullyQualifiedName1 = fullyQualifiedName;
144
145                 if (getIgnoreFilters()[i].match(fullyQualifiedName1)) {
146                     return true;
147                 }
148             }
149         }
150
151         return false;
152     }
153
154     public boolean isFilterEnabled() {
155         IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore();
156         return settings.getBoolean(PREF_USE_FILTERS);
157     }
158
159     public void setFilterEnabled(boolean filterEnabled) {
160         IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore();
161         settings.setValue(PREF_USE_FILTERS, filterEnabled);
162     }
163     
164     /**
165      * Returns the current filters as a string.
166      * @return returns the filters
167      */

168     public String JavaDoc getFilters() {
169         IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore();
170
171         return settings.getString(PREF_FILTERS_LIST);
172     }
173
174     public void setFilters(String JavaDoc filters) {
175         fFilters = null;
176
177         IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore();
178         settings.setValue(PREF_FILTERS_LIST, filters);
179     }
180
181     /**
182      * Returns filters for packages which should not be included in the search results.
183      *
184      * @return StringMatcher[]
185      */

186     private StringMatcher[] getIgnoreFilters() {
187         if (fFilters == null) {
188             String JavaDoc filterString = null;
189
190             if (isFilterEnabled()) {
191                 filterString = getFilters();
192
193                 if (filterString == null) {
194                     filterString = DEFAULT_IGNORE_FILTERS;
195                 }
196             }
197
198             if (filterString != null) {
199                 fFilters = parseList(filterString);
200             } else {
201                 fFilters = null;
202             }
203         }
204
205         return fFilters;
206     }
207
208     /**
209      * Parses the comma separated string into an array of StringMatcher objects
210      * @param listString the string to parse
211      *
212      * @return list
213      */

214     private static StringMatcher[] parseList(String JavaDoc listString) {
215         List JavaDoc list = new ArrayList JavaDoc(10);
216         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(listString, ","); //$NON-NLS-1$
217

218         while (tokenizer.hasMoreTokens()) {
219             String JavaDoc textFilter = tokenizer.nextToken().trim();
220             list.add(new StringMatcher(textFilter, false, false));
221         }
222
223         return (StringMatcher[]) list.toArray(new StringMatcher[list.size()]);
224     }
225     
226     static CompilationUnit getCompilationUnitNode(IMember member, boolean resolveBindings) {
227         ITypeRoot typeRoot= member.getTypeRoot();
228         try {
229             if (typeRoot.exists() && typeRoot.getBuffer() != null) {
230                 ASTParser parser= ASTParser.newParser(AST.JLS3);
231                 parser.setSource(typeRoot);
232                 parser.setResolveBindings(resolveBindings);
233                 return (CompilationUnit) parser.createAST(null);
234             }
235         } catch (JavaModelException e) {
236             JavaPlugin.log(e);
237         }
238         return null;
239     }
240 }
241
Popular Tags