update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / filters / types / AssignableToFilter.java
blob41017d559bb12f69bcf9f11f3e262fee94316ddf
1 /*
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;
28 /**
29 * Created by IntelliJ IDEA.
30 * User: ik
31 * Date: 28.01.2003
32 * Time: 20:53:38
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){
40 myType = 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){
65 return true;
68 public boolean isAcceptable(Object element, PsiElement context){
69 if(myType != null){
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);
87 else return false;
90 public String toString(){
91 if(myType != null)
92 return "assignable-to(" + myType + ")";
93 else if(myFilter != null) return myFilter.toString();
94 return "uninitialized-equals-filter";