mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
FlipSetterCallIntention: deparenthesize; track comments; replace no qualifier with 'this', tests
This commit is contained in:
+16
-24
@@ -9,6 +9,9 @@ import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PropertyUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ipp.base.Intention;
|
||||
import com.siyeh.ipp.base.PsiElementEditorPredicate;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
@@ -48,34 +51,23 @@ public class FlipSetterCallIntention extends Intention {
|
||||
}
|
||||
|
||||
private static void flipCall(PsiMethodCallExpression call) {
|
||||
final PsiExpression qualifierExpression1 = call.getMethodExpression().getQualifierExpression();
|
||||
if (qualifierExpression1 == null) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression[] arguments = call.getArgumentList().getExpressions();
|
||||
if (arguments.length != 1) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression argument = arguments[0];
|
||||
if (!(argument instanceof PsiMethodCallExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)argument;
|
||||
final PsiExpression qualifierExpression2 = methodCallExpression.getMethodExpression().getQualifierExpression();
|
||||
if (qualifierExpression2 == null) {
|
||||
return;
|
||||
}
|
||||
if (arguments.length != 1) return;
|
||||
final PsiExpression argument = PsiUtil.skipParenthesizedExprDown(arguments[0]);
|
||||
if (!(argument instanceof PsiMethodCallExpression)) return;
|
||||
final PsiMethodCallExpression call2 = (PsiMethodCallExpression)argument;
|
||||
|
||||
final PsiExpression qualifierExpression1 = ExpressionUtils.getQualifierOrThis(call.getMethodExpression());
|
||||
final PsiExpression qualifierExpression2 = ExpressionUtils.getQualifierOrThis(call2.getMethodExpression());
|
||||
final PsiMethod setter = call.resolveMethod();
|
||||
final PsiMethod getter = methodCallExpression.resolveMethod();
|
||||
final PsiMethod getter = call2.resolveMethod();
|
||||
final PsiMethod get = PropertyUtil.getReversePropertyMethod(setter);
|
||||
final PsiMethod set = PropertyUtil.getReversePropertyMethod(getter);
|
||||
if (get == null || set == null) {
|
||||
return;
|
||||
}
|
||||
if (get == null || set == null) return;
|
||||
CommentTracker ct = new CommentTracker();
|
||||
final String text =
|
||||
qualifierExpression2.getText() + "." + set.getName() + "(" + qualifierExpression1.getText() + "." + get.getName() + "())";
|
||||
final PsiExpression newExpression = JavaPsiFacade.getElementFactory(call.getProject()).createExpressionFromText(text, call);
|
||||
call.replace(newExpression);
|
||||
ct.text(qualifierExpression2) + "." + set.getName() + "(" + ct.text(qualifierExpression1) + "." + get.getName() + "())";
|
||||
ct.replaceAndRestoreComments(call, text);
|
||||
}
|
||||
|
||||
private static boolean isSetGetMethodCall(PsiElement element) {
|
||||
@@ -87,7 +79,7 @@ public class FlipSetterCallIntention extends Intention {
|
||||
if (arguments.length != 1) {
|
||||
return false;
|
||||
}
|
||||
final PsiExpression argument = arguments[0];
|
||||
final PsiExpression argument = PsiUtil.skipParenthesizedExprDown(arguments[0]);
|
||||
if (!(argument instanceof PsiMethodCallExpression)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class X {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
void test(X a, X b) {
|
||||
a.setName(/*comment*/(b.<caret>getName()));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
class X {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
void test(X a, X b) {
|
||||
/*comment*/
|
||||
b.setName(a.getName(<caret>));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
class X {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
void test(X a, X b) {
|
||||
a.setName(b.<caret>getName());
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class X {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
void test(X a, X b) {
|
||||
b.setName(a.<caret>getName());
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class X {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
void test(X a, X b) {
|
||||
setName(a.<caret>getName());
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class X {
|
||||
String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
void test(X a, X b) {
|
||||
a.setName(this.<caret>getName());
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ipp.expression;
|
||||
|
||||
import com.siyeh.IntentionPowerPackBundle;
|
||||
import com.siyeh.ipp.IPPTestCase;
|
||||
|
||||
/**
|
||||
* @see FlipSetterCallIntention
|
||||
*/
|
||||
public class FlipSetterCallIntentionTest extends IPPTestCase {
|
||||
public void testSimple() { doTest(); }
|
||||
public void testUnqualified() { doTest(); }
|
||||
public void testParentheses() { doTest(); }
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return IntentionPowerPackBundle.message("flip.setter.call.intention.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
return "expression/flip_setter_call";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user