update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / beanProperties / CreateBeanPropertyFix.java
blobcf7884c0c8069c3da4dd30e7c29305473480a114
1 /*
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;
37 /**
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) {
57 if (type == null) {
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));
61 if (aClass == null) {
62 return new CreateBeanPropertyFix[0];
64 type = facade.getElementFactory().createType(aClass);
66 return new CreateBeanPropertyFix[] {
67 new CreateBeanPropertyFix(propertyName, psiClass, type) {
69 @NotNull
70 public String getName() {
71 return QuickFixBundle.message("create.readable.writable.property.with.field", myPropertyName);
74 protected void doFix() throws IncorrectOperationException {
75 createField();
76 createSetter(true);
77 createGetter(true);
80 new CreateBeanPropertyFix(propertyName, psiClass, type) {
81 protected void doFix() throws IncorrectOperationException {
82 if (createSetter) {
83 createSetter(false);
85 else {
86 createGetter(false);
90 @NotNull
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 {
97 createField();
98 if (createSetter) {
99 createSetter(true);
101 else {
102 createGetter(true);
106 @NotNull
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;
118 myType = type;
121 @NotNull
122 public String getFamilyName() {
123 return getName();
126 public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
127 applyFix(project);
130 private void applyFix(final Project project) {
131 new WriteCommandAction.Simple(project, getName(), myPsiClass.getContainingFile()) {
132 protected void run() throws Throwable {
133 try {
134 doFix();
136 catch (IncorrectOperationException e) {
137 LOG.error("Cannot create property", e);
140 }.execute();
143 @NotNull
144 public String getText() {
145 return getName();
148 public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
149 return true;
152 public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
153 applyFix(project);
156 public boolean startInWriteAction() {
157 return false;
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;
172 if (createField) {
173 @NonNls String fieldName = getFieldName();
174 if (fieldName.equals(myPropertyName)) {
175 fieldName = "this." + fieldName;
177 text = "public void " + methodName + "(" + typeName + " " + myPropertyName + ") {" + fieldName + "=" + myPropertyName + ";}";
178 } else {
179 text = "public void " + methodName + "(" + typeName + " " + myPropertyName + ") {}";
181 final PsiMethod method = elementFactory.createMethodFromText(text, null);
182 final PsiMethod psiElement = (PsiMethod)myPsiClass.add(method);
183 if (!createField) {
184 CreateFromUsageUtils.setupMethodBody(psiElement);
186 return 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;
194 if (createField) {
195 final String fieldName = getFieldName();
196 text = "public " + typeName + " " + methodName + "() { return " + fieldName + "; }";
197 } else {
198 text = "public " + typeName + " " + methodName + "() { return null; }";
200 final PsiMethod method = elementFactory.createMethodFromText(text, null);
201 final PsiMethod psiElement = (PsiMethod)myPsiClass.add(method);
202 if (!createField) {
203 CreateFromUsageUtils.setupMethodBody(psiElement);
205 return 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);