Search in sources :

Example 16 with GrMethodCall

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

the class GebPageMemberContributor method processDynamicElements.

@Override
public void processDynamicElements(@NotNull PsiType qualifierType, PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull PsiElement place, @NotNull ResolveState state) {
    if (!ResolveUtil.shouldProcessProperties(processor.getHint(ElementClassHint.KEY)))
        return;
    PsiElement grCall = place.getParent();
    if (grCall instanceof GrMethodCall) {
        PsiElement grClosure = grCall.getParent();
        if (grClosure instanceof GrClosableBlock) {
            PsiElement contentField = grClosure.getParent();
            if (contentField instanceof GrField) {
                GrField f = (GrField) contentField;
                if ("content".equals(f.getName()) && f.hasModifierProperty(PsiModifier.STATIC) && f.getContainingClass() == aClass) {
                    Map<String, PsiField> elements = GebUtil.getContentElements(aClass);
                    for (PsiField field : elements.values()) {
                        if (field.getNavigationElement() == place) {
                            // Don't resolve variable definition.
                            return;
                        }
                    }
                }
            }
        }
    }
    processPageFields(processor, aClass, state);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)

Example 17 with GrMethodCall

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

the class GrClosureSignatureUtil method createSignature.

@Nullable
public static GrClosureSignature createSignature(GrCall call) {
    if (call instanceof GrMethodCall) {
        final GrExpression invokedExpression = ((GrMethodCall) call).getInvokedExpression();
        final PsiType type = invokedExpression.getType();
        if (type instanceof GrClosureType) {
            final GrSignature signature = ((GrClosureType) type).getSignature();
            final Trinity<GrClosureSignature, ArgInfo<PsiType>[], ApplicabilityResult> trinity = getApplicableSignature(signature, PsiUtil.getArgumentTypes(invokedExpression, true), call);
            if (trinity != null) {
                return trinity.first;
            }
            return null;
        }
    }
    final GroovyResolveResult resolveResult = call.advancedResolve();
    final PsiElement element = resolveResult.getElement();
    if (element instanceof PsiMethod) {
        return createSignature((PsiMethod) element, resolveResult.getSubstitutor());
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with GrMethodCall

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

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

the class GdkMethodUtil method getMixinTypes.

/**
   * @return (type[1] in which methods mixed, reference to type[1], type[2] to mixin)
   */
@Nullable
private static Trinity<PsiClassType, GrReferenceExpression, PsiClass> getMixinTypes(final GrStatement statement) {
    if (!(statement instanceof GrMethodCall))
        return null;
    return CachedValuesManager.getCachedValue(statement, () -> {
        GrMethodCall call = (GrMethodCall) statement;
        Pair<PsiClassType, GrReferenceExpression> original = getTypeToMixIn(call);
        if (original == null)
            return CachedValueProvider.Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
        PsiClass mix = getTypeToMix(call);
        if (mix == null)
            return CachedValueProvider.Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
        return CachedValueProvider.Result.create(new Trinity<>(original.first, original.second, mix), PsiModificationTracker.MODIFICATION_COUNT);
    });
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with GrMethodCall

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

the class GdkMethodUtil method checkMethodCall.

@Nullable
private static GrMethodCall checkMethodCall(GrClosableBlock place, String methodName) {
    final PsiElement context = place.getContext();
    GrMethodCall call = null;
    if (context instanceof GrMethodCall) {
        call = (GrMethodCall) context;
    } else if (context instanceof GrArgumentList) {
        final PsiElement ccontext = context.getContext();
        if (ccontext instanceof GrMethodCall) {
            call = (GrMethodCall) ccontext;
        }
    }
    if (call == null)
        return null;
    final GrExpression invoked = call.getInvokedExpression();
    if (!(invoked instanceof GrReferenceExpression) || !methodName.equals(((GrReferenceExpression) invoked).getReferenceName())) {
        return null;
    }
    return call;
}
Also used : 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) 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