Search in sources :

Example 16 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class GrClosureSignatureUtil method generateAllSignaturesForMethod.

public static List<MethodSignature> generateAllSignaturesForMethod(GrMethod method, PsiSubstitutor substitutor) {
    GrClosureSignature signature = createSignature(method, substitutor);
    String name = method.getName();
    PsiTypeParameter[] typeParameters = method.getTypeParameters();
    final ArrayList<MethodSignature> result = new ArrayList<>();
    generateAllMethodSignaturesByClosureSignature(name, signature, typeParameters, substitutor, result);
    return result;
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 17 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class GrClosureSignatureUtil method generateAllMethodSignaturesBySignature.

public static List<MethodSignature> generateAllMethodSignaturesBySignature(@NotNull final String name, @NotNull final GrSignature signature) {
    final ArrayList<MethodSignature> result = new ArrayList<>();
    signature.accept(new GrRecursiveSignatureVisitor() {

        @Override
        public void visitClosureSignature(GrClosureSignature signature) {
            generateAllMethodSignaturesByClosureSignature(name, signature, PsiTypeParameter.EMPTY_ARRAY, PsiSubstitutor.EMPTY, result);
        }
    });
    return result;
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) GrRecursiveSignatureVisitor(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 18 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class GrClosureSignatureUtil method mapArgumentsToParameters.

@Nullable
public static Map<GrExpression, Pair<PsiParameter, PsiType>> mapArgumentsToParameters(@NotNull GroovyResolveResult resolveResult, @NotNull PsiElement context, final boolean partial, final boolean eraseArgs, @NotNull final GrNamedArgument[] namedArgs, @NotNull final GrExpression[] expressionArgs, @NotNull GrClosableBlock[] closureArguments) {
    final GrClosureSignature signature;
    final PsiParameter[] parameters;
    final PsiElement element = resolveResult.getElement();
    final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
    if (element instanceof PsiMethod) {
        signature = createSignature((PsiMethod) element, substitutor, eraseArgs);
        parameters = ((PsiMethod) element).getParameterList().getParameters();
    } else if (element instanceof GrClosableBlock) {
        signature = eraseArgs ? createSignatureWithErasedParameterTypes((GrClosableBlock) element) : createSignature(((GrClosableBlock) element));
        parameters = ((GrClosableBlock) element).getAllParameters();
    } else {
        return null;
    }
    final ArgInfo<PsiElement>[] argInfos = mapParametersToArguments(signature, namedArgs, expressionArgs, closureArguments, context, partial, eraseArgs);
    if (argInfos == null) {
        return null;
    }
    final HashMap<GrExpression, Pair<PsiParameter, PsiType>> result = new HashMap<>();
    for (int i = 0; i < argInfos.length; i++) {
        ArgInfo<PsiElement> info = argInfos[i];
        if (info == null)
            continue;
        for (PsiElement arg : info.args) {
            if (arg instanceof GrNamedArgument) {
                arg = ((GrNamedArgument) arg).getExpression();
            }
            final GrExpression expression = (GrExpression) arg;
            PsiType type = parameters[i].getType();
            if (info.isMultiArg && type instanceof PsiArrayType) {
                type = ((PsiArrayType) type).getComponentType();
            }
            result.put(expression, Pair.create(parameters[i], substitutor.substitute(type)));
        }
    }
    return result;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class TypesUtil method getLeastUpperBound.

@Nullable
public static PsiType getLeastUpperBound(@NotNull PsiType type1, @NotNull PsiType type2, PsiManager manager) {
    {
        PsiType numericLUB = getNumericLUB(type1, type2);
        if (numericLUB != null)
            return numericLUB;
    }
    if (type1 instanceof GrTupleType && type2 instanceof GrTupleType) {
        GrTupleType tuple1 = (GrTupleType) type1;
        GrTupleType tuple2 = (GrTupleType) type2;
        PsiType[] components1 = tuple1.getComponentTypes();
        PsiType[] components2 = tuple2.getComponentTypes();
        if (components1.length == 0)
            return genNewListBy(type2, manager);
        if (components2.length == 0)
            return genNewListBy(type1, manager);
        PsiType[] components3 = PsiType.createArray(Math.min(components1.length, components2.length));
        for (int i = 0; i < components3.length; i++) {
            PsiType c1 = components1[i];
            PsiType c2 = components2[i];
            if (c1 == null || c2 == null) {
                components3[i] = null;
            } else {
                components3[i] = getLeastUpperBound(c1, c2, manager);
            }
        }
        return new GrImmediateTupleType(components3, JavaPsiFacade.getInstance(manager.getProject()), tuple1.getScope().intersectWith(tuple2.getResolveScope()));
    } else if (checkEmptyListAndList(type1, type2)) {
        return genNewListBy(type2, manager);
    } else if (checkEmptyListAndList(type2, type1)) {
        return genNewListBy(type1, manager);
    } else if (type1 instanceof GrMapType && type2 instanceof GrMapType) {
        return GrMapType.merge(((GrMapType) type1), ((GrMapType) type2));
    } else if (checkEmptyMapAndMap(type1, type2)) {
        return genNewMapBy(type2, manager);
    } else if (checkEmptyMapAndMap(type2, type1)) {
        return genNewMapBy(type1, manager);
    } else if (type1 instanceof GrClosureType && type2 instanceof GrClosureType) {
        GrClosureType clType1 = (GrClosureType) type1;
        GrClosureType clType2 = (GrClosureType) type2;
        GrSignature signature1 = clType1.getSignature();
        GrSignature signature2 = clType2.getSignature();
        if (signature1 instanceof GrClosureSignature && signature2 instanceof GrClosureSignature) {
            if (((GrClosureSignature) signature1).getParameterCount() == ((GrClosureSignature) signature2).getParameterCount()) {
                final GrClosureSignature signature = GrImmediateClosureSignatureImpl.getLeastUpperBound(((GrClosureSignature) signature1), ((GrClosureSignature) signature2), manager);
                if (signature != null) {
                    GlobalSearchScope scope = clType1.getResolveScope().intersectWith(clType2.getResolveScope());
                    final LanguageLevel languageLevel = ComparatorUtil.max(clType1.getLanguageLevel(), clType2.getLanguageLevel());
                    return GrClosureType.create(signature, scope, JavaPsiFacade.getInstance(manager.getProject()), languageLevel, true);
                }
            }
        }
    } else if (GroovyCommonClassNames.GROOVY_LANG_GSTRING.equals(getQualifiedName(type1)) && CommonClassNames.JAVA_LANG_STRING.equals(getQualifiedName(type2))) {
        return type2;
    } else if (GroovyCommonClassNames.GROOVY_LANG_GSTRING.equals(getQualifiedName(type2)) && CommonClassNames.JAVA_LANG_STRING.equals(getQualifiedName(type1))) {
        return type1;
    }
    return GenericsUtil.getLeastUpperBound(type1, type2, manager);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) LanguageLevel(com.intellij.pom.java.LanguageLevel) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class PsiUtil method isRawClosureCall.

private static boolean isRawClosureCall(GrMethodCallExpression call, GroovyResolveResult result, GrClosureType returnType) {
    final GrSignature signature = returnType.getSignature();
    GrClosureSignature _signature;
    if (signature instanceof GrClosureSignature) {
        _signature = (GrClosureSignature) signature;
    } else {
        final PsiType[] types = getArgumentTypes(call.getInvokedExpression(), true);
        final Trinity<GrClosureSignature, GrClosureSignatureUtil.ArgInfo<PsiType>[], GrClosureSignatureUtil.ApplicabilityResult> resultTrinity = types != null ? GrClosureSignatureUtil.getApplicableSignature(signature, types, call) : null;
        _signature = resultTrinity != null ? resultTrinity.first : null;
    }
    if (_signature != null) {
        return isRawType(_signature.getReturnType(), TypesUtil.composeSubstitutors(_signature.getSubstitutor(), result.getSubstitutor()));
    }
    return false;
}
Also used : GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)

Aggregations

GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)8 ArrayList (java.util.ArrayList)6 Nullable (org.jetbrains.annotations.Nullable)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)6 GrClosureParameter (org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)6 GrClosureSignatureUtil (org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 MethodSignature (com.intellij.psi.util.MethodSignature)4 List (java.util.List)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)4 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)4 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)4 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)4 Pair (com.intellij.openapi.util.Pair)3 TIntProcedure (gnu.trove.TIntProcedure)3