1 /*******************************************************************************
2 * Copyright (c) 2010 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.javaeditor;
14 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.hyperlink.IHyperlink;
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.JavaModelException;
24 import org.eclipse.jdt.internal.corext.util.Messages;
25 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
26 import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache;
28 import org.eclipse.jdt.ui.JavaElementLabels;
29 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 * Java element super implementation hyperlink.
38 public class JavaElementSuperImplementationHyperlink implements IHyperlink {
40 private final SelectionDispatchAction fOpenAction;
41 private final IMethod fMethod;
42 private final boolean fQualify;
43 private IRegion fRegion;
46 * Creates a new Java element super implementation hyperlink for methods.
48 * @param region the region of the link
49 * @param openAction the action to use to open the java elements
50 * @param method the method to open
51 * @param qualify <code>true</code> if the hyperlink text should show a qualified name for
54 public JavaElementSuperImplementationHyperlink(IRegion region, SelectionDispatchAction openAction, IMethod method, boolean qualify) {
55 Assert.isNotNull(openAction);
56 Assert.isNotNull(region);
57 Assert.isNotNull(method);
60 fOpenAction= openAction;
66 * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkRegion()
68 public IRegion getHyperlinkRegion() {
73 * @see org.eclipse.jface.text.hyperlink.IHyperlink#getTypeLabel()
75 public String getTypeLabel() {
80 * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkText()
82 public String getHyperlinkText() {
84 String methodLabel= JavaElementLabels.getElementLabel(fMethod, JavaElementLabels.ALL_FULLY_QUALIFIED);
85 return Messages.format(JavaEditorMessages.JavaElementSuperImplementationHyperlink_hyperlinkText_qualified, new Object[] { methodLabel });
87 return JavaEditorMessages.JavaElementSuperImplementationHyperlink_hyperlinkText;
92 * @see org.eclipse.jface.text.hyperlink.IHyperlink#open()
96 IMethod method= findSuperImplementation(fMethod);
98 fOpenAction.run(new StructuredSelection(method));
99 } catch (JavaModelException e) {
106 * Finds the super implementation of the method.
108 * @param method the method
109 * @return the super implementation of the method if any or <code>null</code>
110 * @throws JavaModelException if an exception occurs while creating the type hierarchy to find
111 * the super implementation
113 static IMethod findSuperImplementation(IMethod method) throws JavaModelException {
114 MethodOverrideTester tester= SuperTypeHierarchyCache.getMethodOverrideTester(method.getDeclaringType());
115 return tester.findOverriddenMethod(method, false);