mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
functional interface suggester: check return types and try to infer
This commit is contained in:
@@ -93,14 +93,17 @@ public class FunctionalInterfaceSuggester {
|
||||
return null;
|
||||
}
|
||||
|
||||
final PsiType[] left = new PsiType[parameters.length];
|
||||
final PsiType[] right = new PsiType[parameters.length];
|
||||
final PsiType[] left = new PsiType[parameters.length + 1];
|
||||
final PsiType[] right = new PsiType[parameters.length + 1];
|
||||
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
left[i] = interfaceMethodParameters[i].getType();
|
||||
right[i] = parameters[i].getType();
|
||||
}
|
||||
|
||||
left[parameters.length] = method.getReturnType();
|
||||
right[parameters.length] = interfaceMethod.getReturnType();
|
||||
|
||||
final PsiTypeParameter[] typeParameters = aClass.getTypeParameters();
|
||||
final PsiSubstitutor substitutor = PsiResolveHelper.SERVICE.getInstance(aClass.getProject())
|
||||
.inferTypeArguments(typeParameters, left, right, PsiUtil.getLanguageLevel(method));
|
||||
@@ -203,15 +206,19 @@ public class FunctionalInterfaceSuggester {
|
||||
final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters();
|
||||
PsiParameter[] functionalExprParameters;
|
||||
int offset = 0;
|
||||
final PsiType[] left = new PsiType[parameters.length];
|
||||
final PsiType[] right = new PsiType[parameters.length];
|
||||
final PsiType[] left;
|
||||
final PsiType[] right;
|
||||
if (expression instanceof PsiLambdaExpression && ((PsiLambdaExpression)expression).hasFormalParameterTypes()) {
|
||||
left = new PsiType[parameters.length];
|
||||
right = new PsiType[parameters.length];
|
||||
functionalExprParameters = ((PsiLambdaExpression)expression).getParameterList().getParameters();
|
||||
if (parameters.length != functionalExprParameters.length) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (expression instanceof PsiMethodReferenceExpression) {
|
||||
left = new PsiType[parameters.length + 1];
|
||||
right = new PsiType[parameters.length + 1];
|
||||
final PsiMethod method = getTargetMethod((PsiMethodReferenceExpression)expression, qualifierType, parameters, left, right);
|
||||
if (method == null) {
|
||||
return null;
|
||||
@@ -220,6 +227,9 @@ public class FunctionalInterfaceSuggester {
|
||||
if (PsiMethodReferenceUtil.isStaticallyReferenced((PsiMethodReferenceExpression)expression) && !method.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
offset = 1;
|
||||
}
|
||||
|
||||
left[parameters.length] = method.getReturnType();
|
||||
right[parameters.length] = interfaceMethod.getReturnType();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import java.util.function.Supplier;
|
||||
|
||||
class Test {
|
||||
void bar() {
|
||||
foo(new Supplier<String>() {
|
||||
public String get() {
|
||||
String s = "";
|
||||
System.out.println(s);
|
||||
return s;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void foo(Supplier<String> anObject) {
|
||||
|
||||
String s = anObject.get();
|
||||
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -1,9 +1,9 @@
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.function.Function;
|
||||
|
||||
class Test {
|
||||
void bar() {
|
||||
foo(1, new IntPredicate() {
|
||||
public boolean test(int i) {
|
||||
foo(1, new Function<Integer,Boolean>() {
|
||||
public boolean apply(Integer i) {
|
||||
if (i > 0) {
|
||||
System.out.println(i);
|
||||
System.out.println(i);
|
||||
@@ -14,9 +14,9 @@ class Test {
|
||||
});
|
||||
}
|
||||
|
||||
void foo(int i, IntPredicate anObject) {
|
||||
void foo(int i, Function<Integer, Boolean> anObject) {
|
||||
|
||||
if (anObject.test(i)) return;
|
||||
if (anObject.apply(i)) return;
|
||||
|
||||
System.out.println("Hi");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
class Test {
|
||||
void bar() {
|
||||
foo();
|
||||
}
|
||||
|
||||
void foo() {
|
||||
<selection>
|
||||
String s = "";
|
||||
System.out.println(s);
|
||||
</selection>
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
@FunctionalInterface
|
||||
interface I<T> {
|
||||
void foo(T t);
|
||||
T foo(T t);
|
||||
}
|
||||
|
||||
class Foo {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
@FunctionalInterface
|
||||
interface I<T> {
|
||||
void foo(T t);
|
||||
T foo(T t);
|
||||
}
|
||||
|
||||
class Foo {
|
||||
|
||||
@@ -33,6 +33,10 @@ public class IntroduceFunctionalParameterTest extends LightRefactoringTestCase
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFunction() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testIntConsumerFromIfStatement() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user