[java-inspections] IDEA-328239 Casting with conjunction

- fix formatting

GitOrigin-RevId: 74955a0e5ddc5d5894455fcbfb3bec813d081b84
This commit is contained in:
Mikhail Pyltsin
2023-08-30 13:41:47 +02:00
committed by intellij-monorepo-bot
parent 0df2261a21
commit bed8faeb52
2 changed files with 40 additions and 62 deletions

View File

@@ -37,9 +37,9 @@ public final class LambdaUtil {
return getFunctionalInterfaceReturnType(expr.getFunctionalInterfaceType());
}
/**
/**
* @return substituted return type of method which corresponds to the {@code functionalInterfaceType} SAM,
* null when {@code functionalInterfaceType} doesn't correspond to functional interface type
* null when {@code functionalInterfaceType} doesn't correspond to functional interface type
*/
public static @Nullable PsiType getFunctionalInterfaceReturnType(@Nullable PsiType functionalInterfaceType) {
PsiType functionalType = normalizeFunctionalType(functionalInterfaceType);
@@ -65,8 +65,7 @@ public final class LambdaUtil {
}
public static PsiMethod getFunctionalInterfaceMethod(@Nullable PsiElement element) {
return element instanceof PsiFunctionalExpression ? getFunctionalInterfaceMethod(
((PsiFunctionalExpression)element).getFunctionalInterfaceType()) : null;
return element instanceof PsiFunctionalExpression ? getFunctionalInterfaceMethod(((PsiFunctionalExpression)element).getFunctionalInterfaceType()) : null;
}
public static @Nullable PsiMethod getFunctionalInterfaceMethod(@NotNull PsiClassType.ClassResolveResult result) {
@@ -321,15 +320,14 @@ public final class LambdaUtil {
if (method != null) {
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) return null;
return TypeConversionUtil.getSuperClassSubstitutor(containingClass, psiClass, PsiSubstitutor.EMPTY)
.substitute(method.getReturnType());
return TypeConversionUtil.getSuperClassSubstitutor(containingClass, psiClass, PsiSubstitutor.EMPTY).substitute(method.getReturnType());
}
else {
return null;
}
}
public static @Nullable PsiMethod getMethod(PsiClass psiClass, MethodSignature methodSignature) {
private static @Nullable PsiMethod getMethod(PsiClass psiClass, MethodSignature methodSignature) {
if (methodSignature instanceof MethodSignatureBackedByPsiMethod) {
return ((MethodSignatureBackedByPsiMethod)methodSignature).getMethod();
}
@@ -394,9 +392,7 @@ public final class LambdaUtil {
else if (parent instanceof PsiVariable) {
return ((PsiVariable)parent).getType();
}
else if (parent instanceof PsiAssignmentExpression &&
expression instanceof PsiExpression &&
!PsiUtil.isOnAssignmentLeftHand((PsiExpression)expression)) {
else if (parent instanceof PsiAssignmentExpression && expression instanceof PsiExpression && !PsiUtil.isOnAssignmentLeftHand((PsiExpression)expression)) {
final PsiExpression lExpression = ((PsiAssignmentExpression)parent).getLExpression();
return lExpression.getType();
}
@@ -459,17 +455,14 @@ public final class LambdaUtil {
return null;
}
public static boolean processParentOverloads(PsiFunctionalExpression functionalExpression,
final Consumer<? super PsiType> overloadProcessor) {
public static boolean processParentOverloads(PsiFunctionalExpression functionalExpression, final Consumer<? super PsiType> overloadProcessor) {
LOG.assertTrue(PsiTypesUtil.getExpectedTypeByParent(functionalExpression) == null);
PsiElement parent = functionalExpression.getParent();
PsiElement expr = functionalExpression;
while (parent instanceof PsiParenthesizedExpression || parent instanceof PsiConditionalExpression) {
if (parent instanceof PsiConditionalExpression &&
((PsiConditionalExpression)parent).getThenExpression() != expr &&
((PsiConditionalExpression)parent).getElseExpression() != expr) {
break;
}
((PsiConditionalExpression)parent).getElseExpression() != expr) break;
expr = parent;
parent = parent.getParent();
}
@@ -488,7 +481,7 @@ public final class LambdaUtil {
if (gParent instanceof PsiMethodCallExpression) {
results = ((PsiMethodCallExpression)gParent).getMethodExpression().multiResolve(true);
}
else if (gParent instanceof PsiConstructorCall) {
else if (gParent instanceof PsiConstructorCall){
results = getConstructorCandidates((PsiConstructorCall)gParent);
}
@@ -498,8 +491,7 @@ public final class LambdaUtil {
Computable<PsiType> computeType = () -> getSubstitutedType(functionalExpression, true, lambdaIdx, result);
final PsiType functionalExpressionType = results.length == 1
? computeType.compute()
: MethodCandidateInfo.ourOverloadGuard.doPreventingRecursion(functionalExpression,
false, computeType);
: MethodCandidateInfo.ourOverloadGuard.doPreventingRecursion(functionalExpression, false, computeType);
if (functionalExpressionType != null && types.add(functionalExpressionType)) {
overloadProcessor.consume(functionalExpressionType);
}
@@ -532,6 +524,7 @@ public final class LambdaUtil {
return null;
}
private static @Nullable PsiType extractFunctionalConjunct(PsiIntersectionType type) {
PsiType conjunct = null;
PsiClass commonClass = null;
@@ -624,17 +617,17 @@ public final class LambdaUtil {
}
public static boolean isLambdaSubsignature(@NotNull PsiMethod method1,
@NotNull PsiType type1,
@NotNull PsiMethod method2,
@NotNull PsiType type2) {
@NotNull PsiType type1,
@NotNull PsiMethod method2,
@NotNull PsiType type2) {
MethodSignature signature1 = method1.getSignature(PsiSubstitutor.EMPTY);
MethodSignature signature2 = method2.getSignature(PsiSubstitutor.EMPTY);
if (!MethodSignatureUtil.isSubsignature(signature1, signature2) &&
!MethodSignatureUtil.isSubsignature(signature2, signature1)) {
return false;
}
signature1 = getSignatureWithSubstitutors(method1, type1);
signature2 = getSignatureWithSubstitutors(method2, type2);
signature1 = getSignatureWithSubstitutors(method1, type1);
signature2 = getSignatureWithSubstitutors(method2, type2);
if (!MethodSignatureUtil.isSubsignature(signature1, signature2) &&
!MethodSignatureUtil.isSubsignature(signature2, signature1)) {
return false;
@@ -666,8 +659,7 @@ public final class LambdaUtil {
final int finalLambdaIdx;
if (resolve.isVarArgs() && lambdaIdx >= parameters.length) {
finalLambdaIdx = parameters.length - 1;
}
else {
} else {
finalLambdaIdx = lambdaIdx;
}
return finalLambdaIdx;
@@ -776,12 +768,11 @@ public final class LambdaUtil {
* If called to check applicability or anything which may be required to determine type, outer caching prevention is required
* For example, {@link com.intellij.psi.impl.source.tree.java.PsiLambdaExpressionImpl#isAcceptable(PsiType, PsiMethod)}:
* {@code return LambdaUtil.performWithTargetType(this, leftType, () ->
* LambdaUtil.checkReturnTypeCompatible(this, substitutor.substitute(methodReturnType)) == null);}
* <p>
* LambdaUtil.checkReturnTypeCompatible(this, substitutor.substitute(methodReturnType)) == null);}
*
* otherwise calls to {@code expr#getType()} inside the method may lead to infinite recursion
*/
public static Map<PsiElement, @Nls String> checkReturnTypeCompatible(PsiLambdaExpression lambdaExpression,
PsiType functionalInterfaceReturnType) {
public static Map<PsiElement, @Nls String> checkReturnTypeCompatible(PsiLambdaExpression lambdaExpression, PsiType functionalInterfaceReturnType) {
Map<PsiElement, @Nls String> errors = new LinkedHashMap<>();
if (PsiTypes.voidType().equals(functionalInterfaceReturnType)) {
final PsiElement body = lambdaExpression.getBody();
@@ -880,8 +871,7 @@ public final class LambdaUtil {
}
}
if ((parent instanceof PsiConditionalExpression || parent instanceof PsiSwitchExpression) &&
!PsiPolyExpressionUtil.isPolyExpression((PsiExpression)parent)) {
if ((parent instanceof PsiConditionalExpression || parent instanceof PsiSwitchExpression) && !PsiPolyExpressionUtil.isPolyExpression((PsiExpression)parent)) {
break;
}
@@ -901,9 +891,7 @@ public final class LambdaUtil {
top = psiCall;
if (top instanceof PsiExpression && PsiPolyExpressionUtil.isPolyExpression((PsiExpression)top)) {
parent =
PsiTreeUtil.getParentOfType(parent.getParent(), PsiExpressionList.class, PsiLambdaExpression.class, PsiAssignmentExpression.class,
PsiCodeBlock.class);
parent = PsiTreeUtil.getParentOfType(parent.getParent(), PsiExpressionList.class, PsiLambdaExpression.class, PsiAssignmentExpression.class, PsiCodeBlock.class);
}
else {
break;
@@ -962,9 +950,7 @@ public final class LambdaUtil {
});
}
public static <T> T performWithTargetType(@NotNull PsiElement element,
@NotNull PsiType targetType,
@NotNull Supplier<? extends T> producer) {
public static <T> T performWithTargetType(@NotNull PsiElement element, @NotNull PsiType targetType, @NotNull Supplier<? extends T> producer) {
return ThreadLocalTypes.performWithTypes(types -> {
types.forceType(element, targetType);
return producer.get();
@@ -974,7 +960,7 @@ public final class LambdaUtil {
/**
* Generate lambda text for single argument expression lambda
*
* @param variable lambda sole argument
* @param variable lambda sole argument
* @param expression lambda body (expression)
* @return lambda text
*/
@@ -1027,8 +1013,7 @@ public final class LambdaUtil {
if (function == null) return false;
JavaResolveResult resultCopy = copyCall.resolveMethodGenerics();
if (!oldTarget.getManager().areElementsEquivalent(resultCopy.getElement(), oldTarget)) return false;
String copyMessage =
resultCopy instanceof MethodCandidateInfo ? ((MethodCandidateInfo)resultCopy).getInferenceErrorMessage() : null;
String copyMessage = resultCopy instanceof MethodCandidateInfo ? ((MethodCandidateInfo)resultCopy).getInferenceErrorMessage() : null;
if (!Objects.equals(origErrorMessage, copyMessage)) return false;
if (function instanceof PsiFunctionalExpression) {
PsiType functionalType = ((PsiFunctionalExpression)function).getFunctionalInterfaceType();
@@ -1073,8 +1058,7 @@ public final class LambdaUtil {
* lazy computed for lambdas in invocation context only.
* Replacement evaluated to {@code null} is treated as invalid overload
*/
public static boolean isSafeLambdaReplacement(@NotNull PsiLambdaExpression lambda,
@NotNull Supplier<? extends PsiExpression> newFunctionSupplier) {
public static boolean isSafeLambdaReplacement(@NotNull PsiLambdaExpression lambda, @NotNull Supplier<? extends PsiExpression> newFunctionSupplier) {
return isSafeLambdaReplacement(lambda, l -> {
PsiExpression replacement = newFunctionSupplier.get();
return replacement == null ? null : (PsiExpression)l.replace(replacement);
@@ -1102,9 +1086,9 @@ public final class LambdaUtil {
* when invoked method is not overloaded or all overloads are 'lambda friendly'
*
* <p>
* Value-compatible lambda like {@code () -> foo() == true} can be converted to value-compatible AND void-compatible
* {@code () -> foo()} during simplification. This could lead to ambiguity during containing method call resolution and thus
* to the errors after applying the suggestion.
* Value-compatible lambda like {@code () -> foo() == true} can be converted to value-compatible AND void-compatible
* {@code () -> foo()} during simplification. This could lead to ambiguity during containing method call resolution and thus
* to the errors after applying the suggestion.
* </p>
*
* @param lambda a lambda whose body is going to be replaced
@@ -1112,8 +1096,7 @@ public final class LambdaUtil {
* lazy computed for lambdas in invocation context only.
* Replacement evaluated to {@code null} is treated as invalid overload
*/
public static boolean isSafeLambdaBodyReplacement(@NotNull PsiLambdaExpression lambda,
@NotNull Supplier<? extends PsiElement> newBodySupplier) {
public static boolean isSafeLambdaBodyReplacement(@NotNull PsiLambdaExpression lambda, @NotNull Supplier<? extends PsiElement> newBodySupplier) {
return isSafeLambdaReplacement(lambda, l -> {
PsiElement oldBody = l.getBody();
PsiElement newBody = newBodySupplier.get();
@@ -1126,16 +1109,13 @@ public final class LambdaUtil {
/**
* {@link #isSafeLambdaBodyReplacement(PsiLambdaExpression, Supplier)} overload to test the same lambda body,
* but with only return value {@code expression} changed to {@code replacement}
*
* @param lambdaReturnExpression a return expression inside lambda body
* @param replacement a replacement for return expression
* @param replacement a replacement for return expression
*/
public static boolean isSafeLambdaReturnValueReplacement(@NotNull PsiExpression lambdaReturnExpression,
@NotNull PsiExpression replacement) {
if (lambdaReturnExpression.getParent() instanceof PsiReturnStatement ||
lambdaReturnExpression.getParent() instanceof PsiLambdaExpression) {
PsiLambdaExpression lambdaExpression =
PsiTreeUtil.getParentOfType(lambdaReturnExpression, PsiLambdaExpression.class, true, PsiMethod.class);
if (lambdaReturnExpression.getParent() instanceof PsiReturnStatement || lambdaReturnExpression.getParent() instanceof PsiLambdaExpression) {
PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(lambdaReturnExpression, PsiLambdaExpression.class, true, PsiMethod.class);
if (lambdaExpression != null &&
!isSafeLambdaBodyReplacement(lambdaExpression, () -> {
PsiLambdaExpression lambdaExpression1 = PsiTreeUtil.getParentOfType(lambdaReturnExpression, PsiLambdaExpression.class);
@@ -1172,7 +1152,7 @@ public final class LambdaUtil {
final String callableWithExpectedType = "(java.util.concurrent.Callable<" + canonicalText + ">)() -> x";
PsiTypeCastExpression typeCastExpr = (PsiTypeCastExpression)JavaPsiFacade.getElementFactory(expression.getProject())
.createExpressionFromText(callableWithExpectedType, expression);
.createExpressionFromText(callableWithExpectedType, expression);
PsiLambdaExpression lambdaExpression = (PsiLambdaExpression)typeCastExpr.getOperand();
LOG.assertTrue(lambdaExpression != null);
PsiElement body = lambdaExpression.getBody();
@@ -1182,7 +1162,6 @@ public final class LambdaUtil {
/**
* Returns true if given lambda captures any variable or "this" reference.
*
* @param lambda lambda to check
* @return true if given lambda captures any variable or "this" reference.
*/
@@ -1261,8 +1240,7 @@ public final class LambdaUtil {
PsiType psiType;
if (origTypeElement != null && !origTypeElement.isInferredType()) {
psiType = origTypeElement.getType();
}
else {
} else {
psiType = substitutor.substitute(parameters[i].getType());
if (!PsiTypesUtil.isDenotableType(psiType, lambdaExpression)) return null;
}
@@ -1301,7 +1279,7 @@ public final class LambdaUtil {
/**
* @return {@link PsiClass} or {@link PsiLambdaExpression} which contains passed {@code element}.
* {@link PsiAnonymousClass} is skipped if {@code element} is located in the corresponding expression list
* {@link PsiAnonymousClass} is skipped if {@code element} is located in the corresponding expression list
*/
@Nullable
public static PsiElement getContainingClassOrLambda(@NotNull PsiElement element) {

View File

@@ -7,13 +7,13 @@ class Test {
}
public static void main(String... args) {
var shouldCompile = (Consumer<Integer> & IntConsumerAdapter1)
i -> System.out.println("Cons3uming dd" + i);
i -> System.out.println("Consuming" + i);
System.out.println(shouldCompile.getClass());
shouldCompile.accept(42);
shouldCompile.accept(Integer.valueOf(52));
var shouldNotCompile = (Consumer<Integer> & IntConsumerAdapter2)
<error descr="No target method found">i -> System.out.println("Consumins2gs " + i)</error>;
<error descr="No target method found">i -> System.out.println("Consuming " + i)</error>;
shouldNotCompile.accept(42);
shouldNotCompile.accept(Integer.valueOf(52));
}