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
.lookup
.LookupElement
;
20 import com
.intellij
.codeInsight
.lookup
.LookupItem
;
21 import com
.intellij
.openapi
.util
.Condition
;
22 import com
.intellij
.openapi
.util
.text
.StringUtil
;
23 import com
.intellij
.psi
.*;
24 import com
.intellij
.psi
.statistics
.JavaStatisticsManager
;
25 import com
.intellij
.psi
.statistics
.StatisticsInfo
;
26 import com
.intellij
.util
.containers
.ContainerUtil
;
27 import org
.jetbrains
.annotations
.NonNls
;
32 public class JavaCompletionStatistician
extends CompletionStatistician
{
33 @NonNls public static final String CLASS_NAME_COMPLETION_PREFIX
= "classNameCompletion#";
35 public StatisticsInfo
serialize(final LookupElement element
, final CompletionLocation location
) {
36 final Object o
= element
.getObject();
38 if (o
instanceof PsiLocalVariable
|| o
instanceof PsiParameter
|| o
instanceof PsiThisExpression
) {
39 return StatisticsInfo
.EMPTY
;
42 final ExpectedTypeInfo
[] infos
= JavaCompletionUtil
.EXPECTED_TYPES
.getValue(location
);
43 if (infos
!= null && infos
.length
> 0) {
44 final boolean primitivesOnly
= ContainerUtil
.and(infos
, new Condition
<ExpectedTypeInfo
>() {
45 public boolean value(final ExpectedTypeInfo info
) {
46 return info
.getType() instanceof PsiPrimitiveType
;
50 return StatisticsInfo
.EMPTY
; //collecting statistics on primitive types usually has only negative effects
54 LookupItem item
= element
.as(LookupItem
.class);
55 if (item
== null) return null;
57 PsiType qualifierType
= JavaCompletionUtil
.getQualifierType(item
);
58 if (qualifierType
== null) {
59 if (infos
!= null && infos
.length
> 0) {
60 qualifierType
= infos
[0].getDefaultType();
64 final CompletionType type
= location
.getCompletionType();
65 if (o
instanceof PsiMember
) {
66 final boolean isClass
= o
instanceof PsiClass
;
67 if (qualifierType
!= null) {
68 if (type
== CompletionType
.SMART
) {
71 context
= JavaStatisticsManager
.getAfterNewKey(qualifierType
);
73 context
= JavaStatisticsManager
.getMemberUseKey1(qualifierType
);
75 return new StatisticsInfo(context
, JavaStatisticsManager
.getMemberUseKey2((PsiMember
)o
));
77 if (!isClass
&& type
== CompletionType
.BASIC
) return JavaStatisticsManager
.createInfo(qualifierType
, (PsiMember
)o
);
78 return StatisticsInfo
.EMPTY
;
81 if (type
== CompletionType
.CLASS_NAME
&& isClass
) {
82 final String qualifiedName
= ((PsiClass
)o
).getQualifiedName();
83 if (qualifiedName
!= null) {
84 final String prefixCapitals
= StringUtil
.capitalsOnly(element
.getPrefixMatcher().getPrefix());
85 return new StatisticsInfo(CLASS_NAME_COMPLETION_PREFIX
+ prefixCapitals
, qualifiedName
);
90 if (qualifierType
!= null) return StatisticsInfo
.EMPTY
;