[java] make method static: adapt to static methods in anonymous classes, method references (IDEA-293090)

GitOrigin-RevId: c7a58c6e06726607fb5e97d74624a468d684a8ec
This commit is contained in:
Anna Kozlova
2022-04-28 12:09:04 +00:00
committed by intellij-monorepo-bot
parent 106750692a
commit 49b2bd2ca6
4 changed files with 33 additions and 18 deletions
@@ -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;
}
@@ -0,0 +1,11 @@
class MyTest {
{
new Runnable() {
private static void print() {}
@Override
public void run() {
Runnable r = () -> print();
}
}
}
}
@@ -0,0 +1,11 @@
class MyTest {
{
new Runnable() {
private void pri<caret>nt() {}
@Override
public void run() {
Runnable r = this::print;
}
}
}
}
@@ -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);
}