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
.psi
.impl
.source
.tree
.java
;
18 import com
.intellij
.lang
.ASTNode
;
19 import com
.intellij
.openapi
.diagnostic
.Logger
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.impl
.source
.Constants
;
22 import com
.intellij
.psi
.impl
.source
.tree
.ChildRole
;
23 import com
.intellij
.psi
.tree
.IElementType
;
24 import com
.intellij
.psi
.tree
.ChildRoleBase
;
25 import org
.jetbrains
.annotations
.NotNull
;
27 public class PsiSuperExpressionImpl
extends ExpressionPsiElement
implements PsiSuperExpression
, Constants
{
28 private static final Logger LOG
= Logger
.getInstance("#com.intellij.psi.impl.source.tree.java.PsiSuperExpressionImpl");
30 public PsiSuperExpressionImpl() {
31 super(SUPER_EXPRESSION
);
34 public PsiJavaCodeReferenceElement
getQualifier() {
35 return (PsiJavaCodeReferenceElement
)findChildByRoleAsPsiElement(ChildRole
.QUALIFIER
);
38 public PsiType
getType() {
39 PsiJavaCodeReferenceElement qualifier
= getQualifier();
40 if (qualifier
!= null){
41 PsiClass aClass
= (PsiClass
)qualifier
.resolve();
42 if (aClass
== null) return null;
43 return getSuperType(aClass
);
45 for(PsiElement scope
= getContext(); scope
!= null; scope
= scope
.getContext()){
46 if (scope
instanceof PsiClass
){
47 PsiClass aClass
= (PsiClass
)scope
;
48 return getSuperType(aClass
);
50 if (scope
instanceof PsiExpressionList
&& scope
.getParent() instanceof PsiAnonymousClass
){
51 scope
= scope
.getParent();
53 else if (scope
instanceof JavaCodeFragment
) {
54 PsiType fragmentSuperType
= ((JavaCodeFragment
)scope
).getSuperType();
55 if (fragmentSuperType
!= null) return fragmentSuperType
;
61 private PsiType
getSuperType(PsiClass aClass
) {
62 if (aClass
.isInterface()) {
63 JavaPsiFacade facade
= JavaPsiFacade
.getInstance(getProject());
64 return facade
.getElementFactory().createType(facade
.findClass("java.lang.Object", getResolveScope()));
67 if (aClass
instanceof PsiAnonymousClass
) {
68 final PsiClassType baseClassType
= ((PsiAnonymousClass
)aClass
).getBaseClassType();
69 final PsiClass psiClass
= baseClassType
.resolve();
70 if(psiClass
!= null && !psiClass
.isInterface()){
74 return PsiType
.getJavaLangObject(getManager(), getResolveScope());
77 if ("java.lang.Object".equals(aClass
.getQualifiedName())) return null;
78 PsiClassType
[] superTypes
= aClass
.getExtendsListTypes();
79 if (superTypes
.length
== 0) {
80 JavaPsiFacade facade
= JavaPsiFacade
.getInstance(getProject());
81 final PsiClass javaLangObject
= facade
.findClass("java.lang.Object", getResolveScope());
82 if (javaLangObject
!= null) {
83 return facade
.getElementFactory().createType(javaLangObject
);
93 public ASTNode
findChildByRole(int role
) {
94 LOG
.assertTrue(ChildRole
.isUnique(role
));
99 case ChildRole
.QUALIFIER
:
100 if (getFirstChildNode().getElementType() == JAVA_CODE_REFERENCE
){
101 return getFirstChildNode();
108 return findChildByType(DOT
);
110 case ChildRole
.SUPER_KEYWORD
:
111 return getLastChildNode();
115 public int getChildRole(ASTNode child
) {
116 LOG
.assertTrue(child
.getTreeParent() == this);
117 IElementType i
= child
.getElementType();
118 if (i
== JAVA_CODE_REFERENCE
) {
119 return ChildRole
.QUALIFIER
;
122 return ChildRole
.DOT
;
124 else if (i
== SUPER_KEYWORD
) {
125 return ChildRole
.SUPER_KEYWORD
;
128 return ChildRoleBase
.NONE
;
132 public void accept(@NotNull PsiElementVisitor visitor
) {
133 if (visitor
instanceof JavaElementVisitor
) {
134 ((JavaElementVisitor
)visitor
).visitSuperExpression(this);
137 visitor
.visitElement(this);
141 public String
toString() {
142 return "PsiSuperExpression:" + getText();