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
.resolve
;
18 import com
.intellij
.openapi
.util
.Key
;
19 import com
.intellij
.pom
.java
.LanguageLevel
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.filters
.ElementFilter
;
22 import com
.intellij
.psi
.infos
.CandidateInfo
;
23 import com
.intellij
.psi
.scope
.ElementClassFilter
;
24 import com
.intellij
.psi
.scope
.ElementClassHint
;
25 import com
.intellij
.psi
.scope
.JavaScopeProcessorEvent
;
26 import com
.intellij
.psi
.scope
.PsiConflictResolver
;
27 import com
.intellij
.psi
.scope
.conflictResolvers
.JavaVariableConflictResolver
;
28 import com
.intellij
.psi
.scope
.processor
.ConflictFilterProcessor
;
29 import com
.intellij
.psi
.util
.PsiUtil
;
30 import com
.intellij
.util
.SmartList
;
35 public class VariableResolverProcessor
extends ConflictFilterProcessor
implements ElementClassHint
{
36 private static final ElementFilter ourFilter
= ElementClassFilter
.VARIABLE
;
38 private boolean myStaticScopeFlag
= false;
39 private final PsiClass myAccessClass
;
40 private PsiElement myCurrentFileContext
= null;
42 public VariableResolverProcessor(PsiJavaCodeReferenceElement place
) {
43 super(place
.getText(), ourFilter
, new PsiConflictResolver
[]{new JavaVariableConflictResolver()}, new SmartList
<CandidateInfo
>(), place
);
45 PsiElement referenceName
= place
.getReferenceNameElement();
46 if (referenceName
instanceof PsiIdentifier
){
47 setName(referenceName
.getText());
49 PsiClass access
= null;
50 PsiElement qualifier
= place
.getQualifier();
51 if (qualifier
instanceof PsiExpression
) {
52 final JavaResolveResult accessClass
= PsiUtil
.getAccessObjectClass((PsiExpression
)qualifier
);
53 final PsiElement element
= accessClass
.getElement();
54 if (element
instanceof PsiTypeParameter
) {
55 PsiElementFactory factory
= JavaPsiFacade
.getInstance(element
.getProject()).getElementFactory();
56 final PsiClassType type
= factory
.createType((PsiTypeParameter
)element
);
57 final PsiType accessType
= accessClass
.getSubstitutor().substitute(type
);
58 if (accessType
instanceof PsiArrayType
) {
59 LanguageLevel languageLevel
= PsiUtil
.getLanguageLevel(qualifier
);
60 access
= factory
.getArrayClass(languageLevel
);
62 else if (accessType
instanceof PsiClassType
) access
= ((PsiClassType
)accessType
).resolve();
64 else if (element
instanceof PsiClass
) access
= (PsiClass
)element
;
66 myAccessClass
= access
;
69 public final void handleEvent(Event event
, Object associated
) {
70 super.handleEvent(event
, associated
);
71 if(event
== JavaScopeProcessorEvent
.START_STATIC
){
72 myStaticScopeFlag
= true;
74 else if (JavaScopeProcessorEvent
.SET_CURRENT_FILE_CONTEXT
.equals(event
)) {
75 myCurrentFileContext
= (PsiElement
)associated
;
79 public void add(PsiElement element
, PsiSubstitutor substitutor
) {
80 final boolean staticProblem
= myStaticScopeFlag
&& !((PsiModifierListOwner
)element
).hasModifierProperty(PsiModifier
.STATIC
);
81 add(new CandidateInfo(element
, substitutor
, myPlace
, myAccessClass
, staticProblem
, myCurrentFileContext
));
85 public boolean shouldProcess(DeclaractionKind kind
) {
86 return kind
== DeclaractionKind
.VARIABLE
|| kind
== DeclaractionKind
.FIELD
|| kind
== DeclaractionKind
.ENUM_CONST
;
89 public boolean execute(PsiElement element
, ResolveState state
) {
90 if (!(element
instanceof PsiField
) && (myName
== null || PsiUtil
.checkName(element
, myName
, myPlace
))) {
91 super.execute(element
, state
);
92 return myResults
.isEmpty();
95 return super.execute(element
, state
);
99 public <T
> T
getHint(Key
<T
> hintKey
) {
100 if (hintKey
== ElementClassHint
.KEY
) {
104 return super.getHint(hintKey
);