Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 288464 | Differences between
and this patch

Collapse All | Expand All

(-)ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementHyperlinkImplementationDetector.java (-1 / +1 lines)
Lines 40-46 Link Here
40
	 */
40
	 */
41
	protected IHyperlink createHyperlink(IRegion wordRegion, SelectionDispatchAction openAction, IJavaElement element, boolean qualify, ITextEditor editor) {
41
	protected IHyperlink createHyperlink(IRegion wordRegion, SelectionDispatchAction openAction, IJavaElement element, boolean qualify, ITextEditor editor) {
42
		if (element.getElementType() == IJavaElement.METHOD && canBeOverridden((IMethod)element)) {
42
		if (element.getElementType() == IJavaElement.METHOD && canBeOverridden((IMethod)element)) {
43
			return new JavaElementImplementationHyperlink(wordRegion, openAction, element, qualify, editor);
43
			return new JavaElementImplementationHyperlink(wordRegion, openAction, (IMethod)element, qualify, editor);
44
		}
44
		}
45
		return null;
45
		return null;
46
	}
46
	}
(-)ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementImplementationHyperlink.java (-28 / +77 lines)
Lines 36-45 Link Here
36
36
37
import org.eclipse.ui.texteditor.ITextEditor;
37
import org.eclipse.ui.texteditor.ITextEditor;
38
38
39
import org.eclipse.jdt.core.IJavaElement;
40
import org.eclipse.jdt.core.IMethod;
39
import org.eclipse.jdt.core.IMethod;
41
import org.eclipse.jdt.core.IType;
40
import org.eclipse.jdt.core.IType;
41
import org.eclipse.jdt.core.ITypeHierarchy;
42
import org.eclipse.jdt.core.ITypeRoot;
42
import org.eclipse.jdt.core.ITypeRoot;
43
import org.eclipse.jdt.core.JavaModelException;
43
import org.eclipse.jdt.core.dom.ASTNode;
44
import org.eclipse.jdt.core.dom.ASTNode;
44
import org.eclipse.jdt.core.dom.CompilationUnit;
45
import org.eclipse.jdt.core.dom.CompilationUnit;
45
import org.eclipse.jdt.core.dom.Expression;
46
import org.eclipse.jdt.core.dom.Expression;
Lines 50-55 Link Here
50
import org.eclipse.jdt.core.dom.SimpleName;
51
import org.eclipse.jdt.core.dom.SimpleName;
51
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
52
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
52
import org.eclipse.jdt.core.search.IJavaSearchConstants;
53
import org.eclipse.jdt.core.search.IJavaSearchConstants;
54
import org.eclipse.jdt.core.search.IJavaSearchScope;
53
import org.eclipse.jdt.core.search.SearchEngine;
55
import org.eclipse.jdt.core.search.SearchEngine;
54
import org.eclipse.jdt.core.search.SearchMatch;
56
import org.eclipse.jdt.core.search.SearchMatch;
55
import org.eclipse.jdt.core.search.SearchParticipant;
57
import org.eclipse.jdt.core.search.SearchParticipant;
Lines 59-64 Link Here
59
import org.eclipse.jdt.internal.corext.dom.Bindings;
61
import org.eclipse.jdt.internal.corext.dom.Bindings;
60
import org.eclipse.jdt.internal.corext.util.JdtFlags;
62
import org.eclipse.jdt.internal.corext.util.JdtFlags;
61
import org.eclipse.jdt.internal.corext.util.Messages;
63
import org.eclipse.jdt.internal.corext.util.Messages;
64
import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
62
65
63
import org.eclipse.jdt.ui.JavaElementLabels;
66
import org.eclipse.jdt.ui.JavaElementLabels;
64
import org.eclipse.jdt.ui.SharedASTProvider;
67
import org.eclipse.jdt.ui.SharedASTProvider;
Lines 74-82 Link Here
74
 */
77
 */
75
public class JavaElementImplementationHyperlink implements IHyperlink {
78
public class JavaElementImplementationHyperlink implements IHyperlink {
76
	
79
	
80
	/**
81
	 * Indicates whether the search scope needs to be restricted to sub types in the hierarchy.
82
	 * 
83
	 * @since 3.6
84
	 */
85
	private static boolean fCreateSubtypeHierarchyScope;
77
	private final IRegion fRegion;
86
	private final IRegion fRegion;
78
	private final SelectionDispatchAction fOpenAction;
87
	private final SelectionDispatchAction fOpenAction;
79
	private final IJavaElement fElement;
88
	private final IMethod fMethod;
80
	private final boolean fQualify;
89
	private final boolean fQualify;
81
90
82
	/**
91
	/**
Lines 89-109 Link Here
89
	 * 
98
	 * 
90
	 * @param region the region of the link
99
	 * @param region the region of the link
91
	 * @param openAction the action to use to open the java elements
100
	 * @param openAction the action to use to open the java elements
92
	 * @param element the java element to open
101
	 * @param method the method to open
93
	 * @param qualify <code>true</code> if the hyperlink text should show a qualified name for
102
	 * @param qualify <code>true</code> if the hyperlink text should show a qualified name for
94
	 *            element.
103
	 *            element.
95
	 * @param editor the active java editor
104
	 * @param editor the active java editor
96
	 */
105
	 */
97
	public JavaElementImplementationHyperlink(IRegion region, SelectionDispatchAction openAction, IJavaElement element, boolean qualify, ITextEditor editor) {
106
	public JavaElementImplementationHyperlink(IRegion region, SelectionDispatchAction openAction, IMethod method, boolean qualify, ITextEditor editor) {
98
		Assert.isNotNull(openAction);
107
		Assert.isNotNull(openAction);
99
		Assert.isNotNull(region);
108
		Assert.isNotNull(region);
100
		Assert.isNotNull(element);
109
		Assert.isNotNull(method);
101
110
102
		fRegion= region;
111
		fRegion= region;
103
		fOpenAction= openAction;
112
		fOpenAction= openAction;
104
		fElement= element;
113
		fMethod= method;
105
		fQualify= qualify;
114
		fQualify= qualify;
106
		fEditor= editor;
115
		fEditor= editor;
116
		fCreateSubtypeHierarchyScope= true;
107
	}
117
	}
108
118
109
	/*
119
	/*
Lines 118-125 Link Here
118
	 */
128
	 */
119
	public String getHyperlinkText() {
129
	public String getHyperlinkText() {
120
		if (fQualify) {
130
		if (fQualify) {
121
			String elementLabel= JavaElementLabels.getElementLabel(fElement, JavaElementLabels.ALL_FULLY_QUALIFIED);
131
			String methodLabel= JavaElementLabels.getElementLabel(fMethod, JavaElementLabels.ALL_FULLY_QUALIFIED);
122
			return Messages.format(JavaEditorMessages.JavaElementImplementationHyperlink_hyperlinkText_qualified, new Object[] { elementLabel });
132
			return Messages.format(JavaEditorMessages.JavaElementImplementationHyperlink_hyperlinkText_qualified, new Object[] { methodLabel });
123
		} else {
133
		} else {
124
			return JavaEditorMessages.JavaElementImplementationHyperlink_hyperlinkText;
134
			return JavaEditorMessages.JavaElementImplementationHyperlink_hyperlinkText;
125
		}
135
		}
Lines 140-162 Link Here
140
	 * </p>
150
	 * </p>
141
	 */
151
	 */
142
	public void open() {
152
	public void open() {
143
		openImplementations(fEditor, fRegion, fElement, fOpenAction);
153
		openImplementations(fEditor, fRegion, fMethod, fOpenAction);
144
	}
154
	}
145
155
146
	/**
156
	/**
147
	 * Finds the implementations for the method.
157
	 * Finds the implementations for the method.
148
	 * <p>
158
	 * <p>
149
	 * If there's only one implementor that java element is opened in the editor, otherwise the
159
	 * If there's only one implementor that method is opened in the editor, otherwise the Quick
150
	 * Quick Hierarchy is opened.
160
	 * Hierarchy is opened.
151
	 * </p>
161
	 * </p>
152
	 * 
162
	 * 
153
	 * @param openAction the action to use to open the java elements
163
	 * @param openAction the action to use to open the methods
154
	 * @param javaElement the java element
164
	 * @param method the method
155
	 * @param region the region of the selection
165
	 * @param region the region of the selection
156
	 * @param editor the active java editor
166
	 * @param editor the active java editor
157
	 * @since 3.6
167
	 * @since 3.6
158
	 */
168
	 */
159
	public static void openImplementations(IEditorPart editor, IRegion region, final IJavaElement javaElement, SelectionDispatchAction openAction) {
169
	public static void openImplementations(IEditorPart editor, IRegion region, final IMethod method, SelectionDispatchAction openAction) {
160
		ITypeRoot editorInput= EditorUtility.getEditorInputJavaElement(editor, false);
170
		ITypeRoot editorInput= EditorUtility.getEditorInputJavaElement(editor, false);
161
171
162
		CompilationUnit ast= SharedASTProvider.getAST(editorInput, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
172
		CompilationUnit ast= SharedASTProvider.getAST(editorInput, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
Lines 178-191 Link Here
178
				}
188
				}
179
			} else if (parent instanceof SuperMethodInvocation) {
189
			} else if (parent instanceof SuperMethodInvocation) {
180
				// Directly go to the super method definition
190
				// Directly go to the super method definition
181
				openAction.run(new StructuredSelection(javaElement));
191
				openAction.run(new StructuredSelection(method));
182
				return;
192
				return;
183
			} else if (parent instanceof MethodDeclaration) {
193
			} else if (parent instanceof MethodDeclaration) {
184
				parentTypeBinding= Bindings.getBindingOfParentType(node);
194
				parentTypeBinding= Bindings.getBindingOfParentType(node);
185
			}
195
			}
186
		}
196
		}
187
		final IType type= parentTypeBinding != null ? (IType) parentTypeBinding.getJavaElement() : null;
197
		final IType receiverType= parentTypeBinding != null ? (IType)parentTypeBinding.getJavaElement() : null;
188
		if (type == null) {
198
		if (receiverType == null) {
189
			openQuickHierarchy(editor);
199
			openQuickHierarchy(editor);
190
			return;
200
			return;
191
		}
201
		}
Lines 199-226 Link Here
199
					monitor= new NullProgressMonitor();
209
					monitor= new NullProgressMonitor();
200
				}
210
				}
201
				try {
211
				try {
202
					String methodLabel= JavaElementLabels.getElementLabel(javaElement, JavaElementLabels.DEFAULT_QUALIFIED);
212
					String methodLabel= JavaElementLabels.getElementLabel(method, JavaElementLabels.DEFAULT_QUALIFIED);
203
					monitor.beginTask(Messages.format(JavaEditorMessages.JavaElementImplementationHyperlink_search_method_implementors, methodLabel), 100);
213
					monitor.beginTask(Messages.format(JavaEditorMessages.JavaElementImplementationHyperlink_search_method_implementors, methodLabel), 10);
214
					ITypeHierarchy superTypeHierarchy= receiverType.newSupertypeHierarchy(new SubProgressMonitor(monitor, 3));
215
					IType type;
216
					type= findTypeDefiningSearchScope(superTypeHierarchy, method, receiverType, links);
217
204
					SearchRequestor requestor= new SearchRequestor() {
218
					SearchRequestor requestor= new SearchRequestor() {
205
						public void acceptSearchMatch(SearchMatch match) throws CoreException {
219
						public void acceptSearchMatch(SearchMatch match) throws CoreException {
206
							if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
220
							if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
207
								IJavaElement element= (IJavaElement)match.getElement();
221
								Object element= match.getElement();
208
								if (element instanceof IMethod && !JdtFlags.isAbstract((IMethod)element)) {
222
								if (element instanceof IMethod) {
209
									links.add(element);
223
									IMethod methodFound= (IMethod)element;
210
									if (links.size() > 1) {
224
									if (!JdtFlags.isAbstract(methodFound) && !links.contains(methodFound)) {
211
										throw new OperationCanceledException(dummyString);
225
										links.add(element);
226
										if (links.size() > 1) {
227
											throw new OperationCanceledException(dummyString);
228
										}
212
									}
229
									}
213
								}
230
								}
214
							}
231
							}
215
						}
232
						}
216
					};
233
					};
217
					int limitTo= IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
234
					int limitTo= IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
218
					SearchPattern pattern= SearchPattern.createPattern(javaElement, limitTo);
235
					SearchPattern pattern= SearchPattern.createPattern(method, limitTo);
219
					Assert.isNotNull(pattern);
236
					Assert.isNotNull(pattern);
220
					SearchParticipant[] participants= new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
237
					SearchParticipant[] participants= new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
221
					SearchEngine engine= new SearchEngine();
238
					SearchEngine engine= new SearchEngine();
222
					engine.search(pattern, participants, SearchEngine.createHierarchyScope(type), requestor, new SubProgressMonitor(monitor, 100));
239
					IJavaSearchScope hierarchyScope;
223
240
					if (fCreateSubtypeHierarchyScope) {
241
						hierarchyScope= SearchEngine.createHierarchyScope(null, type, true, false, null);
242
					} else {
243
						hierarchyScope= SearchEngine.createHierarchyScope(type);
244
					}
245
					engine.search(pattern, participants, hierarchyScope, requestor, new SubProgressMonitor(monitor, 7));
224
					if (monitor.isCanceled()) {
246
					if (monitor.isCanceled()) {
225
						throw new OperationCanceledException();
247
						throw new OperationCanceledException();
226
					}
248
					}
Lines 237-243 Link Here
237
			context.run(true, true, runnable);
259
			context.run(true, true, runnable);
238
		} catch (InvocationTargetException e) {
260
		} catch (InvocationTargetException e) {
239
			IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK,
261
			IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK,
240
					Messages.format(JavaEditorMessages.JavaElementImplementationHyperlink_error_status_message, javaElement.getElementName()), e.getCause());
262
					Messages.format(JavaEditorMessages.JavaElementImplementationHyperlink_error_status_message, method.getElementName()), e.getCause());
241
			JavaPlugin.log(status);
263
			JavaPlugin.log(status);
242
			ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
264
			ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
243
					JavaEditorMessages.JavaElementImplementationHyperlink_hyperlinkText,
265
					JavaEditorMessages.JavaElementImplementationHyperlink_hyperlinkText,
Lines 256-261 Link Here
256
	}
278
	}
257
279
258
	/**
280
	/**
281
	 * Finds the type which defines the scope to search for implementors. Returns the receiver type
282
	 * if its a class, else return the declaring type of the method.
283
	 * 
284
	 * @param superTypeHierarchy the type hierarchy
285
	 * @param method the method
286
	 * @param receiverType the receiver type
287
	 * @param links the list of implementors
288
	 * @return the receiver type if its a class, else the declaring type of the method.
289
	 * @throws JavaModelException if the java element does not exist or if an exception occurs while
290
	 *             accessing its corresponding resource
291
	 * @since 3.6
292
	 */
293
	private static IType findTypeDefiningSearchScope(ITypeHierarchy superTypeHierarchy, IMethod method, IType receiverType, ArrayList links) throws JavaModelException {
294
		if (receiverType.isClass()) {
295
			MethodOverrideTester methodOverrideTester= new MethodOverrideTester(receiverType, superTypeHierarchy);
296
			IMethod methodInReceiver= methodOverrideTester.findOverriddenMethodInType(receiverType, method);
297
			if (methodInReceiver == null || JdtFlags.isAbstract(methodInReceiver)) {
298
				fCreateSubtypeHierarchyScope= false;
299
			} else {
300
				links.add(methodInReceiver);
301
			}
302
			return receiverType;
303
		}
304
		return method.getDeclaringType();
305
	}
306
307
	/**
259
	 * Opens the quick type hierarchy for the given editor.
308
	 * Opens the quick type hierarchy for the given editor.
260
	 *
309
	 *
261
	 * @param editor the editor for which to open the quick hierarchy
310
	 * @param editor the editor for which to open the quick hierarchy
(-)ui/org/eclipse/jdt/ui/actions/OpenImplementationAction.java (-1 / +1 lines)
Lines 125-131 Link Here
125
			return;
125
			return;
126
126
127
		IRegion region= new Region(selection.getOffset(), 0);
127
		IRegion region= new Region(selection.getOffset(), 0);
128
		JavaElementImplementationHyperlink.openImplementations(fEditor, region, element, openAction);
128
		JavaElementImplementationHyperlink.openImplementations(fEditor, region, (IMethod)element, openAction);
129
129
130
	}
130
	}
131
131

Return to bug 288464