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
.CodeInsightUtilBase
;
19 import com
.intellij
.codeInsight
.daemon
.QuickFixBundle
;
20 import com
.intellij
.codeInspection
.IntentionAndQuickFixAction
;
21 import com
.intellij
.openapi
.application
.ApplicationManager
;
22 import com
.intellij
.openapi
.command
.undo
.UndoUtil
;
23 import com
.intellij
.openapi
.diagnostic
.Logger
;
24 import com
.intellij
.openapi
.editor
.Editor
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.openapi
.util
.Comparing
;
27 import com
.intellij
.psi
.*;
28 import com
.intellij
.psi
.codeStyle
.JavaCodeStyleManager
;
29 import com
.intellij
.psi
.codeStyle
.SuggestedNameInfo
;
30 import com
.intellij
.psi
.codeStyle
.VariableKind
;
31 import com
.intellij
.psi
.util
.PsiUtil
;
32 import com
.intellij
.psi
.util
.TypeConversionUtil
;
33 import com
.intellij
.refactoring
.changeSignature
.ChangeSignatureProcessor
;
34 import com
.intellij
.refactoring
.changeSignature
.ParameterInfoImpl
;
35 import com
.intellij
.util
.IncorrectOperationException
;
36 import org
.jetbrains
.annotations
.NotNull
;
37 import org
.jetbrains
.annotations
.Nullable
;
39 import java
.util
.ArrayList
;
40 import java
.util
.List
;
42 public class MethodParameterFix
extends IntentionAndQuickFixAction
{
43 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.MethodReturnFix");
45 private final PsiMethod myMethod
;
46 private final PsiType myParameterType
;
47 private final int myIndex
;
48 private final boolean myFixWholeHierarchy
;
50 public MethodParameterFix(PsiMethod method
, PsiType type
, int index
, boolean fixWholeHierarchy
) {
52 myParameterType
= type
;
54 myFixWholeHierarchy
= fixWholeHierarchy
;
58 public String
getName() {
59 return QuickFixBundle
.message("fix.parameter.type.text",
61 myParameterType
.getCanonicalText() );
65 public String
getFamilyName() {
66 return QuickFixBundle
.message("fix.parameter.type.family");
69 public boolean isAvailable(@NotNull final Project project
, final Editor editor
, final PsiFile file
) {
70 return myMethod
!= null
72 && myMethod
.getManager().isInProject(myMethod
)
73 && myParameterType
!= null
74 && !TypeConversionUtil
.isNullType(myParameterType
)
75 && myMethod
.getReturnType() != null
76 && !Comparing
.equal(myParameterType
, myMethod
.getReturnType());
79 public void applyFix(final Project project
, final PsiFile file
, @Nullable final Editor editor
) {
80 if (!CodeInsightUtilBase
.prepareFileForWrite(myMethod
.getContainingFile())) return;
82 PsiMethod method
= myMethod
;
83 if (myFixWholeHierarchy
) {
84 method
= myMethod
.findDeepestSuperMethod();
85 if (method
== null) method
= myMethod
;
88 ChangeSignatureProcessor processor
= new ChangeSignatureProcessor(project
,
92 method
.getReturnType(),
93 getNewParametersInfo());
95 if (ApplicationManager
.getApplication().isUnitTestMode()) {
103 UndoUtil
.markPsiFileForUndo(file
);
105 catch (IncorrectOperationException e
) {
110 private ParameterInfoImpl
[] getNewParametersInfo() throws IncorrectOperationException
{
111 List
<ParameterInfoImpl
> result
= new ArrayList
<ParameterInfoImpl
>();
112 PsiParameter
[] parameters
= myMethod
.getParameterList().getParameters();
113 PsiElementFactory factory
= JavaPsiFacade
.getInstance(myMethod
.getProject()).getElementFactory();
114 JavaCodeStyleManager codeStyleManager
= JavaCodeStyleManager
.getInstance(myMethod
.getProject());
115 SuggestedNameInfo nameInfo
= codeStyleManager
.suggestVariableName(VariableKind
.PARAMETER
, null, null, myParameterType
);
116 PsiParameter newParameter
= factory
.createParameter(nameInfo
.names
[0], myParameterType
);
117 if (myMethod
.getContainingClass().isInterface()) {
118 PsiUtil
.setModifierProperty(newParameter
, PsiModifier
.FINAL
, false);
121 for (int i
= 0; i
< parameters
.length
; i
++) {
122 PsiParameter parameter
= parameters
[i
];
124 newParameter
.setName(parameter
.getName());
125 parameter
= newParameter
;
127 result
.add(new ParameterInfoImpl(i
, parameter
.getName(), parameter
.getType()));
129 if (parameters
.length
== myIndex
) {
130 result
.add(new ParameterInfoImpl(-1, newParameter
.getName(), newParameter
.getType()));
132 return result
.toArray(new ParameterInfoImpl
[result
.size()]);
135 public boolean startInWriteAction() {