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
.codeInsight
.completion
;
18 import com
.intellij
.codeInsight
.ExpectedTypeInfo
;
19 import com
.intellij
.codeInsight
.generation
.OverrideImplementUtil
;
20 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
21 import com
.intellij
.psi
.*;
22 import com
.intellij
.psi
.statistics
.StatisticsManager
;
23 import com
.intellij
.psi
.util
.PsiTreeUtil
;
24 import com
.intellij
.psi
.util
.TypeConversionUtil
;
29 public class AbstractExpectedTypeSkipper
extends CompletionPreselectSkipper
{
39 public boolean skipElement(LookupElement element
, CompletionLocation location
) {
40 return skips(element
, location
);
43 public static boolean skips(LookupElement element
, CompletionLocation location
) {
44 return getSkippingStatus(element
, location
) != Result
.ACCEPT
;
47 private static Result
getSkippingStatus(final LookupElement item
, final CompletionLocation location
) {
48 if (location
.getCompletionType() != CompletionType
.SMART
) return Result
.ACCEPT
;
50 final PsiExpression expression
= PsiTreeUtil
.getParentOfType(location
.getCompletionParameters().getPosition(), PsiExpression
.class);
51 if (!(expression
instanceof PsiNewExpression
)) return Result
.ACCEPT
;
53 final Object object
= item
.getObject();
54 if (!(object
instanceof PsiClass
)) return Result
.ACCEPT
;
56 if (StatisticsManager
.getInstance().getUseCount(CompletionService
.STATISTICS_KEY
, item
, location
) > 1) return Result
.ACCEPT
;
58 PsiClass psiClass
= (PsiClass
)object
;
61 for (final PsiMethod method
: psiClass
.getMethods()) {
62 if (method
.hasModifierProperty(PsiModifier
.ABSTRACT
)) {
64 if (toImplement
> 2) return Result
.ABSTRACT
;
68 toImplement
+= OverrideImplementUtil
.getMethodSignaturesToImplement(psiClass
).size();
69 if (toImplement
> 2) return Result
.ABSTRACT
;
71 final ExpectedTypeInfo
[] infos
= JavaCompletionUtil
.EXPECTED_TYPES
.getValue(location
);
72 boolean isDefaultType
= false;
74 final PsiType type
= JavaPsiFacade
.getInstance(psiClass
.getProject()).getElementFactory().createType(psiClass
);
75 for (final ExpectedTypeInfo info
: infos
) {
76 final PsiType infoType
= TypeConversionUtil
.erasure(info
.getType().getDeepComponentType());
77 final PsiType defaultType
= TypeConversionUtil
.erasure(info
.getDefaultType().getDeepComponentType());
78 if (!defaultType
.equals(infoType
) && infoType
.isAssignableFrom(type
)) {
79 if (!defaultType
.isAssignableFrom(type
)) return Result
.NON_DEFAULT
;
85 if (toImplement
> 0) return Result
.ACCEPT
;
87 if (psiClass
.hasModifierProperty(PsiModifier
.ABSTRACT
)) return Result
.ABSTRACT
;
88 if (!isDefaultType
&& CommonClassNames
.JAVA_LANG_STRING
.equals(psiClass
.getQualifiedName())) return Result
.STRING
;
89 if (CommonClassNames
.JAVA_LANG_OBJECT
.equals(psiClass
.getQualifiedName())) return Result
.NON_DEFAULT
;