2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
5 package com
.intellij
.codeInsight
.completion
;
7 import com
.intellij
.codeInsight
.ExpectedTypeInfo
;
8 import com
.intellij
.codeInsight
.generation
.OverrideImplementUtil
;
9 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
10 import com
.intellij
.psi
.*;
11 import com
.intellij
.psi
.statistics
.StatisticsManager
;
12 import com
.intellij
.psi
.util
.PsiTreeUtil
;
13 import com
.intellij
.psi
.util
.TypeConversionUtil
;
18 public class AbstractExpectedTypeSkipper
extends CompletionPreselectSkipper
{
28 public boolean skipElement(LookupElement element
, CompletionLocation location
) {
29 return skips(element
, location
);
32 public static boolean skips(LookupElement element
, CompletionLocation location
) {
33 return getSkippingStatus(element
, location
) != Result
.ACCEPT
;
36 private static Result
getSkippingStatus(final LookupElement item
, final CompletionLocation location
) {
37 if (location
.getCompletionType() != CompletionType
.SMART
) return Result
.ACCEPT
;
39 final PsiExpression expression
= PsiTreeUtil
.getParentOfType(location
.getCompletionParameters().getPosition(), PsiExpression
.class);
40 if (!(expression
instanceof PsiNewExpression
)) return Result
.ACCEPT
;
42 final Object object
= item
.getObject();
43 if (!(object
instanceof PsiClass
)) return Result
.ACCEPT
;
45 if (StatisticsManager
.getInstance().getUseCount(CompletionService
.STATISTICS_KEY
, item
, location
) > 1) return Result
.ACCEPT
;
47 PsiClass psiClass
= (PsiClass
)object
;
50 for (final PsiMethod method
: psiClass
.getMethods()) {
51 if (method
.hasModifierProperty(PsiModifier
.ABSTRACT
)) {
53 if (toImplement
> 2) return Result
.ABSTRACT
;
57 toImplement
+= OverrideImplementUtil
.getMethodSignaturesToImplement(psiClass
).size();
58 if (toImplement
> 2) return Result
.ABSTRACT
;
60 final ExpectedTypeInfo
[] infos
= JavaCompletionUtil
.EXPECTED_TYPES
.getValue(location
);
61 boolean isDefaultType
= false;
63 final PsiType type
= JavaPsiFacade
.getInstance(psiClass
.getProject()).getElementFactory().createType(psiClass
);
64 for (final ExpectedTypeInfo info
: infos
) {
65 final PsiType infoType
= TypeConversionUtil
.erasure(info
.getType().getDeepComponentType());
66 final PsiType defaultType
= TypeConversionUtil
.erasure(info
.getDefaultType().getDeepComponentType());
67 if (!defaultType
.equals(infoType
) && infoType
.isAssignableFrom(type
)) {
68 if (!defaultType
.isAssignableFrom(type
)) return Result
.NON_DEFAULT
;
74 if (toImplement
> 0) return Result
.ACCEPT
;
76 if (psiClass
.hasModifierProperty(PsiModifier
.ABSTRACT
)) return Result
.ABSTRACT
;
77 if (!isDefaultType
&& CommonClassNames
.JAVA_LANG_STRING
.equals(psiClass
.getQualifiedName())) return Result
.STRING
;
78 if (CommonClassNames
.JAVA_LANG_OBJECT
.equals(psiClass
.getQualifiedName())) return Result
.NON_DEFAULT
;