diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/expression/FlipSetterCallIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/expression/FlipSetterCallIntention.java index 1591503f2eba..be4303d55f9c 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/expression/FlipSetterCallIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/expression/FlipSetterCallIntention.java @@ -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; } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Parentheses.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Parentheses.java new file mode 100644 index 000000000000..0a201386ca7f --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Parentheses.java @@ -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.getName())); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Parentheses_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Parentheses_after.java new file mode 100644 index 000000000000..be17232a18f8 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Parentheses_after.java @@ -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()); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Simple.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Simple.java new file mode 100644 index 000000000000..6638a9d0f66a --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Simple.java @@ -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.getName()); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Simple_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Simple_after.java new file mode 100644 index 000000000000..b3079b8c4cd6 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Simple_after.java @@ -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.getName()); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Unqualified.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Unqualified.java new file mode 100644 index 000000000000..6129f8a7eebc --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Unqualified.java @@ -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.getName()); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Unqualified_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Unqualified_after.java new file mode 100644 index 000000000000..6a04965f06e4 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expression/flip_setter_call/Unqualified_after.java @@ -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.getName()); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/expression/FlipSetterCallIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/expression/FlipSetterCallIntentionTest.java new file mode 100644 index 000000000000..76265ad1a2e7 --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/expression/FlipSetterCallIntentionTest.java @@ -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"; + } +}