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
.psi
.impl
.beanProperties
;
18 import com
.intellij
.codeInsight
.daemon
.QuickFixBundle
;
19 import com
.intellij
.codeInsight
.daemon
.impl
.quickfix
.CreateFromUsageUtils
;
20 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
21 import com
.intellij
.codeInspection
.LocalQuickFix
;
22 import com
.intellij
.codeInspection
.ProblemDescriptor
;
23 import com
.intellij
.openapi
.command
.WriteCommandAction
;
24 import com
.intellij
.openapi
.diagnostic
.Logger
;
25 import com
.intellij
.openapi
.editor
.Editor
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.psi
.*;
28 import com
.intellij
.psi
.codeStyle
.JavaCodeStyleManager
;
29 import com
.intellij
.psi
.codeStyle
.VariableKind
;
30 import com
.intellij
.psi
.search
.GlobalSearchScope
;
31 import com
.intellij
.psi
.util
.PropertyUtil
;
32 import com
.intellij
.util
.IncorrectOperationException
;
33 import org
.jetbrains
.annotations
.NonNls
;
34 import org
.jetbrains
.annotations
.NotNull
;
35 import org
.jetbrains
.annotations
.Nullable
;
38 * @author Dmitry Avdeev
40 public abstract class CreateBeanPropertyFix
implements LocalQuickFix
, IntentionAction
{
42 private final static Logger LOG
= Logger
.getInstance("#com.intellij.psi.impl.beanProperties.CreateBeanPropertyFix");
44 protected final String myPropertyName
;
45 @NotNull protected final PsiClass myPsiClass
;
46 @NotNull protected final PsiType myType
;
48 public static LocalQuickFix
[] createFixes(String propertyName
, @NotNull PsiClass psiClass
, @Nullable PsiType type
, final boolean createSetter
) {
49 return (LocalQuickFix
[])create(propertyName
, psiClass
, type
, createSetter
);
52 public static IntentionAction
[] createActions(String propertyName
, @NotNull PsiClass psiClass
, @Nullable PsiType type
, final boolean createSetter
) {
53 return (IntentionAction
[])create(propertyName
, psiClass
, type
, createSetter
);
56 private static Object
[] create(final String propertyName
, final PsiClass psiClass
, PsiType type
, final boolean createSetter
) {
58 final Project project
= psiClass
.getProject();
59 final JavaPsiFacade facade
= JavaPsiFacade
.getInstance(project
);
60 final PsiClass aClass
= facade
.findClass("java.lang.String", GlobalSearchScope
.allScope(project
));
62 return new CreateBeanPropertyFix
[0];
64 type
= facade
.getElementFactory().createType(aClass
);
66 return new CreateBeanPropertyFix
[] {
67 new CreateBeanPropertyFix(propertyName
, psiClass
, type
) {
70 public String
getName() {
71 return QuickFixBundle
.message("create.readable.writable.property.with.field", myPropertyName
);
74 protected void doFix() throws IncorrectOperationException
{
80 new CreateBeanPropertyFix(propertyName
, psiClass
, type
) {
81 protected void doFix() throws IncorrectOperationException
{
91 public String
getName() {
92 return QuickFixBundle
.message(createSetter ?
"create.writable.property" : "create.readable.property", myPropertyName
);
95 new CreateBeanPropertyFix(propertyName
, psiClass
, type
) {
96 protected void doFix() throws IncorrectOperationException
{
107 public String
getName() {
108 return QuickFixBundle
.message(createSetter ?
"create.writable.property.with.field" : "create.readable.property.with.field", myPropertyName
);
115 protected CreateBeanPropertyFix(String propertyName
, @NotNull PsiClass psiClass
, @NotNull PsiType type
) {
116 myPropertyName
= propertyName
;
117 myPsiClass
= psiClass
;
122 public String
getFamilyName() {
126 public void applyFix(@NotNull final Project project
, @NotNull final ProblemDescriptor descriptor
) {
130 private void applyFix(final Project project
) {
131 new WriteCommandAction
.Simple(project
, getName(), myPsiClass
.getContainingFile()) {
132 protected void run() throws Throwable
{
136 catch (IncorrectOperationException e
) {
137 LOG
.error("Cannot create property", e
);
144 public String
getText() {
148 public boolean isAvailable(@NotNull final Project project
, final Editor editor
, final PsiFile file
) {
152 public void invoke(@NotNull final Project project
, final Editor editor
, final PsiFile file
) throws IncorrectOperationException
{
156 public boolean startInWriteAction() {
160 protected abstract void doFix() throws IncorrectOperationException
;
162 private String
getFieldName() {
163 final JavaCodeStyleManager styleManager
= JavaCodeStyleManager
.getInstance(myPsiClass
.getProject());
164 return styleManager
.suggestVariableName(VariableKind
.FIELD
, myPropertyName
, null, myType
).names
[0];
167 protected PsiElement
createSetter(final boolean createField
) throws IncorrectOperationException
{
168 final PsiElementFactory elementFactory
= JavaPsiFacade
.getInstance(myPsiClass
.getProject()).getElementFactory();
169 final String methodName
= PropertyUtil
.suggestSetterName(myPropertyName
);
170 final String typeName
= myType
.getCanonicalText();
171 @NonNls final String text
;
173 @NonNls String fieldName
= getFieldName();
174 if (fieldName
.equals(myPropertyName
)) {
175 fieldName
= "this." + fieldName
;
177 text
= "public void " + methodName
+ "(" + typeName
+ " " + myPropertyName
+ ") {" + fieldName
+ "=" + myPropertyName
+ ";}";
179 text
= "public void " + methodName
+ "(" + typeName
+ " " + myPropertyName
+ ") {}";
181 final PsiMethod method
= elementFactory
.createMethodFromText(text
, null);
182 final PsiMethod psiElement
= (PsiMethod
)myPsiClass
.add(method
);
184 CreateFromUsageUtils
.setupMethodBody(psiElement
);
189 protected PsiElement
createGetter(final boolean createField
) throws IncorrectOperationException
{
190 final PsiElementFactory elementFactory
= JavaPsiFacade
.getInstance(myPsiClass
.getProject()).getElementFactory();
191 final String methodName
= PropertyUtil
.suggestGetterName(myPropertyName
, myType
);
192 final String typeName
= myType
.getCanonicalText();
193 @NonNls final String text
;
195 final String fieldName
= getFieldName();
196 text
= "public " + typeName
+ " " + methodName
+ "() { return " + fieldName
+ "; }";
198 text
= "public " + typeName
+ " " + methodName
+ "() { return null; }";
200 final PsiMethod method
= elementFactory
.createMethodFromText(text
, null);
201 final PsiMethod psiElement
= (PsiMethod
)myPsiClass
.add(method
);
203 CreateFromUsageUtils
.setupMethodBody(psiElement
);
208 protected PsiElement
createField() throws IncorrectOperationException
{
209 final String fieldName
= getFieldName();
210 final PsiElementFactory elementFactory
= JavaPsiFacade
.getInstance(myPsiClass
.getProject()).getElementFactory();
211 final PsiField psiField
= elementFactory
.createField(fieldName
, myType
);
212 return myPsiClass
.add(psiField
);