mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
make method static: fix method refs in method body
EA-120553 - assert: MakeMethodStaticProcessor.changeSelfUsage
This commit is contained in:
+32
-9
@@ -96,8 +96,25 @@ public class MakeMethodStaticProcessor extends MakeMethodOrClassStaticProcessor<
|
||||
}
|
||||
|
||||
protected void changeSelfUsage(SelfUsageInfo usageInfo) throws IncorrectOperationException {
|
||||
PsiElement parent = usageInfo.getElement().getParent();
|
||||
LOG.assertTrue(parent instanceof PsiMethodCallExpression);
|
||||
PsiElement element = usageInfo.getElement();
|
||||
PsiElement parent = element.getParent();
|
||||
if (element instanceof PsiMethodReferenceExpression) {
|
||||
if (needLambdaConversion((PsiMethodReferenceExpression)element)) {
|
||||
PsiMethodCallExpression methodCallExpression = getMethodCallExpression((PsiMethodReferenceExpression)element);
|
||||
if (methodCallExpression == null) return;
|
||||
parent = methodCallExpression;
|
||||
}
|
||||
else {
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(parent.getProject());
|
||||
PsiClass memberClass = myMember.getContainingClass();
|
||||
LOG.assertTrue(memberClass != null);
|
||||
PsiElement qualifier = ((PsiMethodReferenceExpression)element).getQualifier();
|
||||
LOG.assertTrue(qualifier != null);
|
||||
qualifier.replace(factory.createReferenceExpression(memberClass));
|
||||
return;
|
||||
}
|
||||
}
|
||||
LOG.assertTrue(parent instanceof PsiMethodCallExpression, parent);
|
||||
PsiMethodCallExpression methodCall = (PsiMethodCallExpression) parent;
|
||||
final PsiExpression qualifier = methodCall.getMethodExpression().getQualifierExpression();
|
||||
if (qualifier != null) qualifier.delete();
|
||||
@@ -271,13 +288,9 @@ public class MakeMethodStaticProcessor extends MakeMethodOrClassStaticProcessor<
|
||||
|
||||
PsiReferenceExpression methodRef = (PsiReferenceExpression) element;
|
||||
if (methodRef instanceof PsiMethodReferenceExpression && needLambdaConversion((PsiMethodReferenceExpression)methodRef)) {
|
||||
PsiLambdaExpression lambdaExpression =
|
||||
LambdaRefactoringUtil.convertMethodReferenceToLambda(((PsiMethodReferenceExpression)methodRef), true, true);
|
||||
List<PsiExpression> returnExpressions = LambdaUtil.getReturnExpressions(lambdaExpression);
|
||||
if (returnExpressions.size() != 1) return;
|
||||
PsiExpression expression = returnExpressions.get(0);
|
||||
if (!(expression instanceof PsiMethodCallExpression)) return;
|
||||
methodRef = ((PsiMethodCallExpression)expression).getMethodExpression();
|
||||
PsiMethodCallExpression expression = getMethodCallExpression((PsiMethodReferenceExpression)methodRef);
|
||||
if (expression == null) return;
|
||||
methodRef = expression.getMethodExpression();
|
||||
}
|
||||
PsiElement parent = methodRef.getParent();
|
||||
|
||||
@@ -359,6 +372,16 @@ public class MakeMethodStaticProcessor extends MakeMethodOrClassStaticProcessor<
|
||||
}
|
||||
}
|
||||
|
||||
private static PsiMethodCallExpression getMethodCallExpression(PsiMethodReferenceExpression methodRef) {
|
||||
PsiLambdaExpression lambdaExpression =
|
||||
LambdaRefactoringUtil.convertMethodReferenceToLambda(methodRef, true, true);
|
||||
List<PsiExpression> returnExpressions = LambdaUtil.getReturnExpressions(lambdaExpression);
|
||||
if (returnExpressions.size() != 1) return null;
|
||||
PsiExpression expression = returnExpressions.get(0);
|
||||
if (!(expression instanceof PsiMethodCallExpression)) return null;
|
||||
return (PsiMethodCallExpression)expression;
|
||||
}
|
||||
|
||||
private boolean needLambdaConversion(PsiMethodReferenceExpression methodRef) {
|
||||
if (mySettings.isMakeFieldParameters()) {
|
||||
return true;
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
class IWalker {
|
||||
public static void walk(I e) {
|
||||
e.getChildren().forEach(IWalker::walk);
|
||||
final BiConsumer<IWalker, I> walk = (iWalker, e1) -> IWalker.walk(e1);
|
||||
}
|
||||
|
||||
interface I {
|
||||
List<I> getChildren();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
class IWalker {
|
||||
public void w<caret>alk(I e) {
|
||||
e.getChildren().forEach(this::walk);
|
||||
final BiConsumer<IWalker, I> walk = IWalker::walk;
|
||||
}
|
||||
|
||||
interface I {
|
||||
List<I> getChildren();
|
||||
}
|
||||
}
|
||||
@@ -207,6 +207,10 @@ public class MakeMethodStaticTest extends LightRefactoringTestCase {
|
||||
doTest(false);
|
||||
}
|
||||
|
||||
public void testMethodReferenceInTheSameMethod() {
|
||||
doTest(false);
|
||||
}
|
||||
|
||||
public void testExpandMethodReference() {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user