anonym -> functional: allow cast to resolve overload resolution

This commit is contained in:
Anna Kozlova
2016-09-06 11:55:57 +03:00
parent 8b549a9397
commit b8d5b9ab09
3 changed files with 18 additions and 46 deletions

View File

@@ -135,11 +135,6 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
}
public static boolean hasForbiddenRefsInsideBody(PsiMethod method, PsiAnonymousClass aClass) {
final PsiType inferredType = getInferredType(aClass, method);
if (inferredType == null) {
return true;
}
final ForbiddenRefsChecker checker = new ForbiddenRefsChecker(method, aClass);
final PsiCodeBlock body = method.getBody();
LOG.assertTrue(body != null);
@@ -147,44 +142,6 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
return checker.hasForbiddenRefs();
}
private static PsiType getInferredType(PsiAnonymousClass aClass, PsiMethod method) {
final PsiExpression expression = (PsiExpression)aClass.getParent();
final PsiType psiType = PsiTypesUtil.getExpectedTypeByParent(expression);
if (psiType != null) {
return psiType;
}
PsiExpression topExpr = expression;
while (topExpr.getParent() instanceof PsiParenthesizedExpression) {
topExpr = (PsiExpression)topExpr.getParent();
}
final PsiCall call = LambdaUtil.treeWalkUp(topExpr);
if (call != null && call.resolveMethod() != null) {
final int offsetInTopCall = aClass.getTextRange().getStartOffset() - call.getTextRange().getStartOffset();
PsiCall copyCall = LambdaUtil.copyTopLevelCall(call);
if (copyCall == null) return null;
final PsiAnonymousClass classArg = PsiTreeUtil.getParentOfType(copyCall.findElementAt(offsetInTopCall), PsiAnonymousClass.class);
if (classArg != null) {
PsiExpression lambda = JavaPsiFacade.getElementFactory(aClass.getProject())
.createExpressionFromText(ReplaceWithLambdaFix.composeLambdaText(method), expression);
lambda = (PsiExpression)classArg.getParent().replace(lambda);
((PsiLambdaExpression)lambda).getBody().replace(method.getBody());
final PsiType interfaceType;
if (copyCall.resolveMethod() == null) {
interfaceType = null;
}
else {
interfaceType = ((PsiLambdaExpression)lambda).getFunctionalInterfaceType();
}
return interfaceType;
}
}
return PsiType.NULL;
}
public static boolean canBeConvertedToLambda(PsiAnonymousClass aClass,
boolean acceptParameterizedFunctionTypes,
@NotNull Set<String> ignoredRuntimeAnnotations) {
@@ -213,9 +170,9 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
final PsiMethod method = methods[0];
return method.getBody() != null &&
method.getDocComment() == null &&
!hasForbiddenRefsInsideBody(method, aClass) &&
!hasRuntimeAnnotations(method, ignoredRuntimeAnnotations) &&
!method.hasModifierProperty(PsiModifier.SYNCHRONIZED);
!method.hasModifierProperty(PsiModifier.SYNCHRONIZED) &&
!hasForbiddenRefsInsideBody(method, aClass);
}
}
}

View File

@@ -0,0 +1,15 @@
// "Replace with lambda" "true"
import java.util.function.Function;
abstract class LambdaConvert {
public abstract <T> T query(Function<Double, T> rse);
public void with() {
add(query((Function<Double, String>) resultSet -> ""));
}
public void add(String s) {}
public void add(Integer i) {}
}

View File

@@ -1,4 +1,4 @@
// "Replace with lambda" "false"
// "Replace with lambda" "true"
import java.util.function.Function;