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
.ExpectedTypeInfo
;
19 import com
.intellij
.codeInsight
.daemon
.QuickFixBundle
;
20 import com
.intellij
.codeInspection
.LocalQuickFix
;
21 import com
.intellij
.codeInspection
.ProblemDescriptor
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.util
.Pair
;
24 import com
.intellij
.openapi
.vfs
.ReadonlyStatusHandler
;
25 import com
.intellij
.psi
.*;
26 import com
.intellij
.psi
.util
.PsiFormatUtil
;
27 import com
.intellij
.util
.Function
;
28 import com
.intellij
.util
.IncorrectOperationException
;
29 import com
.intellij
.util
.containers
.ContainerUtil
;
30 import org
.jetbrains
.annotations
.NonNls
;
31 import org
.jetbrains
.annotations
.NotNull
;
32 import org
.jetbrains
.annotations
.Nullable
;
34 import java
.util
.List
;
36 public class CreateMethodQuickFix
implements LocalQuickFix
{
37 protected final PsiClass myTargetClass
;
38 protected final String mySignature
;
39 protected final String myBody
;
41 private CreateMethodQuickFix(final PsiClass targetClass
, @NonNls final String signature
, @NonNls final String body
) {
42 myTargetClass
= targetClass
;
43 mySignature
= signature
;
48 public String
getName() {
50 String signature
= PsiFormatUtil
.formatMethod(createMethod(myTargetClass
.getProject()), PsiSubstitutor
.EMPTY
,
51 PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_TYPE
| PsiFormatUtil
.SHOW_PARAMETERS
| PsiFormatUtil
.SHOW_RAW_TYPE
,
52 PsiFormatUtil
.SHOW_TYPE
| PsiFormatUtil
.SHOW_RAW_TYPE
, 2);
53 return QuickFixBundle
.message("create.method.from.usage.text", signature
);
57 public String
getFamilyName() {
58 return QuickFixBundle
.message("create.method.from.usage.family");
61 public void applyFix(@NotNull final Project project
, @NotNull final ProblemDescriptor descriptor
) {
63 if (ReadonlyStatusHandler
.getInstance(project
).ensureFilesWritable(myTargetClass
.getContainingFile().getVirtualFile()).hasReadonlyFiles()) return;
65 PsiMethod method
= createMethod(project
);
66 List
<Pair
<PsiExpression
,PsiType
>> arguments
=
67 ContainerUtil
.map2List(method
.getParameterList().getParameters(), new Function
<PsiParameter
, Pair
<PsiExpression
, PsiType
>>() {
68 public Pair
<PsiExpression
, PsiType
> fun(PsiParameter psiParameter
) {
69 return Pair
.create(null, psiParameter
.getType());
73 method
= (PsiMethod
)myTargetClass
.add(method
);
74 CreateMethodFromUsageFix
.doCreate(myTargetClass
, method
, arguments
, PsiSubstitutor
.EMPTY
, ExpectedTypeInfo
.EMPTY_ARRAY
, method
);
76 catch (IncorrectOperationException e
) {
77 throw new RuntimeException(e
);
81 private PsiMethod
createMethod(Project project
) {
82 PsiElementFactory elementFactory
= JavaPsiFacade
.getInstance(project
).getElementFactory();
83 String methodText
= mySignature
+ "{" + myBody
+ "}";
84 return elementFactory
.createMethodFromText(methodText
, null);
88 public static CreateMethodQuickFix
createFix(final PsiClass targetClass
, @NonNls final String signature
, @NonNls final String body
) {
89 CreateMethodQuickFix fix
= new CreateMethodQuickFix(targetClass
, signature
, body
);
91 fix
.createMethod(targetClass
.getProject());
94 catch (IncorrectOperationException e
) {