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
.daemon
.impl
.quickfix
;
18 import com
.intellij
.codeInsight
.daemon
.impl
.HighlightInfo
;
19 import com
.intellij
.codeInsight
.daemon
.impl
.analysis
.GenericsHighlightUtil
;
20 import com
.intellij
.codeInsight
.daemon
.impl
.analysis
.HighlightUtil
;
21 import com
.intellij
.psi
.*;
22 import com
.intellij
.psi
.search
.GlobalSearchScope
;
23 import com
.intellij
.psi
.search
.PsiShortNamesCache
;
24 import com
.intellij
.openapi
.project
.DumbService
;
26 import java
.util
.HashMap
;
28 public class VariableParameterizedTypeFix
{
29 public static void registerIntentions(HighlightInfo highlightInfo
, PsiVariable variable
, PsiReferenceParameterList parameterList
) {
30 PsiType type
= variable
.getType();
31 if (!(type
instanceof PsiClassType
)) return;
33 if (DumbService
.getInstance(variable
.getProject()).isDumb()) return;
35 String shortName
= ((PsiClassType
)type
).getClassName();
36 PsiManager manager
= parameterList
.getManager();
37 final JavaPsiFacade facade
= JavaPsiFacade
.getInstance(manager
.getProject());
38 PsiShortNamesCache shortNamesCache
= facade
.getShortNamesCache();
39 PsiClass
[] classes
= shortNamesCache
.getClassesByName(shortName
, GlobalSearchScope
.allScope(manager
.getProject()));
40 PsiElementFactory factory
= facade
.getElementFactory();
41 for (PsiClass aClass
: classes
) {
42 if (GenericsHighlightUtil
.checkReferenceTypeArgumentList(aClass
, parameterList
, PsiSubstitutor
.EMPTY
, false) == null) {
43 PsiType
[] actualTypeParameters
= parameterList
.getTypeArguments();
44 PsiTypeParameter
[] classTypeParameters
= aClass
.getTypeParameters();
45 HashMap
<PsiTypeParameter
, PsiType
> map
= new HashMap
<PsiTypeParameter
, PsiType
>();
46 for (int j
= 0; j
< classTypeParameters
.length
; j
++) {
47 PsiTypeParameter classTypeParameter
= classTypeParameters
[j
];
48 PsiType actualTypeParameter
= actualTypeParameters
[j
];
49 map
.put(classTypeParameter
, actualTypeParameter
);
51 PsiSubstitutor substitutor
= factory
.createSubstitutor(map
);
52 PsiType suggestedType
= factory
.createType(aClass
, substitutor
);
53 HighlightUtil
.registerChangeVariableTypeFixes(variable
, suggestedType
, highlightInfo
);