mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
process overloads to create method by method reference (IDEA-172193)
This commit is contained in:
@@ -22,10 +22,10 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -46,7 +46,7 @@ public class CreateMethodFromMethodReferenceFix extends CreateFromUsageBaseFix {
|
||||
protected boolean isAvailableImpl(int offset) {
|
||||
final PsiMethodReferenceExpression call = getMethodReference();
|
||||
if (call == null || !call.isValid()) return false;
|
||||
final PsiType functionalInterfaceType = call.getFunctionalInterfaceType();
|
||||
final PsiType functionalInterfaceType = getFunctionalExpressionType(call);
|
||||
if (functionalInterfaceType == null ||
|
||||
LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType) == null){
|
||||
return false;
|
||||
@@ -62,6 +62,16 @@ public class CreateMethodFromMethodReferenceFix extends CreateFromUsageBaseFix {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static PsiType getFunctionalExpressionType(PsiMethodReferenceExpression ref) {
|
||||
PsiType functionalInterfaceType = ref.getFunctionalInterfaceType();
|
||||
if (functionalInterfaceType != null) return functionalInterfaceType;
|
||||
Ref<PsiType> type = new Ref<>();
|
||||
if (LambdaUtil.processParentOverloads(ref, (fType) -> type.set(fType))) {
|
||||
return type.get();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PsiElement getElement() {
|
||||
final PsiMethodReferenceExpression call = getMethodReference();
|
||||
@@ -125,7 +135,7 @@ public class CreateMethodFromMethodReferenceFix extends CreateFromUsageBaseFix {
|
||||
|
||||
final PsiElement context = PsiTreeUtil.getParentOfType(expression, PsiClass.class, PsiMethod.class);
|
||||
|
||||
final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
|
||||
final PsiType functionalInterfaceType = getFunctionalExpressionType(expression);
|
||||
final PsiClassType.ClassResolveResult classResolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
|
||||
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(classResolveResult);
|
||||
LOG.assertTrue(interfaceMethod != null);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// "Create method 'fooBar'" "true"
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class MyTest {
|
||||
public void testMethod() {
|
||||
overloadOtherParam("anything", this::fooBar);
|
||||
}
|
||||
|
||||
private void fooBar(String s) {
|
||||
<caret>
|
||||
}
|
||||
|
||||
|
||||
private void overloadOtherParam(String anything, Consumer<String> consumer) {
|
||||
}
|
||||
|
||||
private void overloadOtherParam(int anything, Consumer<String> consumer) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// "Create method 'fooBar'" "true"
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class MyTest {
|
||||
public void testMethod() {
|
||||
overloadOtherParam("anything", this::foo<caret>Bar);
|
||||
}
|
||||
|
||||
|
||||
private void overloadOtherParam(String anything, Consumer<String> consumer) {
|
||||
}
|
||||
|
||||
private void overloadOtherParam(int anything, Consumer<String> consumer) {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user