Search in sources :

Example 1 with GrBuilderMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrBuilderMethod in project intellij-community by JetBrains.

the class GroovyTypeCheckVisitor method checkMethodApplicability.

private <T extends GroovyPsiElement> boolean checkMethodApplicability(@NotNull final GroovyResolveResult methodResolveResult, boolean checkUnknownArgs, @NotNull final CallInfo<T> info) {
    final PsiElement element = methodResolveResult.getElement();
    if (!(element instanceof PsiMethod))
        return true;
    if (element instanceof GrBuilderMethod)
        return true;
    final PsiMethod method = (PsiMethod) element;
    if ("call".equals(method.getName()) && info.getInvokedExpression() instanceof GrReferenceExpression) {
        final GrExpression qualifierExpression = ((GrReferenceExpression) info.getInvokedExpression()).getQualifierExpression();
        if (qualifierExpression != null) {
            final PsiType type = qualifierExpression.getType();
            if (type instanceof GrClosureType) {
                GrClosureSignatureUtil.ApplicabilityResult result = PsiUtil.isApplicableConcrete(info.getArgumentTypes(), (GrClosureType) type, info.getInvokedExpression());
                switch(result) {
                    case inapplicable:
                        highlightInapplicableMethodUsage(methodResolveResult, info, method);
                        return false;
                    case //q(1,2)
                    canBeApplicable:
                        if (checkUnknownArgs) {
                            highlightUnknownArgs(info);
                        }
                        return !checkUnknownArgs;
                    default:
                        return true;
                }
            }
        }
    }
    if (method instanceof GrGdkMethod && info.getInvokedExpression() instanceof GrReferenceExpression) {
        final GrReferenceExpression invoked = (GrReferenceExpression) info.getInvokedExpression();
        final GrExpression qualifier = PsiImplUtil.getRuntimeQualifier(invoked);
        if (qualifier == null && method.getName().equals("call")) {
            GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(invoked.getProject());
            final GrReferenceExpression callRef = factory.createReferenceExpressionFromText("qualifier.call", invoked);
            callRef.setQualifier(invoked);
            return checkMethodApplicability(methodResolveResult, checkUnknownArgs, new DelegatingCallInfo<T>(info) {

                @Nullable
                @Override
                public GrExpression getInvokedExpression() {
                    return callRef;
                }

                @NotNull
                @Override
                public GroovyResolveResult advancedResolve() {
                    return methodResolveResult;
                }

                @NotNull
                @Override
                public GroovyResolveResult[] multiResolve() {
                    return new GroovyResolveResult[] { methodResolveResult };
                }

                @Nullable
                @Override
                public PsiType getQualifierInstanceType() {
                    return info.getInvokedExpression().getType();
                }
            });
        }
        final PsiMethod staticMethod = ((GrGdkMethod) method).getStaticMethod();
        PsiType qualifierType = info.getQualifierInstanceType();
        if (method.hasModifierProperty(PsiModifier.STATIC)) {
            qualifierType = ResolveUtil.unwrapClassType(qualifierType);
        }
        //check methods processed by @Category(ClassWhichProcessMethod) annotation
        if (qualifierType != null && !GdkMethodUtil.isCategoryMethod(staticMethod, qualifierType, qualifier, methodResolveResult.getSubstitutor()) && !checkCategoryQualifier(invoked, qualifier, staticMethod, methodResolveResult.getSubstitutor())) {
            registerError(info.getHighlightElementForCategoryQualifier(), ProblemHighlightType.GENERIC_ERROR, GroovyInspectionBundle.message("category.method.0.cannot.be.applied.to.1", method.getName(), qualifierType.getCanonicalText()));
            return false;
        }
    }
    if (info.getArgumentTypes() == null)
        return true;
    GrClosureSignatureUtil.ApplicabilityResult applicable = PsiUtil.isApplicableConcrete(info.getArgumentTypes(), method, methodResolveResult.getSubstitutor(), info.getCall(), false);
    switch(applicable) {
        case inapplicable:
            highlightInapplicableMethodUsage(methodResolveResult, info, method);
            return false;
        case canBeApplicable:
            if (checkUnknownArgs) {
                highlightUnknownArgs(info);
            }
            return !checkUnknownArgs;
        default:
            return true;
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrBuilderMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrBuilderMethod) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrBuilderMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrBuilderMethod)1 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)1 GrClosureType (org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)1 GrClosureSignatureUtil (org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil)1