2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.codeInsight
.navigation
;
18 import com
.intellij
.codeInsight
.CodeInsightActionHandler
;
19 import com
.intellij
.codeInsight
.CodeInsightBundle
;
20 import com
.intellij
.openapi
.editor
.Editor
;
21 import com
.intellij
.openapi
.fileEditor
.FileEditorManager
;
22 import com
.intellij
.openapi
.fileEditor
.OpenFileDescriptor
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.psi
.util
.PsiSuperMethodUtil
;
26 import com
.intellij
.psi
.util
.PsiTreeUtil
;
27 import org
.jetbrains
.annotations
.NotNull
;
28 import org
.jetbrains
.annotations
.Nullable
;
30 import java
.util
.ArrayList
;
31 import java
.util
.Arrays
;
32 import java
.util
.Iterator
;
33 import java
.util
.List
;
35 public class JavaGotoSuperHandler
implements CodeInsightActionHandler
{
36 public void invoke(@NotNull final Project project
, @NotNull final Editor editor
, @NotNull final PsiFile file
) {
37 int offset
= editor
.getCaretModel().getOffset();
38 PsiElement
[] superElements
= findSuperElements(file
, offset
);
39 if (superElements
== null || superElements
.length
== 0) return;
40 if (superElements
.length
== 1) {
41 PsiElement superElement
= superElements
[0].getNavigationElement();
42 OpenFileDescriptor descriptor
= new OpenFileDescriptor(project
, superElement
.getContainingFile().getVirtualFile(), superElement
.getTextOffset());
43 FileEditorManager
.getInstance(project
).openTextEditor(descriptor
, true);
45 String title
= superElements
[0] instanceof PsiMethod ?
46 CodeInsightBundle
.message("goto.super.method.chooser.title") :
47 CodeInsightBundle
.message("goto.super.class.chooser.title");
49 NavigationUtil
.getPsiElementPopup(superElements
, title
).showInBestPositionFor(editor
);
54 private static PsiElement
[] findSuperElements(PsiFile file
, int offset
) {
55 PsiElement element
= file
.findElementAt(offset
);
56 if (element
== null) return null;
58 PsiMember e
= PsiTreeUtil
.getParentOfType(element
, PsiMethod
.class, PsiClass
.class);
59 if (e
instanceof PsiClass
) {
60 PsiClass aClass
= (PsiClass
) e
;
61 List
<PsiClass
> allSupers
= new ArrayList
<PsiClass
>(Arrays
.asList(aClass
.getSupers()));
62 for (Iterator
<PsiClass
> iterator
= allSupers
.iterator(); iterator
.hasNext();) {
63 PsiClass superClass
= iterator
.next();
64 if ("java.lang.Object".equals(superClass
.getQualifiedName())) iterator
.remove();
66 return allSupers
.toArray(new PsiClass
[allSupers
.size()]);
67 } else if (e
instanceof PsiMethod
) {
68 PsiMethod method
= (PsiMethod
) e
;
69 if (method
.isConstructor()) {
70 PsiMethod constructorInSuper
= PsiSuperMethodUtil
.findConstructorInSuper(method
);
71 if (constructorInSuper
!= null) {
72 return new PsiElement
[]{constructorInSuper
};
75 return method
.findSuperMethods(false);
81 public boolean startInWriteAction() {