Search in sources :

Example 31 with GrMethodCall

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

the class MissingReturnInspection method getExpectedClosureReturnType.

@Nullable
public static PsiType getExpectedClosureReturnType(GrClosableBlock closure) {
    List<PsiType> expectedReturnTypes = new ArrayList<>();
    PsiElement parent = closure.getParent();
    if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall || parent instanceof GrMethodCall) {
        GrMethodCall call = (GrMethodCall) (parent instanceof GrArgumentList ? parent.getParent() : parent);
        GroovyResolveResult[] variants = call.getCallVariants(null);
        for (GroovyResolveResult variant : variants) {
            Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(variant, closure, true, true, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
            if (map != null) {
                Pair<PsiParameter, PsiType> pair = map.get(closure);
                if (pair == null)
                    continue;
                PsiParameter parameter = pair.getFirst();
                PsiType type = parameter.getType();
                if (TypesUtil.isPsiClassTypeToClosure(type)) {
                    PsiType[] parameters = ((PsiClassType) type).getParameters();
                    if (parameters.length == 1) {
                        expectedReturnTypes.add(parameters[0]);
                    }
                }
            }
        }
    } else {
        for (PsiType expectedType : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
            if (TypesUtil.isPsiClassTypeToClosure(expectedType)) {
                PsiType[] parameters = ((PsiClassType) expectedType).getParameters();
                if (parameters.length == 1) {
                    expectedReturnTypes.add(parameters[0]);
                }
            }
        }
    }
    for (PsiType type : expectedReturnTypes) {
        if (PsiType.VOID.equals(type))
            return PsiType.VOID;
    }
    return TypesUtil.getLeastUpperBoundNullable(expectedReturnTypes, closure.getManager());
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with GrMethodCall

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

the class UnnecessaryQualifiedReferenceInspection method isQualifiedStaticMethodWithUnnecessaryQualifier.

private static boolean isQualifiedStaticMethodWithUnnecessaryQualifier(GrReferenceExpression ref) {
    if (ref.getQualifier() == null)
        return false;
    final PsiElement resolved = ref.resolve();
    if (!(resolved instanceof PsiMember))
        return false;
    if (!((PsiMember) resolved).hasModifierProperty(PsiModifier.STATIC))
        return false;
    PsiElement copyResolved;
    final PsiElement parent = ref.getParent();
    if (parent instanceof GrMethodCall) {
        final GrMethodCall copy = (GrMethodCall) parent.copy();
        GrReferenceExpression invoked = (GrReferenceExpression) copy.getInvokedExpression();
        assert invoked != null;
        invoked.setQualifier(null);
        copyResolved = ((GrReferenceExpression) copy.getInvokedExpression()).resolve();
    } else {
        final GrReferenceExpression copy = (GrReferenceExpression) ref.copy();
        copy.setQualifier(null);
        copyResolved = copy.resolve();
    }
    return ref.getManager().areElementsEquivalent(copyResolved, resolved);
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 33 with GrMethodCall

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

Example 34 with GrMethodCall

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

the class GdkMethodUtil method getTypeToMixIn.

@Nullable
private static Pair<PsiClassType, GrReferenceExpression> getTypeToMixIn(GrMethodCall methodCall) {
    GrExpression invoked = methodCall.getInvokedExpression();
    if (invoked instanceof GrReferenceExpression && GrImportUtil.acceptName((GrReferenceExpression) invoked, "mixin")) {
        PsiElement resolved = ((GrReferenceExpression) invoked).resolve();
        if (resolved instanceof PsiMethod && isMixinMethod((PsiMethod) resolved)) {
            GrExpression qualifier = ((GrReferenceExpression) invoked).getQualifier();
            Pair<PsiClassType, GrReferenceExpression> type = getPsiClassFromReference(qualifier);
            if (type != null) {
                return type;
            }
            if (qualifier == null) {
                qualifier = PsiImplUtil.getRuntimeQualifier((GrReferenceExpression) invoked);
            }
            if (qualifier != null && isMetaClass(qualifier.getType())) {
                if (qualifier instanceof GrMethodCall)
                    qualifier = ((GrMethodCall) qualifier).getInvokedExpression();
                if (qualifier instanceof GrReferenceExpression) {
                    GrExpression qqualifier = ((GrReferenceExpression) qualifier).getQualifier();
                    if (qqualifier != null) {
                        Pair<PsiClassType, GrReferenceExpression> type1 = getPsiClassFromReference(qqualifier);
                        if (type1 != null) {
                            return type1;
                        }
                    } else {
                        PsiType qtype = PsiImplUtil.getQualifierType((GrReferenceExpression) qualifier);
                        if (qtype instanceof PsiClassType && ((PsiClassType) qtype).resolve() != null) {
                            return Pair.create((PsiClassType) qtype, (GrReferenceExpression) qualifier);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 35 with GrMethodCall

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

the class ResolveUtil method getSelfOrWithQualifier.

@Nullable
public static GrExpression getSelfOrWithQualifier(GrReferenceExpression ref) {
    final GrExpression qualifier = ref.getQualifierExpression();
    if (qualifier != null) {
        return qualifier;
    }
    PsiElement place = ref;
    while (true) {
        final GrClosableBlock closure = PsiTreeUtil.getParentOfType(place, GrClosableBlock.class, true, GrMember.class, GroovyFile.class);
        if (closure == null)
            break;
        place = closure;
        PsiElement clParent = closure.getParent();
        if (clParent instanceof GrArgumentList)
            clParent = clParent.getParent();
        if (!(clParent instanceof GrMethodCall))
            continue;
        final GrExpression expression = ((GrMethodCall) clParent).getInvokedExpression();
        if (expression instanceof GrReferenceExpression && GdkMethodUtil.isWithName(((GrReferenceExpression) expression).getReferenceName()) && ((GrReferenceExpression) expression).resolve() instanceof GrGdkMethod) {
            final GrExpression withQualifier = ((GrReferenceExpression) expression).getQualifierExpression();
            if (withQualifier != null) {
                return withQualifier;
            }
        }
    }
    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) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)51 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)23 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)22 Nullable (org.jetbrains.annotations.Nullable)18 PsiElement (com.intellij.psi.PsiElement)17 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)9 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 NotNull (org.jetbrains.annotations.NotNull)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 PsiMethod (com.intellij.psi.PsiMethod)3 PsiType (com.intellij.psi.PsiType)3 ArrayList (java.util.ArrayList)3 GroovyMapContentProvider (org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider)3 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)3 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)3