diff --git a/java/java-impl-refactorings/src/com/intellij/refactoring/makeStatic/MakeMethodStaticProcessor.java b/java/java-impl-refactorings/src/com/intellij/refactoring/makeStatic/MakeMethodStaticProcessor.java index 4fca8cf54e93..d20ccfca1239 100644 --- a/java/java-impl-refactorings/src/com/intellij/refactoring/makeStatic/MakeMethodStaticProcessor.java +++ b/java/java-impl-refactorings/src/com/intellij/refactoring/makeStatic/MakeMethodStaticProcessor.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 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. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.refactoring.makeStatic; import com.intellij.codeInsight.AnnotationUtil; @@ -304,7 +304,7 @@ public class MakeMethodStaticProcessor extends MakeMethodOrClassStaticProcessor< final PsiClass memberClass = myMember.getContainingClass(); if (instanceRef == null || instanceRef instanceof PsiSuperExpression) { - PsiClass contextClass = PsiTreeUtil.getParentOfType(element, PsiClass.class); + PsiClass contextClass = PsiTreeUtil.getParentOfType(methodRef, PsiClass.class); if (!InheritanceUtil.isInheritorOrSelf(contextClass, memberClass, true)) { instanceRef = factory.createExpressionFromText(memberClass.getQualifiedName() + ".this", null); } else { @@ -313,7 +313,7 @@ public class MakeMethodStaticProcessor extends MakeMethodOrClassStaticProcessor< newQualifier = null; } else { - newQualifier = factory.createReferenceExpression(memberClass); + newQualifier = memberClass == null || memberClass instanceof PsiAnonymousClass ? null : factory.createReferenceExpression(memberClass); } if (mySettings.getNewParametersNumber() > 1) { @@ -386,6 +386,9 @@ public class MakeMethodStaticProcessor extends MakeMethodOrClassStaticProcessor< } private boolean needLambdaConversion(PsiMethodReferenceExpression methodRef) { + if (myMember.getContainingClass() instanceof PsiAnonymousClass) { + return true; + } if (mySettings.isMakeFieldParameters()) { return true; } diff --git a/java/java-tests/testData/refactoring/makeMethodStatic/afterMethodReferenceInAnonymousClass.java b/java/java-tests/testData/refactoring/makeMethodStatic/afterMethodReferenceInAnonymousClass.java new file mode 100644 index 000000000000..e82c03c3adbe --- /dev/null +++ b/java/java-tests/testData/refactoring/makeMethodStatic/afterMethodReferenceInAnonymousClass.java @@ -0,0 +1,11 @@ +class MyTest { + { + new Runnable() { + private static void print() {} + @Override + public void run() { + Runnable r = () -> print(); + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/makeMethodStatic/beforeMethodReferenceInAnonymousClass.java b/java/java-tests/testData/refactoring/makeMethodStatic/beforeMethodReferenceInAnonymousClass.java new file mode 100644 index 000000000000..8952f380e2bf --- /dev/null +++ b/java/java-tests/testData/refactoring/makeMethodStatic/beforeMethodReferenceInAnonymousClass.java @@ -0,0 +1,11 @@ +class MyTest { + { + new Runnable() { + private void print() {} + @Override + public void run() { + Runnable r = this::print; + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/MakeMethodStaticTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/MakeMethodStaticTest.java index a2ff7254cc45..1e8022b86201 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/MakeMethodStaticTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/MakeMethodStaticTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.refactoring; import com.intellij.JavaTestUtil; @@ -230,6 +216,10 @@ public class MakeMethodStaticTest extends LightRefactoringTestCase { doTest(true, true); } + public void testMethodReferenceInAnonymousClass() { + BaseRefactoringProcessor.ConflictsInTestsException.withIgnoredConflicts(() -> doTest()); + } + private void doTest() { doTest(false); }