Search in sources :

Example 1 with GrTupleType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.

the class GppClosureParameterTypeProvider method getClosureParameterType.

@Override
protected PsiType getClosureParameterType(GrClosableBlock closure, int index) {
    final PsiElement parent = closure.getParent();
    if (parent instanceof GrNamedArgument) {
        final Pair<PsiMethod, PsiSubstitutor> pair = getOverriddenMethod((GrNamedArgument) parent);
        if (pair != null) {
            final PsiParameter[] parameters = pair.first.getParameterList().getParameters();
            if (parameters.length > index) {
                return pair.second.substitute(parameters[index].getType());
            }
            return null;
        }
    }
    if (parent instanceof GrListOrMap) {
        final GrListOrMap list = (GrListOrMap) parent;
        if (!list.isMap()) {
            final PsiType listType = list.getType();
            final int argIndex = Arrays.asList(list.getInitializers()).indexOf(closure);
            assert argIndex >= 0;
            if (listType instanceof GrTupleType) {
                for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(list)) {
                    if (!(type instanceof PsiClassType))
                        continue;
                    final GroovyResolveResult[] candidates = PsiUtil.getConstructorCandidates((PsiClassType) type, ((GrTupleType) listType).getComponentTypes(), closure);
                    for (GroovyResolveResult resolveResult : candidates) {
                        final PsiElement method = resolveResult.getElement();
                        if (!(method instanceof PsiMethod) || !((PsiMethod) method).isConstructor())
                            continue;
                        final PsiParameter[] parameters = ((PsiMethod) method).getParameterList().getParameters();
                        if (parameters.length <= argIndex)
                            continue;
                        final PsiType toCastTo = resolveResult.getSubstitutor().substitute(parameters[argIndex].getType());
                        final PsiType suggestion = getSingleMethodParameterType(toCastTo, index, closure);
                        if (suggestion != null)
                            return suggestion;
                    }
                }
            }
            return null;
        }
    }
    for (PsiType constraint : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
        final PsiType suggestion = getSingleMethodParameterType(constraint, index, closure);
        if (suggestion != null) {
            return suggestion;
        }
    }
    return null;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)

Example 2 with GrTupleType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.

the class TypeInferenceHelper method getInitializerTypeFor.

@Nullable
public static PsiType getInitializerTypeFor(PsiElement element) {
    final PsiElement parent = skipParentheses(element.getParent(), true);
    if (parent instanceof GrAssignmentExpression) {
        if (element instanceof GrIndexProperty) {
            final GrExpression rvalue = ((GrAssignmentExpression) parent).getRValue();
            //don't try to infer assignment type in case of index property because of infinite recursion (example: a[2]+=4)
            return rvalue != null ? rvalue.getType() : null;
        }
        return ((GrAssignmentExpression) parent).getType();
    }
    if (parent instanceof GrTupleExpression) {
        GrTupleExpression list = (GrTupleExpression) parent;
        if (list.getParent() instanceof GrAssignmentExpression) {
            // multiple assignment
            final GrExpression rValue = ((GrAssignmentExpression) list.getParent()).getRValue();
            int idx = list.indexOf(element);
            if (idx >= 0 && rValue != null) {
                PsiType rType = rValue.getType();
                if (rType instanceof GrTupleType) {
                    PsiType[] componentTypes = ((GrTupleType) rType).getComponentTypes();
                    if (idx < componentTypes.length)
                        return componentTypes[idx];
                    return null;
                }
                return PsiUtil.extractIterableTypeParameter(rType, false);
            }
        }
    }
    if (parent instanceof GrUnaryExpression && TokenSets.POSTFIX_UNARY_OP_SET.contains(((GrUnaryExpression) parent).getOperationTokenType())) {
        return ((GrUnaryExpression) parent).getType();
    }
    return null;
}
Also used : GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GrTupleType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.

the class GrIndexPropertyImpl method resolveImpl.

private GroovyResolveResult[] resolveImpl(boolean incompleteCode, @Nullable GrExpression upToArgument, @Nullable Boolean isSetter) {
    if (isSetter == null)
        isSetter = PsiUtil.isLValue(this);
    GrExpression invoked = getInvokedExpression();
    PsiType thisType = invoked.getType();
    if (thisType == null) {
        thisType = TypesUtil.getJavaLangObject(this);
    }
    GrArgumentList argList = getArgumentList();
    //don't use short PsiUtil.getArgumentTypes(...) because it use incorrect 'isSetter' value
    PsiType[] argTypes = PsiUtil.getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, true, upToArgument);
    if (argTypes == null)
        return GroovyResolveResult.EMPTY_ARRAY;
    final GlobalSearchScope resolveScope = getResolveScope();
    if (argTypes.length == 0) {
        PsiType arrType = null;
        if (invoked instanceof GrBuiltinTypeClassExpression) {
            arrType = ((GrBuiltinTypeClassExpression) invoked).getPrimitiveType();
        }
        if (invoked instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) invoked).resolve();
            if (resolved instanceof PsiClass) {
                String qname = ((PsiClass) resolved).getQualifiedName();
                if (qname != null) {
                    arrType = TypesUtil.createTypeByFQClassName(qname, this);
                }
            }
        }
        if (arrType != null) {
            return GroovyResolveResult.EMPTY_ARRAY;
        }
    }
    GroovyResolveResult[] candidates;
    final String name = isSetter ? "putAt" : "getAt";
    if (isSetter && !incompleteCode) {
        argTypes = ArrayUtil.append(argTypes, TypeInferenceHelper.getInitializerTypeFor(this), PsiType.class);
    }
    if (PsiImplUtil.isSimpleArrayAccess(thisType, argTypes, this, isSetter)) {
        return GroovyResolveResult.EMPTY_ARRAY;
    }
    candidates = ResolveUtil.getMethodCandidates(thisType, name, invoked, true, incompleteCode, argTypes);
    //hack for remove DefaultGroovyMethods.getAt(Object, ...)
    if (candidates.length == 2) {
        for (int i = 0; i < candidates.length; i++) {
            GroovyResolveResult candidate = candidates[i];
            final PsiElement element = candidate.getElement();
            if (element instanceof GrGdkMethod) {
                final PsiMethod staticMethod = ((GrGdkMethod) element).getStaticMethod();
                final PsiParameter param = staticMethod.getParameterList().getParameters()[0];
                if (param.getType().equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
                    return new GroovyResolveResult[] { candidates[1 - i] };
                }
            }
        }
    }
    if (candidates.length != 1) {
        final GrTupleType tupleType = new GrImmediateTupleType(argTypes, JavaPsiFacade.getInstance(getProject()), resolveScope);
        final GroovyResolveResult[] tupleCandidates = ResolveUtil.getMethodCandidates(thisType, name, invoked, tupleType);
        if (incompleteCode) {
            candidates = ArrayUtil.mergeArrays(candidates, tupleCandidates, count -> new GroovyResolveResult[count]);
        } else {
            candidates = tupleCandidates;
        }
    }
    return candidates;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) ArrayUtil(com.intellij.util.ArrayUtil) PsiImplUtil(org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) ResolveUtil(org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil) ResolveUtil.getClassReferenceFromExpression(org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil.getClassReferenceFromExpression) GrBuiltinTypeClassExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBuiltinTypeClassExpression) GroovyElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) TypeInferenceHelper(org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) ResolveCache(com.intellij.psi.impl.source.resolve.ResolveCache) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NullableFunction(com.intellij.util.NullableFunction) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) TextRange(com.intellij.openapi.util.TextRange) TypesUtil(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) GrImmediateTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrImmediateTupleType) Function(com.intellij.util.Function) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrExpressionImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.GrExpressionImpl) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) GrBuiltinTypeClassExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBuiltinTypeClassExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrImmediateTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrImmediateTupleType)

Example 4 with GrTupleType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.

the class ClosureParameterEnhancer method findTypeForIteration.

@Contract("null,_ -> null")
@Nullable
public static PsiType findTypeForIteration(@Nullable PsiType type, @NotNull PsiElement context) {
    final PsiManager manager = context.getManager();
    final GlobalSearchScope resolveScope = context.getResolveScope();
    if (type instanceof PsiArrayType) {
        return TypesUtil.boxPrimitiveType(((PsiArrayType) type).getComponentType(), manager, resolveScope);
    }
    if (type instanceof GrTupleType) {
        PsiType[] types = ((GrTupleType) type).getParameters();
        return types.length == 1 ? types[0] : null;
    }
    if (type instanceof GrRangeType) {
        return ((GrRangeType) type).getIterationType();
    }
    PsiType fromIterator = findTypeFromIteratorMethod(type, context);
    if (fromIterator != null) {
        return fromIterator;
    }
    PsiType extracted = PsiUtil.extractIterableTypeParameter(type, true);
    if (extracted != null) {
        return extracted;
    }
    if (TypesUtil.isClassType(type, JAVA_LANG_STRING) || TypesUtil.isClassType(type, JAVA_IO_FILE)) {
        return PsiType.getJavaLangString(manager, resolveScope);
    }
    if (InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
        return getEntryForMap(type, manager.getProject(), resolveScope);
    }
    return type;
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) GrRangeType(org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GrTupleType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.

the class GdkMethodUtil method categoryIteration.

public static boolean categoryIteration(GrClosableBlock place, final PsiScopeProcessor processor, ResolveState state) {
    if (!ResolveUtil.shouldProcessMethods(processor.getHint(ElementClassHint.KEY)) && !(processor instanceof GroovyResolverProcessor && ((GroovyResolverProcessor) processor).isPropertyResolve())) {
        return true;
    }
    final GrMethodCall call = checkMethodCall(place, USE);
    if (call == null)
        return true;
    final GrClosableBlock[] closures = call.getClosureArguments();
    GrExpression[] args = call.getExpressionArguments();
    if (!(placeEqualsSingleClosureArg(place, closures) || placeEqualsLastArg(place, args)))
        return true;
    if (!(call.resolveMethod() instanceof GrGdkMethod))
        return true;
    state = state.put(ClassHint.RESOLVE_CONTEXT, call);
    if ((args.length == 1 || args.length == 2 && placeEqualsLastArg(place, args))) {
        PsiType type = args[0].getType();
        if (type instanceof GrTupleType) {
            return processTypesFromTuple((GrTupleType) type, processor, state, place);
        }
    }
    return processTypesFomArgs(args, processor, state, place);
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.GroovyResolverProcessor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType)

Aggregations

GrTupleType (org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType)7 Nullable (org.jetbrains.annotations.Nullable)3 PsiType (com.intellij.psi.PsiType)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrIndexProperty (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty)2 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)2 ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 Trinity (com.intellij.openapi.util.Trinity)1 com.intellij.psi (com.intellij.psi)1 PsiElement (com.intellij.psi.PsiElement)1 ResolveCache (com.intellij.psi.impl.source.resolve.ResolveCache)1 InheritanceUtil (com.intellij.psi.util.InheritanceUtil)1 ArrayUtil (com.intellij.util.ArrayUtil)1 Function (com.intellij.util.Function)1