Search in sources :

Example 61 with GroovyResolveResult

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

the class GrNumericBinaryExpressionTypeCalculator method fun.

@Nullable
@Override
public PsiType fun(GrOperatorExpression e) {
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(e.multiResolve(false));
    if (resolveResult.isApplicable() && !PsiUtil.isDGMMethod(resolveResult.getElement())) {
        return ResolveUtil.extractReturnTypeFromCandidate(resolveResult, e, new PsiType[] { e.getRightType() });
    }
    PsiType lType = e.getLeftType();
    PsiType rType = e.getRightType();
    if (TypesUtil.isNumericType(lType) && TypesUtil.isNumericType(rType)) {
        return inferNumericType(lType, rType, e);
    }
    return ResolveUtil.extractReturnTypeFromCandidate(resolveResult, e, new PsiType[] { rType });
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 62 with GroovyResolveResult

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

the class GrArgumentLabelImpl method multiResolve.

@NotNull
@Override
public GroovyResolveResult[] multiResolve(boolean incompleteCode) {
    final ResolveResult[] results = getRealReference().multiResolve(incompleteCode);
    if (results instanceof GroovyResolveResult[]) {
        return (GroovyResolveResult[]) results;
    } else {
        final GroovyResolveResult[] results1 = new GroovyResolveResult[results.length];
        for (int i = 0; i < results.length; i++) {
            ResolveResult result = results[i];
            final PsiElement element = result.getElement();
            if (element == null) {
                results1[i] = GroovyResolveResult.EMPTY_RESULT;
            } else {
                results1[i] = new GroovyResolveResultImpl(element, true);
            }
        }
        return results1;
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 63 with GroovyResolveResult

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

the class DefaultCallExpressionTypeCalculator method calculateReturnType.

@Override
public PsiType calculateReturnType(@NotNull GrMethodCall callExpression, GroovyResolveResult[] resolveResults) {
    GrExpression invoked = callExpression.getInvokedExpression();
    if (invoked instanceof GrReferenceExpression) {
        GrReferenceExpression refExpr = (GrReferenceExpression) invoked;
        PsiManager manager = callExpression.getManager();
        PsiType result = null;
        for (GroovyResolveResult resolveResult : resolveResults) {
            PsiType returnType = calculateReturnTypeInner(callExpression, refExpr, resolveResult);
            if (returnType == null)
                return null;
            PsiType nonVoid = PsiType.VOID.equals(returnType) && !isCompileStatic(callExpression) ? PsiType.NULL : returnType;
            PsiType normalized = nonVoid instanceof GrLiteralClassType ? nonVoid : TypesUtil.substituteAndNormalizeType(nonVoid, resolveResult.getSubstitutor(), resolveResult.getSpreadState(), callExpression);
            LOG.assertTrue(normalized != null, "return type: " + returnType + "; substitutor: " + resolveResult.getSubstitutor());
            if (result == null || normalized.isAssignableFrom(result)) {
                result = normalized;
            } else if (!result.isAssignableFrom(normalized)) {
                result = TypesUtil.getLeastUpperBound(result, normalized, manager);
            }
        }
        return result;
    } else {
        return extractReturnTypeFromType(invoked.getType(), false, callExpression);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrLiteralClassType(org.jetbrains.plugins.groovy.lang.psi.impl.GrLiteralClassType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 64 with GroovyResolveResult

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

the class GrIndexPropertyImpl method inferType.

private PsiType inferType(@Nullable Boolean isSetter) {
    PsiType arrayType = inferArrayType();
    if (arrayType != null) {
        return arrayType;
    }
    GrExpression selected = getInvokedExpression();
    PsiType thisType = selected.getType();
    if (thisType == null) {
        thisType = TypesUtil.getJavaLangObject(this);
    }
    GrArgumentList argList = getArgumentList();
    PsiType[] argTypes = PsiUtil.getArgumentTypes(argList);
    if (argTypes == null)
        return null;
    final PsiManager manager = getManager();
    final GlobalSearchScope resolveScope = getResolveScope();
    if (PsiImplUtil.isSimpleArrayAccess(thisType, argTypes, this, isSetter != null ? isSetter.booleanValue() : PsiUtil.isLValue(this))) {
        return TypesUtil.boxPrimitiveType(((PsiArrayType) thisType).getComponentType(), manager, resolveScope);
    }
    final GroovyResolveResult[] candidates;
    if (isSetter != null) {
        candidates = isSetter.booleanValue() ? multiResolveSetter(false) : multiResolveGetter(false);
    } else {
        candidates = multiResolve(false);
    }
    //don't use short PsiUtil.getArgumentTypes(...) because it use incorrect 'isSetter' value
    PsiType[] args = PsiUtil.getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, true, null);
    final GroovyResolveResult candidate = PsiImplUtil.extractUniqueResult(candidates);
    final PsiElement element = candidate.getElement();
    if (element instanceof PsiNamedElement) {
        final String name = ((PsiNamedElement) element).getName();
        if ("putAt".equals(name) && args != null) {
            args = ArrayUtil.append(args, TypeInferenceHelper.getInitializerTypeFor(this), PsiType.class);
        }
    }
    PsiType overloadedOperatorType = ResolveUtil.extractReturnTypeFromCandidate(candidate, this, args);
    PsiType componentType = extractMapValueType(thisType, args, manager, resolveScope);
    if (overloadedOperatorType != null && (componentType == null || !TypesUtil.isAssignableByMethodCallConversion(overloadedOperatorType, componentType, selected))) {
        return TypesUtil.boxPrimitiveType(overloadedOperatorType, manager, resolveScope);
    }
    return componentType;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 65 with GroovyResolveResult

use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult 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)

Aggregations

GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)132 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)33 Nullable (org.jetbrains.annotations.Nullable)29 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)29 NotNull (org.jetbrains.annotations.NotNull)25 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)23 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)14 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)13 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 PsiElement (com.intellij.psi.PsiElement)10 ArrayList (java.util.ArrayList)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 IElementType (com.intellij.psi.tree.IElementType)7 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)7 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)7 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)7 Project (com.intellij.openapi.project.Project)6