local variables and recursion checks are more important for relevance than expected...
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / completion / AbstractExpectedTypeSkipper.java
blob17e40e36f3806e9971b94a13ccd303a89665606c
1 /*
2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
4 */
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;
15 /**
16 * @author peter
18 public class AbstractExpectedTypeSkipper extends CompletionPreselectSkipper {
20 private enum Result {
21 NON_DEFAULT,
22 STRING,
23 ABSTRACT,
24 ACCEPT
27 @Override
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;
49 int toImplement = 0;
50 for (final PsiMethod method : psiClass.getMethods()) {
51 if (method.hasModifierProperty(PsiModifier.ABSTRACT)) {
52 toImplement++;
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;
62 if (infos != null) {
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;
69 isDefaultType = true;
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;
80 return Result.ACCEPT;