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
.getters
;
18 import com
.intellij
.codeInsight
.ExpectedTypeInfo
;
19 import com
.intellij
.codeInsight
.ExpectedTypesProvider
;
20 import com
.intellij
.codeInsight
.completion
.CompletionContext
;
21 import com
.intellij
.psi
.PsiElement
;
22 import com
.intellij
.psi
.PsiExpression
;
23 import com
.intellij
.psi
.PsiType
;
24 import com
.intellij
.psi
.filters
.ContextGetter
;
25 import com
.intellij
.psi
.util
.PsiTreeUtil
;
26 import gnu
.trove
.THashSet
;
27 import org
.jetbrains
.annotations
.NotNull
;
32 * Created by IntelliJ IDEA.
36 * To change this template use Options | File Templates.
38 public class ExpectedTypesGetter
implements ContextGetter
{
41 public PsiType
[] get(PsiElement context
, CompletionContext completionContext
){
42 return getExpectedTypes(context
, false);
45 public static PsiType
[] getExpectedTypes(final PsiElement context
, boolean defaultTypes
) {
46 ExpectedTypesProvider typesProvider
= ExpectedTypesProvider
.getInstance(context
.getProject());
47 PsiExpression expression
= PsiTreeUtil
.getContextOfType(context
, PsiExpression
.class, true);
48 if(expression
== null) return PsiType
.EMPTY_ARRAY
;
50 ExpectedTypeInfo
[] infos
= typesProvider
.getExpectedTypes(expression
, true);
52 Set
<PsiType
> result
= new THashSet
<PsiType
>(infos
.length
);
53 for (ExpectedTypeInfo info
: infos
) {
54 final PsiType type
= info
.getType();
55 final PsiType defaultType
= info
.getDefaultType();
56 if (!defaultTypes
&& !defaultType
.equals(type
)) {
59 result
.add(defaultType
);
61 return result
.toArray(new PsiType
[result
.size()]);