[java-inspections] IDEA-301083 "Replace lambda with method reference" doesn't work for constructor calls in nested Enum

Copied enum was created always at top-level. In fact, as we copy the whole file anyway, there's no need to do something additional with enum. Just find it in copy and return.

GitOrigin-RevId: d85c21acfc879c02be0ef85eff4264faf0c99ffb
This commit is contained in:
Tagir Valeev
2022-09-05 20:28:06 +02:00
committed by intellij-monorepo-bot
parent a47cf9f44e
commit 98e7ca52f1
3 changed files with 48 additions and 16 deletions

View File

@@ -806,22 +806,8 @@ public final class LambdaUtil {
public static PsiCall copyTopLevelCall(@NotNull PsiCall call) {
if (call instanceof PsiEnumConstant) {
PsiClass containingClass = ((PsiEnumConstant)call).getContainingClass();
if (containingClass == null) {
return null;
}
String enumName = containingClass.getName();
if (enumName == null) {
return null;
}
PsiMethod resolveMethod = call.resolveMethod();
if (resolveMethod == null) {
return null;
}
PsiElement contextFile = call.getContainingFile().copy();
PsiClass anEnum = (PsiClass)contextFile.add(JavaPsiFacade.getElementFactory(call.getProject()).createEnum(enumName));
anEnum.add(resolveMethod);
return (PsiCall)anEnum.add(call);
PsiFile contextFile = (PsiFile)call.getContainingFile().copy();
return PsiTreeUtil.findSameElementInCopy(call, contextFile);
}
PsiElement expressionForType = call;
while (true) {

View File

@@ -0,0 +1,23 @@
// "Replace lambda with method reference" "true-preview"
import java.util.function.Function;
public abstract class x {
public enum E {
A(Object::toString),
B(n -> { return n.toString();}),
C(Number::toString);
E(Function<Number, Object> iFunc) {}
}
public void dothis(Function<Number, Object> iFunc) { }
public void trythis() {
dothis(n -> n.toString());
}
public void trythis2() {
dothis(n -> { return n.toString(); });
}
}

View File

@@ -0,0 +1,23 @@
// "Replace lambda with method reference" "true-preview"
import java.util.function.Function;
public abstract class x {
public enum E {
A(n -> <caret>n.toString()),
B(n -> { return n.toString();}),
C(Number::toString);
E(Function<Number, Object> iFunc) {}
}
public void dothis(Function<Number, Object> iFunc) { }
public void trythis() {
dothis(n -> n.toString());
}
public void trythis2() {
dothis(n -> { return n.toString(); });
}
}