create property:both getter nad setter from usage (IDEA-73186); return cursor to the initial state

This commit is contained in:
anna
2012-07-12 20:45:33 +02:00
parent b2530c8d58
commit f3f98b6f54
10 changed files with 172 additions and 5 deletions

View File

@@ -497,6 +497,7 @@ public class HighlightMethodUtil {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreateConstructorFromSuperFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreateConstructorFromThisFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreatePropertyFromUsageFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new CreateGetterSetterPropertyFromUsageFix(methodCall));
CandidateInfo[] methodCandidates = resolveHelper.getReferencedMethodCandidates(methodCall, false);
CastMethodArgumentFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
PermuteArgumentsFix.registerFix(highlightInfo, methodCall, methodCandidates, fixRange);

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiMethodCallExpression;
import com.intellij.psi.util.PropertyUtil;
import java.util.List;
/**
* User: anna
* Date: 7/12/12
*/
public class CreateGetterSetterPropertyFromUsageFix extends CreatePropertyFromUsageFix {
public CreateGetterSetterPropertyFromUsageFix(PsiMethodCallExpression methodCall) {
super(methodCall);
}
@Override
protected boolean isAvailableImpl(int offset) {
boolean available = super.isAvailableImpl(offset);
if (available) {
setText("Create Property");
}
return available;
}
@Override
protected boolean checkTargetClasses(List<PsiClass> classes, String methodName) {
String propertyName = PropertyUtil.getPropertyName(methodName);
if (propertyName == null) return false;
String getterName = PropertyUtil.suggestGetterName(propertyName, null);
String setterName = PropertyUtil.suggestSetterName(propertyName);
for (PsiClass aClass : classes) {
if (aClass.findMethodsByName(getterName, false).length > 0 || aClass.findMethodsByName(setterName, false).length > 0) return false;
}
return true;
}
@Override
protected void beforeTemplateFinished(PsiClass aClass, PsiField field) {
PsiMethod getterPrototype = PropertyUtil.generateGetterPrototype(field);
if (aClass.findMethodsBySignature(getterPrototype, false).length == 0) {
aClass.add(getterPrototype);
}
PsiMethod setterPrototype = PropertyUtil.generateSetterPrototype(field);
if (aClass.findMethodsBySignature(setterPrototype, false).length == 0) {
aClass.add(setterPrototype);
}
super.beforeTemplateFinished(aClass, field);
}
}

View File

@@ -18,6 +18,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.completion.JavaLookupElementBuilder;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInsight.intention.impl.TypeExpression;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.template.*;
@@ -47,7 +48,7 @@ import java.util.Set;
/**
* @author ven
*/
public class CreatePropertyFromUsageFix extends CreateFromUsageBaseFix {
public class CreatePropertyFromUsageFix extends CreateFromUsageBaseFix implements HighPriorityAction {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.CreatePropertyFromUsageFix");
@NonNls private static final String FIELD_VARIABLE = "FIELD_NAME_VARIABLE";
@NonNls private static final String TYPE_VARIABLE = "FIELD_TYPE_VARIABLE";
@@ -59,7 +60,7 @@ public class CreatePropertyFromUsageFix extends CreateFromUsageBaseFix {
myMethodCall = methodCall;
}
private final PsiMethodCallExpression myMethodCall;
protected final PsiMethodCallExpression myMethodCall;
@Override
@NotNull
@@ -98,6 +99,8 @@ public class CreatePropertyFromUsageFix extends CreateFromUsageBaseFix {
List<PsiClass> classes = getTargetClasses(myMethodCall);
if (classes.isEmpty()) return false;
if (!checkTargetClasses(classes, methodName)) return false;
for (PsiClass aClass : classes) {
if (!aClass.isInterface()) {
setText(getterOrSetter);
@@ -108,6 +111,10 @@ public class CreatePropertyFromUsageFix extends CreateFromUsageBaseFix {
return false;
}
protected boolean checkTargetClasses(List<PsiClass> classes, String methodName) {
return true;
}
static class FieldExpression extends Expression {
private final String myDefaultFieldName;
private final PsiField myField;
@@ -266,15 +273,19 @@ public class CreatePropertyFromUsageFix extends CreateFromUsageBaseFix {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
if (aClass == null) return;
if (aClass.findFieldByName(fieldName, true) != null) return;
PsiField field = aClass.findFieldByName(fieldName, true);
if (field != null){
CreatePropertyFromUsageFix.this.beforeTemplateFinished(aClass, field);
return;
}
PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
try {
PsiType type = factory.createTypeFromText(fieldType, aClass);
try {
PsiField field = factory.createField(fieldName, type);
field = factory.createField(fieldName, type);
field = (PsiField)aClass.add(field);
PsiUtil.setModifierProperty(field, PsiModifier.STATIC, isStatic1);
positionCursor(project, field.getContainingFile(), field);
CreatePropertyFromUsageFix.this.beforeTemplateFinished(aClass, field);
}
catch (IncorrectOperationException e) {
LOG.error(e);
@@ -303,6 +314,10 @@ public class CreatePropertyFromUsageFix extends CreateFromUsageBaseFix {
});
}
protected void beforeTemplateFinished(PsiClass aClass, PsiField field) {
positionCursor(myMethodCall.getProject(), myMethodCall.getContainingFile(), myMethodCall);
}
private static String getVariableName(PsiMethodCallExpression methodCall, boolean isStatic) {
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(methodCall.getProject());
String methodName = methodCall.getMethodExpression().getReferenceName();

View File

@@ -1,6 +1,7 @@
// "Create Getter" "true"
public class Test {
Integer field;
public foo() {
getField();
}

View File

@@ -0,0 +1,16 @@
// "Create Property" "true"
class Calculator {
int i;
public void printError() {
setI(0);
}
public void setI(int i) {
this.i = i;
}
public int getI() {
return i;
}
}

View File

@@ -0,0 +1,16 @@
// "Create Property" "true"
class Calculator {
private int i;
public void printError() {
setI(0);
}
public void setI(int i) {
this.i = i;
}
public int getI() {
return i;
}
}

View File

@@ -0,0 +1,8 @@
// "Create Property" "false"
class Calculator {
int i;
public void printError() {
set<caret>I(0);
}
public int getI() {return i;}
}

View File

@@ -0,0 +1,7 @@
// "Create Property" "true"
class Calculator {
int i;
public void printError() {
set<caret>I(0);
}
}

View File

@@ -0,0 +1,6 @@
// "Create Property" "true"
class Calculator {
public void printError() {
set<caret>I(0);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.quickFix;
public class CreatePropertyFromUsageTest extends LightQuickFix15TestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/createPropertyFromUsage";
}
}