Search in sources :

Example 81 with GrReferenceExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression 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 82 with GrReferenceExpression

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

the class GrWithTraitTypeCalculator method calculateReturnType.

@Nullable
@Override
protected PsiType calculateReturnType(@NotNull GrMethodCall callExpression, @NotNull PsiMethod resolvedMethod) {
    if (!"withTraits".equals(resolvedMethod.getName()))
        return null;
    if (resolvedMethod instanceof GrGdkMethod) {
        resolvedMethod = ((GrGdkMethod) resolvedMethod).getStaticMethod();
    }
    GrExpression invokedExpression = callExpression.getInvokedExpression();
    if (!(invokedExpression instanceof GrReferenceExpression))
        return null;
    GrExpression originalObject = ((GrReferenceExpression) invokedExpression).getQualifierExpression();
    if (originalObject == null)
        return null;
    PsiType invokedType = originalObject.getType();
    if (!(invokedType instanceof PsiClassType) && !(invokedType instanceof GrTraitType))
        return null;
    PsiClass containingClass = resolvedMethod.getContainingClass();
    if (containingClass == null || !GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(containingClass.getQualifiedName()))
        return null;
    List<PsiType> traits = ContainerUtil.newArrayList();
    GrExpression[] args = callExpression.getArgumentList().getExpressionArguments();
    for (GrExpression arg : args) {
        PsiType type = arg.getType();
        PsiType classItem = PsiUtil.substituteTypeParameter(type, CommonClassNames.JAVA_LANG_CLASS, 0, false);
        PsiClass psiClass = PsiTypesUtil.getPsiClass(classItem);
        if (GrTraitUtil.isTrait(psiClass)) {
            traits.add(classItem);
        }
    }
    return GrTraitType.createTraitType(invokedType, traits);
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrTraitType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTraitType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 83 with GrReferenceExpression

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

the class GrConstructorInvocationImpl method getKeywordType.

@Nullable
private IElementType getKeywordType() {
    GrReferenceExpression keyword = getInvokedExpression();
    PsiElement refElement = keyword.getReferenceNameElement();
    if (refElement == null)
        return null;
    return refElement.getNode().getElementType();
}
Also used : GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 84 with GrReferenceExpression

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

the class ClosureParameterEnhancer method inferType.

@Nullable
public static PsiType inferType(@NotNull GrClosableBlock closure, int index) {
    PsiElement parent = closure.getParent();
    if (parent instanceof GrStringInjection && index == 0) {
        return TypesUtil.createTypeByFQClassName("java.io.StringWriter", closure);
    }
    if (parent instanceof GrArgumentList)
        parent = parent.getParent();
    if (!(parent instanceof GrMethodCall)) {
        return null;
    }
    String methodName = findMethodName((GrMethodCall) parent);
    GrExpression expression = ((GrMethodCall) parent).getInvokedExpression();
    if (!(expression instanceof GrReferenceExpression))
        return null;
    GrExpression qualifier = ((GrReferenceExpression) expression).getQualifierExpression();
    if (qualifier == null)
        return null;
    PsiType type = qualifier.getType();
    if (type == null) {
        return null;
    }
    final PsiParameter[] params = closure.getAllParameters();
    if (params.length == 1 && simpleTypes.containsKey(methodName)) {
        final String typeText = simpleTypes.get(methodName);
        if (typeText.indexOf('<') < 0) {
            return TypesUtil.createTypeByFQClassName(typeText, closure);
        } else {
            return JavaPsiFacade.getElementFactory(closure.getProject()).createTypeFromText(typeText, closure);
        }
    }
    if (iterations.contains(methodName)) {
        if (params.length == 1) {
            return findTypeForIteration(qualifier, closure);
        }
        if (params.length == 2 && InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
            if (index == 0) {
                return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 0, true);
            }
            return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 1, true);
        }
    } else if (GdkMethodUtil.isWithName(methodName) && params.length == 1) {
        return type;
    } else if (GdkMethodUtil.EACH_WITH_INDEX.equals(methodName)) {
        PsiType res = findTypeForIteration(qualifier, closure);
        if (params.length == 2 && res != null) {
            if (index == 0) {
                return res;
            }
            return TypesUtil.createTypeByFQClassName(JAVA_LANG_INTEGER, closure);
        }
        if (InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
            if (params.length == 2) {
                if (index == 0) {
                    return getEntryForMap(type, closure.getProject(), closure.getResolveScope());
                }
                return TypesUtil.createTypeByFQClassName(JAVA_LANG_INTEGER, closure);
            }
            if (params.length == 3) {
                if (index == 0) {
                    return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 0, true);
                }
                if (index == 1) {
                    return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 1, true);
                }
                return TypesUtil.createTypeByFQClassName(JAVA_LANG_INTEGER, closure);
            }
        }
    } else if (GdkMethodUtil.INJECT.equals(methodName) && params.length == 2) {
        if (index == 0) {
            return TypesUtil.createTypeByFQClassName(JAVA_LANG_OBJECT, closure);
        }
        PsiType res = findTypeForIteration(qualifier, closure);
        if (res != null) {
            return res;
        }
        if (InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
            return getEntryForMap(type, closure.getProject(), closure.getResolveScope());
        }
    } else if (GdkMethodUtil.EACH_PERMUTATION.equals(methodName) && params.length == 1) {
        final PsiType itemType = findTypeForIteration(qualifier, closure);
        if (itemType != null) {
            return JavaPsiFacade.getElementFactory(closure.getProject()).createTypeFromText(JAVA_UTIL_ARRAY_LIST + "<" + itemType.getCanonicalText() + ">", closure);
        }
        return TypesUtil.createTypeByFQClassName(JAVA_UTIL_ARRAY_LIST, closure);
    } else if (GdkMethodUtil.WITH_DEFAULT.equals(methodName)) {
        if (params.length == 1 && InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
            return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 0, true);
        }
    } else if (GdkMethodUtil.SORT.equals(methodName)) {
        if (params.length < 3) {
            return findTypeForIteration(qualifier, closure);
        }
    } else if (GdkMethodUtil.WITH_STREAM.equals(methodName)) {
        final PsiMethod method = ((GrMethodCall) parent).resolveMethod();
        if (method instanceof GrGdkMethod) {
            return qualifier.getType();
        } else if (method != null) {
            final PsiParameter[] parameters = method.getParameterList().getParameters();
            if (parameters.length > 0) {
                return parameters[0].getType();
            }
        }
    } else if (GdkMethodUtil.WITH_STREAMS.equals(methodName)) {
        if (index == 0) {
            return TypesUtil.createTypeByFQClassName("java.io.InputStream", closure);
        } else if (index == 1)
            return TypesUtil.createTypeByFQClassName("java.io.OutputStream", closure);
    } else if (GdkMethodUtil.WITH_OBJECT_STREAMS.equals(methodName)) {
        if (index == 0) {
            return TypesUtil.createTypeByFQClassName("java.io.ObjectInputStream", closure);
        } else if (index == 1)
            return TypesUtil.createTypeByFQClassName("java.io.ObjectOutputStream", closure);
    }
    return null;
}
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) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 85 with GrReferenceExpression

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

the class GdkMethodUtil method getClosureMixins.

private static Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>> getClosureMixins(final GrStatement statement) {
    if (!(statement instanceof GrAssignmentExpression))
        return null;
    final GrAssignmentExpression assignment = (GrAssignmentExpression) statement;
    return CachedValuesManager.getCachedValue(statement, new CachedValueProvider<Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>>>() {

        @Nullable
        @Override
        public Result<Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>>> compute() {
            Pair<PsiClassType, GrReferenceExpression> original = getTypeToMixIn(assignment);
            if (original == null)
                return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
            final Pair<GrSignature, String> signatures = getTypeToMix(assignment);
            if (signatures == null)
                return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
            final String name = signatures.second;
            final List<GrMethod> methods = ContainerUtil.newArrayList();
            final PsiClass closure = JavaPsiFacade.getInstance(statement.getProject()).findClass(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, statement.getResolveScope());
            if (closure == null)
                return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
            signatures.first.accept(new GrSignatureVisitor() {

                @Override
                public void visitClosureSignature(GrClosureSignature signature) {
                    super.visitClosureSignature(signature);
                    GrMethod method = createMethod(signature, name, assignment, closure);
                    methods.add(method);
                }
            });
            return Result.create(Trinity.create(original.first, original.second, methods), PsiModificationTracker.MODIFICATION_COUNT);
        }
    });
}
Also used : Trinity(com.intellij.openapi.util.Trinity) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrSignatureVisitor(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignatureVisitor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) List(java.util.List) Nullable(org.jetbrains.annotations.Nullable) Pair(com.intellij.openapi.util.Pair)

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11