mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-21 05:51:25 +07:00
lambda -> method ref: insert cast when resulted ref is ambiguous (IDEA-100385)
This commit is contained in:
@@ -251,12 +251,21 @@ public class LambdaCanBeMethReferenceInspection extends BaseJavaLocalInspectionT
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
|
||||
if (lambdaExpression == null) return;
|
||||
final String methodRefText = createMethodReferenceText(element, lambdaExpression.getFunctionalInterfaceType());
|
||||
final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
|
||||
final String methodRefText = createMethodReferenceText(element, functionalInterfaceType);
|
||||
|
||||
if (methodRefText != null) {
|
||||
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
|
||||
final PsiExpression psiExpression =
|
||||
JavaPsiFacade.getElementFactory(project).createExpressionFromText(methodRefText, lambdaExpression);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(lambdaExpression.replace(psiExpression));
|
||||
factory.createExpressionFromText(methodRefText, lambdaExpression);
|
||||
PsiElement replace = lambdaExpression.replace(psiExpression);
|
||||
if (((PsiMethodReferenceExpression)replace).getFunctionalInterfaceType() == null) { //ambiguity
|
||||
final PsiTypeCastExpression cast = (PsiTypeCastExpression)factory.createExpressionFromText("(A)a", replace);
|
||||
cast.getCastType().replace(factory.createTypeElement(functionalInterfaceType));
|
||||
cast.getOperand().replace(replace);
|
||||
replace = replace.replace(cast);
|
||||
}
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.*;
|
||||
class IDEA100385 {
|
||||
void foo(N<Double> n, List<Double> l){
|
||||
n.forEach((DoubleConsumer) l::add);
|
||||
}
|
||||
static interface N<E> {
|
||||
default void forEach(DoubleConsumer consumer) {
|
||||
}
|
||||
void forEach(Consumer<? super E> consumer);
|
||||
}
|
||||
|
||||
interface DoubleConsumer {
|
||||
void _(double d);
|
||||
}
|
||||
|
||||
interface Consumer<T> {
|
||||
public void accept(T t);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.*;
|
||||
class IDEA100385 {
|
||||
void foo(N<Double> n, List<Double> l){
|
||||
n.forEach((double e) -> {
|
||||
l.ad<caret>d(e);
|
||||
});
|
||||
}
|
||||
static interface N<E> {
|
||||
default void forEach(DoubleConsumer consumer) {
|
||||
}
|
||||
void forEach(Consumer<? super E> consumer);
|
||||
}
|
||||
|
||||
interface DoubleConsumer {
|
||||
void _(double d);
|
||||
}
|
||||
|
||||
interface Consumer<T> {
|
||||
public void accept(T t);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user