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
.QuickFixBundle
;
19 import com
.intellij
.openapi
.diagnostic
.Logger
;
20 import com
.intellij
.pom
.java
.LanguageLevel
;
21 import com
.intellij
.psi
.*;
22 import com
.intellij
.psi
.util
.PsiUtil
;
23 import com
.intellij
.util
.IncorrectOperationException
;
24 import org
.jetbrains
.annotations
.NotNull
;
26 public class AddTypeArgumentsFix
extends MethodArgumentFix
{
27 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.AddTypeArgumentsFix");
29 private AddTypeArgumentsFix(PsiExpressionList list
, int i
, PsiType toType
, final ArgumentFixerActionFactory factory
) {
30 super(list
, i
, toType
, factory
);
34 public String
getText() {
35 if (myArgList
.getExpressions().length
== 1) {
36 return QuickFixBundle
.message("add.type.arguments.single.argument.text");
39 return QuickFixBundle
.message("add.type.arguments.text", myIndex
+ 1);
42 private static class MyFixerActionFactory
extends ArgumentFixerActionFactory
{
43 public AddTypeArgumentsFix
createFix(final PsiExpressionList list
, final int i
, final PsiType toType
) {
44 return new AddTypeArgumentsFix(list
, i
, toType
, this);
47 protected PsiExpression
getModifiedArgument(final PsiExpression expression
, final PsiType toType
) throws IncorrectOperationException
{
48 if (!PsiUtil
.isLanguageLevel5OrHigher(expression
)) return null;
50 if (expression
instanceof PsiMethodCallExpression
) {
51 final PsiMethodCallExpression methodCall
= (PsiMethodCallExpression
)expression
;
52 final PsiReferenceParameterList list
= methodCall
.getMethodExpression().getParameterList();
53 if (list
== null || list
.getTypeArguments().length
> 0) return null;
54 final JavaResolveResult resolveResult
= methodCall
.resolveMethodGenerics();
55 final PsiElement element
= resolveResult
.getElement();
56 if (element
instanceof PsiMethod
) {
57 final PsiMethod method
= (PsiMethod
)element
;
58 final PsiType returnType
= method
.getReturnType();
59 if (returnType
== null) return null;
61 final PsiTypeParameter
[] typeParameters
= method
.getTypeParameters();
62 if (typeParameters
.length
> 0) {
63 PsiType
[] mappings
= new PsiType
[typeParameters
.length
];
64 PsiResolveHelper helper
= JavaPsiFacade
.getInstance(expression
.getProject()).getResolveHelper();
65 LanguageLevel level
= PsiUtil
.getLanguageLevel(expression
);
66 for (int i
= 0; i
< typeParameters
.length
; i
++) {
67 PsiTypeParameter typeParameter
= typeParameters
[i
];
68 final PsiType substitution
= helper
.getSubstitutionForTypeParameter(typeParameter
, returnType
, toType
, false, level
);
69 if (substitution
== null || PsiType
.NULL
.equals(substitution
)) return null;
70 mappings
[i
] = substitution
;
73 final PsiElementFactory factory
= JavaPsiFacade
.getInstance(expression
.getProject()).getElementFactory();
74 PsiMethodCallExpression copy
= (PsiMethodCallExpression
)expression
.copy();
75 final PsiReferenceParameterList parameterList
= copy
.getMethodExpression().getParameterList();
76 LOG
.assertTrue(parameterList
!= null);
77 for (PsiType mapping
: mappings
) {
78 parameterList
.add(factory
.createTypeElement(mapping
));
89 public boolean areTypesConvertible(final PsiType exprType
, final PsiType parameterType
, final PsiElement context
) {
90 return !(exprType
instanceof PsiPrimitiveType
) &&
91 !(parameterType
instanceof PsiPrimitiveType
);
95 public static ArgumentFixerActionFactory REGISTRAR
= new AddTypeArgumentsFix
.MyFixerActionFactory();