]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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 | * Stephan Herrmann (stephan@cs.tu-berlin.de): | |
12 | * - bug 206949: [call hierarchy] filter field accesses (only write or only read) | |
13 | *******************************************************************************/ | |
14 | package org.eclipse.jdt.internal.corext.callhierarchy; | |
15 | ||
16 | import java.util.HashMap; | |
17 | import java.util.Map; | |
18 | ||
19 | import org.eclipse.core.runtime.CoreException; | |
20 | import org.eclipse.core.runtime.IProgressMonitor; | |
21 | import org.eclipse.core.runtime.SubProgressMonitor; | |
22 | ||
23 | import org.eclipse.jdt.core.Flags; | |
24 | import org.eclipse.jdt.core.IField; | |
25 | import org.eclipse.jdt.core.IInitializer; | |
26 | import org.eclipse.jdt.core.IJavaElement; | |
27 | import org.eclipse.jdt.core.IMember; | |
28 | import org.eclipse.jdt.core.IMethod; | |
29 | import org.eclipse.jdt.core.ISourceRange; | |
30 | import org.eclipse.jdt.core.IType; | |
31 | import org.eclipse.jdt.core.JavaModelException; | |
32 | import org.eclipse.jdt.core.search.IJavaSearchConstants; | |
33 | import org.eclipse.jdt.core.search.IJavaSearchScope; | |
34 | import org.eclipse.jdt.core.search.SearchEngine; | |
35 | import org.eclipse.jdt.core.search.SearchPattern; | |
36 | ||
37 | import org.eclipse.jdt.internal.corext.util.JdtFlags; | |
38 | import org.eclipse.jdt.internal.corext.util.SearchUtils; | |
39 | ||
40 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
41 | import org.eclipse.jdt.internal.ui.callhierarchy.ExpandWithConstructorsAction; | |
42 | ||
43 | public class CallerMethodWrapper extends MethodWrapper { | |
44 | /** | |
45 | * Value of the expand with constructors mode. | |
46 | * | |
47 | * @since 3.5 | |
48 | */ | |
49 | private boolean fExpandWithConstructors; | |
50 | ||
51 | /** | |
52 | * Tells whether the expand with constructors mode has been set. | |
53 | * | |
54 | * @see #setExpandWithConstructors(boolean) | |
55 | * @since 3.5 | |
56 | */ | |
57 | private boolean fIsExpandWithConstructorsSet; | |
58 | ||
59 | public CallerMethodWrapper(MethodWrapper parent, MethodCall methodCall) { | |
60 | super(parent, methodCall); | |
61 | } | |
62 | ||
63 | protected IJavaSearchScope getSearchScope() { | |
64 | return CallHierarchy.getDefault().getSearchScope(); | |
65 | } | |
66 | ||
67 | @Override | |
68 | protected String getTaskName() { | |
69 | return CallHierarchyMessages.CallerMethodWrapper_taskname; | |
70 | } | |
71 | ||
72 | /* (non-Javadoc) | |
73 | * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#createMethodWrapper(org.eclipse.jdt.internal.corext.callhierarchy.MethodCall) | |
74 | */ | |
75 | @Override | |
76 | public MethodWrapper createMethodWrapper(MethodCall methodCall) { | |
77 | return new CallerMethodWrapper(this, methodCall); | |
78 | } | |
79 | ||
80 | /* | |
81 | * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#canHaveChildren() | |
82 | */ | |
83 | @Override | |
84 | public boolean canHaveChildren() { | |
85 | IMember member= getMember(); | |
86 | if (member instanceof IField) { | |
87 | if (getLevel() == 1) | |
88 | return true; | |
89 | int mode= getFieldSearchMode(); | |
90 | return mode == IJavaSearchConstants.REFERENCES || mode == IJavaSearchConstants.READ_ACCESSES; | |
91 | } | |
92 | return member instanceof IMethod || member instanceof IType; | |
93 | } | |
94 | ||
95 | /** | |
96 | * @return The result of the search for children | |
97 | * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#findChildren(org.eclipse.core.runtime.IProgressMonitor) | |
98 | */ | |
99 | @Override | |
100 | protected Map<String, MethodCall> findChildren(IProgressMonitor progressMonitor) { | |
101 | try { | |
102 | ||
103 | IProgressMonitor monitor= new SubProgressMonitor(progressMonitor, 95, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL); | |
104 | ||
105 | checkCanceled(progressMonitor); | |
106 | ||
107 | IMember member= getMember(); | |
108 | SearchPattern pattern= null; | |
109 | IType type= null; | |
110 | if (member instanceof IType) { | |
111 | type= (IType) member; | |
112 | } else if (member instanceof IInitializer && ! Flags.isStatic(member.getFlags())) { | |
113 | type= (IType) member.getParent(); | |
114 | } | |
115 | if (type != null) { | |
116 | if (type.isAnonymous()) { | |
117 | // search engine does not find reference to anonymous, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=207774 | |
118 | CallSearchResultCollector resultCollector= new CallSearchResultCollector(); | |
119 | IJavaElement parent= type.getParent(); | |
120 | if (parent instanceof IMember) { | |
121 | IMember parentMember= (IMember) parent; | |
122 | ISourceRange nameRange= type.getNameRange(); | |
123 | int start= nameRange != null ? nameRange.getOffset() : -1; | |
124 | int len= nameRange != null ? nameRange.getLength() : 0; | |
125 | resultCollector.addMember(type, parentMember, start, start + len); | |
126 | return resultCollector.getCallers(); | |
127 | } | |
128 | } else if (type.getParent() instanceof IMethod) { | |
129 | // good enough for local types (does not find super(..) references in subtype constructors): | |
130 | pattern= SearchPattern.createPattern(type, | |
131 | IJavaSearchConstants.CLASS_INSTANCE_CREATION_TYPE_REFERENCE, | |
132 | SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE); | |
133 | } else { | |
134 | pattern= SearchPattern.createPattern(type.getFullyQualifiedName('.'), | |
135 | IJavaSearchConstants.CONSTRUCTOR, | |
136 | IJavaSearchConstants.REFERENCES, | |
137 | SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE); | |
138 | } | |
139 | } | |
140 | if (pattern == null) { | |
141 | int limitTo= IJavaSearchConstants.REFERENCES; | |
142 | if (member.getElementType() == IJavaElement.FIELD) | |
143 | limitTo= getFieldSearchMode(); | |
144 | pattern= SearchPattern.createPattern(member, limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE); | |
145 | } | |
146 | if (pattern == null) { // e.g. for initializers | |
147 | return new HashMap<String, MethodCall>(0); | |
148 | } | |
149 | ||
150 | SearchEngine searchEngine= new SearchEngine(); | |
151 | MethodReferencesSearchRequestor searchRequestor= new MethodReferencesSearchRequestor(); | |
152 | IJavaSearchScope defaultSearchScope= getSearchScope(); | |
153 | return searchRequestor.generated_5737876568461377929(monitor, member, pattern, searchEngine, this, defaultSearchScope); | |
154 | ||
155 | } catch (CoreException e) { | |
156 | JavaPlugin.log(e); | |
157 | return new HashMap<String, MethodCall>(0); | |
158 | } | |
159 | } | |
160 | ||
161 | IJavaSearchScope getAccurateSearchScope(IJavaSearchScope defaultSearchScope, IMember member) throws JavaModelException { | |
162 | if (! JdtFlags.isPrivate(member)) | |
163 | return defaultSearchScope; | |
164 | ||
165 | if (member.getCompilationUnit() != null) { | |
166 | return SearchEngine.createJavaSearchScope(new IJavaElement[] { member.getCompilationUnit() }); | |
167 | } else if (member.getClassFile() != null) { | |
168 | // member could be called from an inner class-> search | |
169 | // package fragment (see also bug 109053): | |
170 | return SearchEngine.createJavaSearchScope(new IJavaElement[] { member.getAncestor(IJavaElement.PACKAGE_FRAGMENT) }); | |
171 | } else { | |
172 | return defaultSearchScope; | |
173 | } | |
174 | } | |
175 | ||
176 | /** | |
177 | * Returns the value of expand with constructors mode. | |
178 | * | |
179 | * @return <code>true</code> if in expand with constructors mode, <code>false</code> otherwise or if not yet set | |
180 | * @see #isExpandWithConstructorsSet() | |
181 | * | |
182 | * @since 3.5 | |
183 | */ | |
184 | public boolean getExpandWithConstructors() { | |
185 | return fIsExpandWithConstructorsSet && fExpandWithConstructors; | |
186 | } | |
187 | ||
188 | /** | |
189 | * Sets the expand with constructors mode. | |
190 | * | |
191 | * @param value <code>true</code> if in expand with constructors mode, <code>false</code> | |
192 | * otherwise | |
193 | * @since 3.5 | |
194 | */ | |
195 | public void setExpandWithConstructors(boolean value) { | |
196 | fExpandWithConstructors= value; | |
197 | fIsExpandWithConstructorsSet= true; | |
198 | ||
199 | } | |
200 | ||
201 | /** | |
202 | * Tells whether the expand with constructors mode has been set. | |
203 | * | |
204 | * @return <code>true</code> if expand with constructors mode has been set explicitly, <code>false</code> otherwise | |
205 | * @see #setExpandWithConstructors(boolean) | |
206 | * @since 3.5 | |
207 | */ | |
208 | public boolean isExpandWithConstructorsSet() { | |
209 | return fIsExpandWithConstructorsSet; | |
210 | } | |
211 | ||
212 | public Object[] generated_2421808467164206615(MethodWrapper methodWrapper, IMember[] constructors, Object[] children) { | |
213 | for (int j= 0; j < constructors.length; j++) { | |
214 | MethodCall constructor= new MethodCall(constructors[j]); | |
215 | CallerMethodWrapper constructorWrapper= (CallerMethodWrapper)createMethodWrapper(constructor); | |
216 | children[j]= constructorWrapper; | |
217 | } | |
218 | children[constructors.length]= new RealCallers(methodWrapper, getMethodCall()); | |
219 | return children; | |
220 | } | |
221 | ||
222 | public void generated_2297222693293678322(boolean isChecked, ExpandWithConstructorsAction expandwithconstructorsaction) { | |
223 | setExpandWithConstructors(isChecked); | |
224 | if (!isChecked) { // must collapse before refresh | |
225 | expandwithconstructorsaction.fCallHierarchyViewer.setExpandedState(this, false); | |
226 | } | |
227 | } | |
228 | ||
229 | } |