KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IMember;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.core.search.IJavaSearchConstants;
28 import org.eclipse.jdt.core.search.IJavaSearchScope;
29 import org.eclipse.jdt.core.search.SearchEngine;
30 import org.eclipse.jdt.core.search.SearchParticipant;
31 import org.eclipse.jdt.core.search.SearchPattern;
32
33 import org.eclipse.jdt.internal.corext.util.JdtFlags;
34 import org.eclipse.jdt.internal.corext.util.SearchUtils;
35
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37
38 class CallerMethodWrapper extends MethodWrapper {
39     public CallerMethodWrapper(MethodWrapper parent, MethodCall methodCall) {
40         super(parent, methodCall);
41     }
42
43     protected IJavaSearchScope getSearchScope() {
44         return CallHierarchy.getDefault().getSearchScope();
45     }
46
47     protected String JavaDoc getTaskName() {
48         return CallHierarchyMessages.CallerMethodWrapper_taskname;
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#createMethodWrapper(org.eclipse.jdt.internal.corext.callhierarchy.MethodCall)
53      */

54     protected MethodWrapper createMethodWrapper(MethodCall methodCall) {
55         return new CallerMethodWrapper(this, methodCall);
56     }
57
58     /**
59      * @return The result of the search for children
60      * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#findChildren(org.eclipse.core.runtime.IProgressMonitor)
61      */

62     protected Map JavaDoc findChildren(IProgressMonitor progressMonitor) {
63         try {
64             MethodReferencesSearchRequestor searchRequestor= new MethodReferencesSearchRequestor();
65             SearchEngine searchEngine= new SearchEngine();
66
67             IProgressMonitor monitor= new SubProgressMonitor(progressMonitor, 95, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
68             IJavaSearchScope defaultSearchScope= getSearchScope();
69             boolean isWorkspaceScope= SearchEngine.createWorkspaceScope().equals(defaultSearchScope);
70
71             for (Iterator JavaDoc iter= getMembers().iterator(); iter.hasNext();) {
72                 checkCanceled(progressMonitor);
73
74                 IMember member= (IMember) iter.next();
75                 SearchPattern pattern= SearchPattern.createPattern(member, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
76                 IJavaSearchScope searchScope= isWorkspaceScope ? getAccurateSearchScope(defaultSearchScope, member) : defaultSearchScope;
77                 searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, searchScope, searchRequestor,
78                         monitor);
79             }
80             return searchRequestor.getCallers();
81             
82         } catch (CoreException e) {
83             JavaPlugin.log(e);
84             return new HashMap JavaDoc(0);
85         }
86     }
87
88     private IJavaSearchScope getAccurateSearchScope(IJavaSearchScope defaultSearchScope, IMember member) throws JavaModelException {
89         if (! JdtFlags.isPrivate(member))
90             return defaultSearchScope;
91         
92         if (member.getCompilationUnit() != null) {
93             return SearchEngine.createJavaSearchScope(new IJavaElement[] { member.getCompilationUnit() });
94         } else if (member.getClassFile() != null) {
95             // member could be called from an inner class-> search
96
// package fragment (see also bug 109053):
97
return SearchEngine.createJavaSearchScope(new IJavaElement[] { member.getAncestor(IJavaElement.PACKAGE_FRAGMENT) });
98         } else {
99             return defaultSearchScope;
100         }
101     }
102
103     /**
104      * Returns a collection of IMember instances representing what to search for
105      */

106     private Collection JavaDoc getMembers() {
107         Collection JavaDoc result = new ArrayList JavaDoc();
108
109         result.add(getMember());
110
111         return result;
112     }
113 }
114
Popular Tags