summaryrefslogtreecommitdiff
path: root/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionStatistician.java
blob: 4a42e11903be25112f1f65aeec10c2a741792d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.codeInsight.completion;

import com.intellij.codeInsight.ExpectedTypeInfo;
import com.intellij.codeInsight.ExpectedTypeInfoImpl;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupItem;
import com.intellij.psi.*;
import com.intellij.psi.statistics.JavaStatisticsManager;
import com.intellij.psi.statistics.StatisticsInfo;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.util.containers.ContainerUtil;

import java.util.List;

/**
 * @author peter
 */
public class JavaCompletionStatistician extends CompletionStatistician{

  @Override
  public StatisticsInfo serialize(final LookupElement element, final CompletionLocation location) {
    Object o = element.getObject();

    if (o instanceof PsiLocalVariable || o instanceof PsiParameter || o instanceof PsiThisExpression || o instanceof PsiKeyword) {
      return StatisticsInfo.EMPTY;
    }

    LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY);
    if (item == null) return null;

    PsiType qualifierType = JavaCompletionUtil.getQualifierType(item);

    if (o instanceof PsiMember) {
      final ExpectedTypeInfo[] infos = JavaCompletionUtil.EXPECTED_TYPES.getValue(location);
      final ExpectedTypeInfo firstInfo = infos != null && infos.length > 0 ? infos[0] : null;
      String key2 = JavaStatisticsManager.getMemberUseKey2((PsiMember)o);
      if (o instanceof PsiClass) {
        PsiType expectedType = firstInfo != null ? firstInfo.getDefaultType() : null;
        return new StatisticsInfo(JavaStatisticsManager.getAfterNewKey(expectedType), key2);
      }

      PsiClass containingClass = ((PsiMember)o).getContainingClass();
      if (containingClass != null) {
        String expectedName = firstInfo instanceof ExpectedTypeInfoImpl ? ((ExpectedTypeInfoImpl)firstInfo).expectedName.compute() : null;
        String contextPrefix = expectedName == null ? "" : "expectedName=" + expectedName + "###";
        String context = contextPrefix + JavaStatisticsManager.getMemberUseKey2(containingClass);

        if (o instanceof PsiMethod) {
          String memberValue = JavaStatisticsManager.getMemberUseKey2(RecursionWeigher.findDeepestSuper((PsiMethod)o));

          List<StatisticsInfo> superMethodInfos = ContainerUtil.newArrayList(new StatisticsInfo(contextPrefix + context, memberValue));
          for (PsiClass superClass : InheritanceUtil.getSuperClasses(containingClass)) {
            superMethodInfos.add(new StatisticsInfo(contextPrefix + JavaStatisticsManager.getMemberUseKey2(superClass), memberValue));
          }
          return StatisticsInfo.createComposite(superMethodInfos);
        }

        return new StatisticsInfo(context, key2);
      }
    }

    if (qualifierType != null) return StatisticsInfo.EMPTY;

    return null;
  }

}