update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / completion / CollectionsUtilityMethodsProvider.java
blob1d323e3f6a6b4ca71c7d4d694a8345bce5dccae9
1 /*
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 org.jetbrains.annotations.NotNull;
19 import org.jetbrains.annotations.NonNls;
20 import com.intellij.util.ProcessingContext;
21 import com.intellij.psi.*;
22 import static com.intellij.psi.CommonClassNames.*;
23 import com.intellij.codeInsight.lookup.AutoCompletionPolicy;
25 /**
26 * @author peter
28 class CollectionsUtilityMethodsProvider extends CompletionProvider<JavaSmartCompletionParameters> {
29 public void addCompletions(@NotNull final JavaSmartCompletionParameters parameters, final ProcessingContext context, @NotNull final CompletionResultSet result) {
30 final PsiElement element = parameters.getPosition();
32 final PsiElement parent = element.getParent();
33 if (parent instanceof PsiReferenceExpression && ((PsiReferenceExpression)parent).getQualifierExpression() != null) return;
35 final PsiClass collectionsClass =
36 JavaPsiFacade.getInstance(element.getProject()).findClass(JAVA_UTIL_COLLECTIONS, element.getResolveScope());
37 if (collectionsClass == null) return;
39 final PsiType type = parameters.getExpectedType();
40 final PsiType defaultType = parameters.getDefaultType();
41 final PsiElement pparent = parent.getParent();
42 if (parameters.getInvocationCount() > 1 ||
43 pparent instanceof PsiReturnStatement ||
44 pparent instanceof PsiConditionalExpression && pparent.getParent() instanceof PsiReturnStatement) {
45 addCollectionMethod(result, type, defaultType, JAVA_UTIL_LIST, "emptyList", collectionsClass);
46 addCollectionMethod(result, type, defaultType, JAVA_UTIL_SET, "emptySet", collectionsClass);
47 addCollectionMethod(result, type, defaultType, JAVA_UTIL_MAP, "emptyMap", collectionsClass);
50 if (parameters.getInvocationCount() > 1) {
51 addCollectionMethod(result, type, defaultType, JAVA_UTIL_LIST, "singletonList", collectionsClass);
52 addCollectionMethod(result, type, defaultType, JAVA_UTIL_SET, "singleton", collectionsClass);
53 addCollectionMethod(result, type, defaultType, JAVA_UTIL_MAP, "singletonMap", collectionsClass);
55 addCollectionMethod(result, type, defaultType, JAVA_UTIL_COLLECTION, "unmodifiableCollection", collectionsClass);
56 addCollectionMethod(result, type, defaultType, JAVA_UTIL_LIST, "unmodifiableList", collectionsClass);
57 addCollectionMethod(result, type, defaultType, JAVA_UTIL_SET, "unmodifiableSet", collectionsClass);
58 addCollectionMethod(result, type, defaultType, JAVA_UTIL_MAP, "unmodifiableMap", collectionsClass);
59 addCollectionMethod(result, type, defaultType, "java.util.SortedSet", "unmodifiableSortedSet", collectionsClass);
60 addCollectionMethod(result, type, defaultType, "java.util.SortedMap", "unmodifiableSortedMap", collectionsClass);
65 private static void addCollectionMethod(final CompletionResultSet result, final PsiType expectedType,
66 final PsiType defaultType, final String baseClassName,
67 @NonNls final String method, @NotNull final PsiClass collectionsClass) {
68 if (isClassType(expectedType, baseClassName) || isClassType(expectedType, JAVA_UTIL_COLLECTION) ||
69 isClassType(defaultType, baseClassName) || isClassType(defaultType, JAVA_UTIL_COLLECTION)) {
70 final PsiMethod[] methods = collectionsClass.findMethodsByName(method, false);
71 if (methods.length != 0) {
72 result.addElement(JavaCompletionUtil.qualify(new JavaMethodCallElement(methods[0]).setAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE)));
77 private static boolean isClassType(final PsiType type, final String className) {
78 if (type instanceof PsiClassType) {
79 final PsiClass psiClass = ((PsiClassType)type).resolve();
80 return psiClass != null && className.equals(psiClass.getQualifiedName());
82 return false;