2 * Copyright 2000-2013 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.psi.impl.source.resolve.graphInference.constraints;
18 import com.intellij.codeInsight.ExceptionUtil;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.psi.*;
21 import com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil;
22 import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession;
23 import com.intellij.psi.impl.source.resolve.graphInference.InferenceVariable;
24 import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil;
25 import com.intellij.psi.util.PsiUtil;
26 import com.intellij.psi.util.TypeConversionUtil;
27 import com.intellij.util.Function;
28 import com.intellij.util.containers.ContainerUtil;
30 import java.util.ArrayList;
31 import java.util.List;
34 public class CheckedExceptionCompatibilityConstraint extends InputOutputConstraintFormula {
35 private static final Logger LOG = Logger.getInstance(CheckedExceptionCompatibilityConstraint.class);
36 private final PsiExpression myExpression;
39 public CheckedExceptionCompatibilityConstraint(PsiExpression expression, PsiType t) {
40 myExpression = expression;
45 public boolean reduce(final InferenceSession session, List<ConstraintFormula> constraints) {
46 if (!PsiPolyExpressionUtil.isPolyExpression(myExpression)) {
49 if (myExpression instanceof PsiParenthesizedExpression) {
50 constraints.add(new CheckedExceptionCompatibilityConstraint(((PsiParenthesizedExpression)myExpression).getExpression(), myT));
53 if (myExpression instanceof PsiConditionalExpression) {
54 final PsiExpression thenExpression = ((PsiConditionalExpression)myExpression).getThenExpression();
55 if (thenExpression != null) {
56 constraints.add(new CheckedExceptionCompatibilityConstraint(thenExpression, myT));
58 final PsiExpression elseExpression = ((PsiConditionalExpression)myExpression).getElseExpression();
59 if (elseExpression != null) {
60 constraints.add(new CheckedExceptionCompatibilityConstraint(elseExpression, myT));
64 if (myExpression instanceof PsiLambdaExpression || myExpression instanceof PsiMethodReferenceExpression) {
65 if (!LambdaUtil.isFunctionalType(myT)) {
66 session.registerIncompatibleErrorMessage(session.getPresentableText(myT) + " is not a functional interface");
70 final PsiType groundTargetType = myExpression instanceof PsiLambdaExpression ? FunctionalInterfaceParameterizationUtil.getGroundTargetType(myT, (PsiLambdaExpression)myExpression, false)
71 : FunctionalInterfaceParameterizationUtil.getGroundTargetType(myT);
72 final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(groundTargetType);
73 if (interfaceMethod == null) {
74 session.registerIncompatibleErrorMessage("No valid function type can be found for " + session.getPresentableText(myT));
78 final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(interfaceMethod, PsiUtil.resolveGenericsClassInType(groundTargetType));
79 if (myExpression instanceof PsiLambdaExpression && !((PsiLambdaExpression)myExpression).hasFormalParameterTypes() ||
80 myExpression instanceof PsiMethodReferenceExpression && !((PsiMethodReferenceExpression)myExpression).isExact()) {
81 for (PsiParameter parameter : interfaceMethod.getParameterList().getParameters()) {
82 final PsiType type = substitutor.substitute(parameter.getType());
83 if (!session.isProperType(type)) {
84 session.registerIncompatibleErrorMessage("Parameter type is not yet inferred: " + session.getPresentableText(type));
90 final PsiType returnType = interfaceMethod.getReturnType();
91 if (myExpression instanceof PsiLambdaExpression || !((PsiMethodReferenceExpression)myExpression).isExact()) {
92 final PsiType type = substitutor.substitute(returnType);
93 if (!session.isProperType(type)) {
94 session.registerIncompatibleErrorMessage("Return type is not yet inferred: " + session.getPresentableText(type));
100 expectedThrownTypes = ContainerUtil.map(interfaceMethod.getThrowsList().getReferencedTypes(),
101 (Function<PsiType, PsiType>)type -> session.substituteWithInferenceVariables(substitutor.substitute(type)));
102 final List<PsiType> expectedNonProperThrownTypes = new ArrayList<>();
103 for (PsiType type : expectedThrownTypes) {
104 if (!session.isProperType(type)) {
105 expectedNonProperThrownTypes.add(type);
109 final List<PsiType> thrownTypes = new ArrayList<>();
110 final PsiElement body = myExpression instanceof PsiLambdaExpression ? ((PsiLambdaExpression)myExpression).getBody() : myExpression;
112 final List<PsiClassType> exceptions = ExceptionUtil.getUnhandledExceptions(new PsiElement[] {body});
113 if (exceptions != null) {
114 thrownTypes.addAll(ContainerUtil.filter(exceptions, type -> !ExceptionUtil.isUncheckedException(type)));
118 if (expectedNonProperThrownTypes.isEmpty()) {
119 for (PsiType thrownType : thrownTypes) {
120 if (!isAddressed(expectedThrownTypes, thrownType)) {
121 session.registerIncompatibleErrorMessage("Unhandled exception: " + session.getPresentableText(thrownType));
126 final ArrayList<PsiType> expectedProperTypes = new ArrayList<>(expectedThrownTypes);
127 expectedProperTypes.removeAll(expectedNonProperThrownTypes);
128 for (PsiType thrownType : thrownTypes) {
129 if (!isAddressed(expectedProperTypes, thrownType)) {
130 for (PsiType expectedNonProperThrownType : expectedNonProperThrownTypes) {
131 constraints.add(new StrictSubtypingConstraint(expectedNonProperThrownType, thrownType));
136 for (PsiType expectedNonProperThrownType : expectedNonProperThrownTypes) {
137 final InferenceVariable variable = session.getInferenceVariable(expectedNonProperThrownType);
138 //could be null for invalid code
139 if (variable != null) {
140 variable.setThrownBound();
149 private static boolean isAddressed(List<PsiType> expectedThrownTypes, PsiType thrownType) {
150 for (PsiType expectedThrownType : expectedThrownTypes) {
151 if (TypeConversionUtil.isAssignable(expectedThrownType, thrownType)) {
159 public PsiExpression getExpression() {
164 protected PsiType getT() {
169 protected void setT(PsiType t) {
174 protected InputOutputConstraintFormula createSelfConstraint(PsiType type, PsiExpression expression) {
175 return new CheckedExceptionCompatibilityConstraint(expression, type);
179 protected void collectReturnTypeVariables(InferenceSession session,
180 PsiExpression psiExpression,
182 Set<InferenceVariable> result) {
183 session.collectDependencies(returnType, result);