mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
lambda: infer lambda param types in method call, return statement context
This commit is contained in:
@@ -2635,13 +2635,6 @@ public class HighlightUtil {
|
||||
|
||||
@Nullable
|
||||
public static HighlightInfo checkLambdaFeature(final PsiLambdaExpression expression) {
|
||||
final HighlightInfo info = checkFeature(expression, Feature.LAMBDA_EXPRESSIONS);
|
||||
if (info != null) return info;
|
||||
if (expression.getParent() instanceof PsiExpressionList) {
|
||||
// todo[r.sh] stub; remove after implementing support in TypeConversionUtil
|
||||
final String message = "Lambda expressions type check is not yet implemented";
|
||||
return HighlightInfo.createHighlightInfo(HighlightInfoType.WEAK_WARNING, expression, message);
|
||||
}
|
||||
return null;
|
||||
return checkFeature(expression, Feature.LAMBDA_EXPRESSIONS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.daemon.impl.analysis;
|
||||
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfoFilter;
|
||||
import com.intellij.lang.annotation.HighlightSeverity;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* A filter to temporarily suppress highlighting errors inside lambda expressions.
|
||||
* todo[r.sh] stub; remove after implementing type inference
|
||||
*/
|
||||
public class JavaHighlightInfoFilter implements HighlightInfoFilter {
|
||||
@Override
|
||||
public boolean accept(@NotNull final HighlightInfo info, @Nullable final PsiFile file) {
|
||||
if (!(file instanceof PsiJavaFile)) return true;
|
||||
if (info.getSeverity() != HighlightSeverity.ERROR) return true;
|
||||
if (description(info, "Lambda expressions are not supported at this language level")) return true;
|
||||
|
||||
final PsiElement element = file.findElementAt(info.getStartOffset());
|
||||
if (element == null) return true;
|
||||
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class, false);
|
||||
if (lambdaExpression != null) {
|
||||
if (lambdaExpression.getParent() instanceof PsiExpressionList) return false;
|
||||
if (isLambdaInAmbiguousCall(info, element)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isLambdaInAmbiguousCall(final HighlightInfo info, final PsiElement element) {
|
||||
if (!description(info, "Ambiguous method call")) return false;
|
||||
final PsiElement exprList = element.getParent();
|
||||
if (!(exprList instanceof PsiExpressionList)) return false;
|
||||
return PsiTreeUtil.getChildOfType(exprList, PsiLambdaExpression.class) != null;
|
||||
}
|
||||
|
||||
private static boolean description(final HighlightInfo info, final String prefix) {
|
||||
return info.description != null && info.description.startsWith(prefix);
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ package com.intellij.psi.impl.source.tree.java;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -114,19 +115,13 @@ public class LambdaUtil {
|
||||
final int parameterIndex = ((PsiParameterList)paramParent).getParameterIndex(param);
|
||||
if (parameterIndex > -1) {
|
||||
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(param, PsiLambdaExpression.class);
|
||||
if (lambdaExpression != null) {
|
||||
final PsiElement parent = lambdaExpression.getParent();
|
||||
PsiType type = null;
|
||||
if (parent instanceof PsiTypeCastExpression) {
|
||||
type = ((PsiTypeCastExpression)parent).getType();
|
||||
} else if (parent instanceof PsiVariable) {
|
||||
type = ((PsiVariable)parent).getType();
|
||||
}
|
||||
if (type instanceof PsiClassType) {
|
||||
final PsiClassType.ClassResolveResult resolveResult = ((PsiClassType)type).resolveGenerics();
|
||||
final MethodSignature methodSignature = getFunction(resolveResult.getElement());
|
||||
if (methodSignature != null) {
|
||||
return resolveResult.getSubstitutor().substitute(methodSignature.getParameterTypes()[parameterIndex]);
|
||||
final PsiClassType.ClassResolveResult resolveResult = getFunctionInterfaceType(lambdaExpression);
|
||||
if (resolveResult != null) {
|
||||
final MethodSignature methodSignature = getFunction(resolveResult.getElement());
|
||||
if (methodSignature != null) {
|
||||
final PsiType[] types = methodSignature.getParameterTypes();
|
||||
if (parameterIndex < types.length) {
|
||||
return resolveResult.getSubstitutor().substitute(types[parameterIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,4 +129,46 @@ public class LambdaUtil {
|
||||
}
|
||||
return new PsiLambdaParameterType(param);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiClassType.ClassResolveResult getFunctionInterfaceType(@Nullable PsiLambdaExpression lambdaExpression) {
|
||||
if (lambdaExpression != null) {
|
||||
final PsiElement parent = lambdaExpression.getParent();
|
||||
PsiType type = null;
|
||||
if (parent instanceof PsiTypeCastExpression) {
|
||||
type = ((PsiTypeCastExpression)parent).getType();
|
||||
}
|
||||
else if (parent instanceof PsiVariable) {
|
||||
type = ((PsiVariable)parent).getType();
|
||||
}
|
||||
else if (parent instanceof PsiExpressionList) {
|
||||
final PsiExpressionList expressionList = (PsiExpressionList)parent;
|
||||
final int lambdaIdx = ArrayUtil.find(expressionList.getExpressions(), lambdaExpression);
|
||||
if (lambdaIdx > -1) {
|
||||
final PsiElement gParent = expressionList.getParent();
|
||||
if (gParent instanceof PsiMethodCallExpression) {
|
||||
final JavaResolveResult resolveResult = ((PsiMethodCallExpression)gParent).resolveMethodGenerics();
|
||||
final PsiElement resolve = resolveResult.getElement();
|
||||
if (resolve instanceof PsiMethod) {
|
||||
final PsiParameter[] parameters = ((PsiMethod)resolve).getParameterList().getParameters();
|
||||
if (lambdaIdx < parameters.length) {
|
||||
type = resolveResult.getSubstitutor().substitute(parameters[lambdaIdx].getType());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (parent instanceof PsiReturnStatement) {
|
||||
final PsiMethod method = PsiTreeUtil.getParentOfType(parent, PsiMethod.class);
|
||||
if (method != null) {
|
||||
type = method.getReturnType();
|
||||
}
|
||||
}
|
||||
|
||||
if (type instanceof PsiClassType) {
|
||||
return ((PsiClassType)type).resolveGenerics();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class C {
|
||||
|
||||
void test() {
|
||||
Simplest simplest = () -> { };
|
||||
use(<weak_warning descr="Lambda expressions type check is not yet implemented">() -> { }</weak_warning>);
|
||||
use(() -> { });
|
||||
|
||||
IntParser intParser = (String s) -> Integer.parseInt(s);
|
||||
}
|
||||
|
||||
@@ -9,4 +9,10 @@ class Foo {
|
||||
I ii1 = (final int k)->{
|
||||
<error descr="Incompatible types. Found: 'int', required: 'java.lang.String'">String s = k;</error>
|
||||
};
|
||||
|
||||
void bazz() {
|
||||
bar((String s) -> {
|
||||
System.out.println(s);});
|
||||
}
|
||||
void bar(I i){}
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
import java.lang.String;
|
||||
|
||||
interface I {
|
||||
void m(int i);
|
||||
}
|
||||
@@ -19,5 +17,19 @@ class Foo {
|
||||
String s = ab;
|
||||
<error descr="Incompatible types. Found: 'java.lang.String', required: 'int'">int i = ab;</error>
|
||||
};
|
||||
|
||||
A<Integer> bazz() {
|
||||
bar((o) -> {
|
||||
String s = o;
|
||||
<error descr="Incompatible types. Found: 'java.lang.String', required: 'int'">int i = o;</error>
|
||||
});
|
||||
return (i) -> {
|
||||
Integer k = i;
|
||||
int n = i;
|
||||
<error descr="Incompatible types. Found: 'java.lang.Integer', required: 'java.lang.String'">String s = i;</error>
|
||||
};
|
||||
}
|
||||
|
||||
void bar(A<String> a){}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ class Foo {
|
||||
|
||||
void bar() {
|
||||
foo<error descr="Ambiguous method call: both 'Foo.foo(I)' and 'Foo.foo(K)' match">((p) -> {
|
||||
System.out.println(p);
|
||||
System.out.println<error descr="Cannot resolve method 'println(<lambda parameter>)'">(p)</error>;
|
||||
})</error>;
|
||||
|
||||
foo((p, k) -> {
|
||||
|
||||
@@ -378,7 +378,6 @@
|
||||
<projectService serviceImplementation="com.intellij.javadoc.JavadocGenerationManager"/>
|
||||
|
||||
<highlightVisitor implementation="com.intellij.codeInsight.daemon.impl.analysis.HighlightVisitorImpl"/>
|
||||
<daemon.highlightInfoFilter implementation="com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightInfoFilter"/>
|
||||
|
||||
<lang.foldingBuilder language="JAVA" implementationClass="com.intellij.codeInsight.daemon.impl.actions.SuppressWarningsFoldingBuilder" />
|
||||
<moduleConfigurationEditorProvider implementation="com.intellij.openapi.module.WebModuleConfigurationEditorProvider"/>
|
||||
|
||||
Reference in New Issue
Block a user