IDEA-61130 Intention to replace an assignment with a setter call implemented

This commit is contained in:
Danila Ponomarenko
2012-06-08 16:18:43 +04:00
parent b59b632997
commit 8edc336fd2
8 changed files with 181 additions and 0 deletions
@@ -0,0 +1,101 @@
/*
* 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.intention.impl;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Danila Ponomarenko
*/
public class EncapsulateFieldAction implements IntentionAction {
@NotNull
@Override
public String getText() {
return CodeInsightBundle.message("intention.encapsulate.field.text");
}
@NotNull
@Override
public String getFamilyName() {
return getText();
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
final PsiField field = getField(getElement(editor, file));
return field != null && !field.hasModifierProperty(PsiModifier.FINAL) && !field.hasModifierProperty(PsiModifier.PRIVATE);
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final PsiField field = getField(getElement(editor, file));
if (field == null) {
return;
}
final RefactoringActionHandler refactoringActionHandler = JavaRefactoringActionHandlerFactory.getInstance().createEncapsulateFieldsHandler();
refactoringActionHandler.invoke(project, new PsiElement[]{field}, null);
}
@Nullable
protected static PsiField getField(@Nullable PsiElement element) {
if (element == null || !(element instanceof PsiIdentifier)) {
return null;
}
final PsiElement parent = element.getParent();
if (parent == null || !(parent instanceof PsiReferenceExpression)) {
return null;
}
final PsiReferenceExpression ref = (PsiReferenceExpression)parent;
final PsiExpression qualifier = ref.getQualifierExpression();
if (qualifier == null || qualifier instanceof PsiThisExpression) {
return null;
}
final PsiElement resolved = ref.resolve();
if (resolved == null || !(resolved instanceof PsiField)) {
return null;
}
return (PsiField)resolved;
}
@Nullable
protected static PsiElement getElement(Editor editor, @NotNull PsiFile file) {
if (!file.getManager().isInProject(file)) return null;
final CaretModel caretModel = editor.getCaretModel();
final int position = caretModel.getOffset();
return file.findElementAt(position);
}
@Override
public boolean startInWriteAction() {
return false;
}
}
@@ -0,0 +1,13 @@
// "Encapsulate field" "false"
class A {
public final boolean m_bool;
}
public class B {
void method() {
A a = new A();
a.m_bo<caret>ol = true;
}
}
@@ -0,0 +1,13 @@
// "Encapsulate field" "false"
class A {
private boolean m_bool;
}
public class B {
void method() {
A a = new A();
a.m_bo<caret>ol = true;
}
}
@@ -0,0 +1,9 @@
// "Encapsulate field" "false"
class A {
public final boolean m_bool;
void method() {
m_bo<caret>ol = true;
}
}
@@ -0,0 +1,9 @@
// "Encapsulate field" "false"
class A {
public final boolean m_bool;
void method() {
this.m_bo<caret>ol = true;
}
}
@@ -0,0 +1,31 @@
/*
* 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.intention;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
/**
* @author Danila Ponomarenko
*/
public class EncapsulateFieldTest extends LightIntentionActionTestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/encapsulateField";
}
}
@@ -157,6 +157,7 @@ intention.make.type.generic.text=Change type of {0} to {1}
intention.split.if.family=Split If
intention.split.if.text=Split into 2 if's
intention.introduce.variable.text=Introduce local variable
intention.encapsulate.field.text=Encapsulate field
intention.implement.abstract.method.family=Implement Abstract Method
intention.implement.abstract.method.text=Implement method ''{0}''
intention.override.method.text=Override method ''{0}''
+4
View File
@@ -639,6 +639,10 @@
<className>com.intellij.codeInsight.intention.impl.IntroduceVariableAction</className>
<category>Declaration</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.EncapsulateFieldAction</className>
<category>Declaration</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.daemon.impl.quickfix.DelegateWithDefaultParamValueIntentionAction</className>