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
.filters
.types
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.psi
.filters
.ElementFilter
;
20 import com
.intellij
.psi
.filters
.FilterUtil
;
21 import com
.intellij
.psi
.filters
.InitializableFilter
;
22 import com
.intellij
.psi
.filters
.OrFilter
;
23 import com
.intellij
.psi
.infos
.CandidateInfo
;
25 import java
.util
.ArrayList
;
26 import java
.util
.List
;
29 * Created by IntelliJ IDEA.
33 * To change this template use Options | File Templates.
35 public class AssignableToFilter
implements InitializableFilter
{
36 private PsiType myType
= null;
37 private ElementFilter myFilter
= null;
39 public AssignableToFilter(PsiType type
){
43 public AssignableToFilter(){}
45 public void init(Object
[] type
){
46 myFilter
= new OrFilter();
47 final List
<ElementFilter
> filters
= new ArrayList
<ElementFilter
>();
48 for (final Object o
: type
) {
49 PsiType currentType
= null;
50 if (o
instanceof PsiType
) {
51 currentType
= (PsiType
)o
;
53 else if (o
instanceof PsiClass
) {
54 final PsiClass psiClass
= (PsiClass
)o
;
55 currentType
= JavaPsiFacade
.getInstance(psiClass
.getProject()).getElementFactory().createType(psiClass
);
57 if (currentType
!= null) {
58 filters
.add(new AssignableToFilter(currentType
));
61 myFilter
= new OrFilter(filters
.toArray(new ElementFilter
[filters
.size()]));
64 public boolean isClassAcceptable(Class hintClass
){
68 public boolean isAcceptable(Object element
, PsiElement context
){
70 if(element
== null) return false;
71 if (element
instanceof PsiType
) return myType
.isAssignableFrom((PsiType
) element
);
72 PsiSubstitutor substitutor
= null;
73 if(element
instanceof CandidateInfo
){
74 final CandidateInfo info
= (CandidateInfo
)element
;
75 substitutor
= info
.getSubstitutor();
76 element
= info
.getElement();
79 PsiType typeByElement
= FilterUtil
.getTypeByElement((PsiElement
)element
, context
);
80 if(substitutor
!= null) typeByElement
= substitutor
.substitute(typeByElement
);
81 return typeByElement
!= null && typeByElement
.isAssignableFrom(myType
) && !typeByElement
.equals(myType
);
83 else if(myFilter
!= null){
84 if(element
== null) return false;
85 return myFilter
.isAcceptable(element
, context
);
90 public String
toString(){
92 return "assignable-to(" + myType
+ ")";
93 else if(myFilter
!= null) return myFilter
.toString();
94 return "uninitialized-equals-filter";