From 5758bb4185cde7360f35ef42afd16284c0ab601c Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Mon, 13 Nov 2017 15:54:40 +0100 Subject: [PATCH] move instance method with method ref: ensure qualifier is not deleted IDEA-181919 --- .../MoveInstanceMethodProcessor.java | 9 +++++++-- .../MethodReferenceWithThisTarget.java | 12 ++++++++++++ .../MethodReferenceWithThisTarget.java.after | 13 +++++++++++++ .../moveMethod/MoveInstanceMethodTest.java | 4 ++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/refactoring/moveInstanceMethod/MethodReferenceWithThisTarget.java create mode 100644 java/java-tests/testData/refactoring/moveInstanceMethod/MethodReferenceWithThisTarget.java.after diff --git a/java/java-impl/src/com/intellij/refactoring/move/moveInstanceMethod/MoveInstanceMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/move/moveInstanceMethod/MoveInstanceMethodProcessor.java index e3e18003517c..68d098d04a44 100644 --- a/java/java-impl/src/com/intellij/refactoring/move/moveInstanceMethod/MoveInstanceMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/move/moveInstanceMethod/MoveInstanceMethodProcessor.java @@ -500,8 +500,13 @@ public class MoveInstanceMethodProcessor extends BaseRefactoringProcessor{ } } } - //Target is a field, replace target.m -> m - qualifier.delete(); + if (expression instanceof PsiMethodReferenceExpression) { + qualifier.replace(factory.createExpressionFromText("this", null)); + } + else { + //Target is a field, replace target.m -> m + qualifier.delete(); + } return; } if (myTargetVariable.equals(resolved)) { diff --git a/java/java-tests/testData/refactoring/moveInstanceMethod/MethodReferenceWithThisTarget.java b/java/java-tests/testData/refactoring/moveInstanceMethod/MethodReferenceWithThisTarget.java new file mode 100644 index 000000000000..b853a5c5263a --- /dev/null +++ b/java/java-tests/testData/refactoring/moveInstanceMethod/MethodReferenceWithThisTarget.java @@ -0,0 +1,12 @@ +import java.util.function.Consumer; + +class Source { + public void foo(Destination destination) { + Consumer doSomething = destination::doSomething; + } +} + +class Destination { + public void doSomething(String s) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/moveInstanceMethod/MethodReferenceWithThisTarget.java.after b/java/java-tests/testData/refactoring/moveInstanceMethod/MethodReferenceWithThisTarget.java.after new file mode 100644 index 000000000000..ad44fc5bc6f0 --- /dev/null +++ b/java/java-tests/testData/refactoring/moveInstanceMethod/MethodReferenceWithThisTarget.java.after @@ -0,0 +1,13 @@ +import java.util.function.Consumer; + +class Source { +} + +class Destination { + public void doSomething(String s) { + } + + public void foo() { + Consumer doSomething = this::doSomething; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/moveMethod/MoveInstanceMethodTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/moveMethod/MoveInstanceMethodTest.java index 1304be50a9c6..abd2fe12a9a8 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/moveMethod/MoveInstanceMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/moveMethod/MoveInstanceMethodTest.java @@ -116,6 +116,10 @@ public class MoveInstanceMethodTest extends LightRefactoringTestCase { doTest(false, 0); } + public void testMethodReferenceWithThisTarget() { + BaseRefactoringProcessor.ConflictsInTestsException.withIgnoredConflicts(() -> doTest(true, 0)); + } + public void testMethodReferenceToExpandToLambda() { BaseRefactoringProcessor.ConflictsInTestsException.withIgnoredConflicts(() -> doTest(true, 1)); }