mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
new array refs: accept signatures with assignable to int params (IDEA-106973)
This commit is contained in:
@@ -19,6 +19,7 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.util.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -165,7 +166,12 @@ public class PsiMethodReferenceUtil {
|
||||
if (interfaceReturnType == PsiType.VOID) return true;
|
||||
final PsiParameter[] parameters = method.getParameterList().getParameters();
|
||||
if (resolve == JavaPsiFacade.getElementFactory(resolve.getProject()).getArrayClass(PsiUtil.getLanguageLevel(resolve))) {
|
||||
if (parameters.length != 1 || parameters[0].getType() != PsiType.INT) return false;
|
||||
if (!arrayCompatibleSignature(parameters, new Function<PsiParameter[], PsiType>() {
|
||||
@Override
|
||||
public PsiType fun(PsiParameter[] parameters) {
|
||||
return resolveResult.getSubstitutor().substitute(parameters[0].getType());
|
||||
}
|
||||
})) return false;
|
||||
final PsiTypeParameter[] typeParameters = ((PsiClass)resolve).getTypeParameters();
|
||||
if (typeParameters.length == 1) {
|
||||
final PsiType arrayComponentType = result.getSubstitutor().substitute(typeParameters[0]);
|
||||
@@ -281,7 +287,12 @@ public class PsiMethodReferenceUtil {
|
||||
|
||||
|
||||
public static boolean onArrayType(PsiClass containingClass, MethodSignature signature) {
|
||||
if (signature.getParameterTypes().length == 1 && signature.getParameterTypes()[0] == PsiType.INT) {
|
||||
if (arrayCompatibleSignature(signature.getParameterTypes(), new Function<PsiType[], PsiType>() {
|
||||
@Override
|
||||
public PsiType fun(PsiType[] types) {
|
||||
return types[0];
|
||||
}
|
||||
})) {
|
||||
if (containingClass != null) {
|
||||
final Project project = containingClass.getProject();
|
||||
final LanguageLevel level = PsiUtil.getLanguageLevel(containingClass);
|
||||
@@ -291,6 +302,14 @@ public class PsiMethodReferenceUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static <T> boolean arrayCompatibleSignature(T[] paramTypes, Function<T[], PsiType> fun) {
|
||||
if (paramTypes.length == 1) {
|
||||
final PsiType paramType = fun.fun(paramTypes);
|
||||
if (paramType != null && TypeConversionUtil.isAssignable(PsiType.INT, paramType)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static PsiType getExpandedType(PsiType type, @NotNull PsiElement typeElement) {
|
||||
if (type instanceof PsiArrayType) {
|
||||
type = JavaPsiFacade.getElementFactory(typeElement.getProject()).getArrayClassType(((PsiArrayType)type).getComponentType(),
|
||||
|
||||
@@ -45,3 +45,16 @@ class OnArrayTest {
|
||||
ObjectArrayReturnType a5 = Foo<? extends String>[]::new;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class IDEA106973 {
|
||||
interface Function<T, R> {
|
||||
R apply(T t);
|
||||
}
|
||||
|
||||
{
|
||||
Function<Integer, String[]> a = String[] :: new;
|
||||
<error descr="Incompatible types. Found: '<method reference>', required: 'IDEA106973.Function<java.lang.String,java.lang.String[]>'">Function<String, String[]> a1 = String[] :: new;</error>
|
||||
Function<Short, String[]> a2 = String[] :: new;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user