Search in sources :

Example 31 with GroovyResolveResult

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

the class GroovyIntroduceParameterUtil method removeParametersFromCall.

private static void removeParametersFromCall(GrMethodCallExpression methodCall, GrIntroduceParameterSettings settings) {
    final GroovyResolveResult resolveResult = methodCall.advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    LOG.assertTrue(resolved instanceof PsiMethod);
    final GrClosureSignature signature = GrClosureSignatureUtil.createSignature((PsiMethod) resolved, resolveResult.getSubstitutor());
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, methodCall);
    LOG.assertTrue(argInfos != null);
    settings.parametersToRemove().forEach(new TIntProcedure() {

        @Override
        public boolean execute(int value) {
            final List<PsiElement> args = argInfos[value].args;
            for (PsiElement arg : args) {
                arg.delete();
            }
            return true;
        }
    });
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) TIntProcedure(gnu.trove.TIntProcedure) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) TIntArrayList(gnu.trove.TIntArrayList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 32 with GroovyResolveResult

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

the class OldReferencesResolver method getQualifierFromGetterCall.

/**
   * checks for the case: qualifier.getFoo()(args)
   * @param methodExpression
   */
@Nullable
private static GrExpression getQualifierFromGetterCall(GrMethodCall methodExpression) {
    final GroovyResolveResult result = methodExpression.advancedResolve();
    if (!(result.getElement() instanceof GrAccessorMethod) || result.isInvokedOnProperty())
        return null;
    final GrExpression invoked = methodExpression.getInvokedExpression();
    if (invoked instanceof GrReferenceExpression)
        return ((GrReferenceExpression) invoked).getQualifier();
    return null;
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with GroovyResolveResult

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

the class GroovyAnnotator method visitTypeArgumentList.

@Override
public void visitTypeArgumentList(@NotNull GrTypeArgumentList typeArgumentList) {
    PsiElement parent = typeArgumentList.getParent();
    if (!(parent instanceof GrReferenceElement))
        return;
    final GroovyResolveResult resolveResult = ((GrReferenceElement) parent).advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
    if (resolved == null)
        return;
    if (!(resolved instanceof PsiTypeParameterListOwner)) {
        myHolder.createWarningAnnotation(typeArgumentList, GroovyBundle.message("type.argument.list.is.not.allowed.here"));
        return;
    }
    if (parent instanceof GrCodeReferenceElement) {
        if (!checkDiamonds((GrCodeReferenceElement) parent, myHolder))
            return;
    }
    final PsiTypeParameter[] parameters = ((PsiTypeParameterListOwner) resolved).getTypeParameters();
    final GrTypeElement[] arguments = typeArgumentList.getTypeArgumentElements();
    if (arguments.length != parameters.length) {
        myHolder.createWarningAnnotation(typeArgumentList, GroovyBundle.message("wrong.number.of.type.arguments", arguments.length, parameters.length));
        return;
    }
    for (int i = 0; i < parameters.length; i++) {
        PsiTypeParameter parameter = parameters[i];
        final PsiClassType[] superTypes = parameter.getExtendsListTypes();
        final PsiType argType = arguments[i].getType();
        for (PsiClassType superType : superTypes) {
            final PsiType substitutedSuper = substitutor.substitute(superType);
            if (substitutedSuper != null && !substitutedSuper.isAssignableFrom(argType)) {
                myHolder.createWarningAnnotation(arguments[i], GroovyBundle.message("type.argument.0.is.not.in.its.bound.should.extend.1", argType.getCanonicalText(), superType.getCanonicalText()));
                break;
            }
        }
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)

Example 34 with GroovyResolveResult

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

the class GroovyAnnotator method visitConstructorInvocation.

@Override
public void visitConstructorInvocation(@NotNull GrConstructorInvocation invocation) {
    final GroovyResolveResult resolveResult = invocation.advancedResolve();
    if (resolveResult.getElement() == null) {
        final GroovyResolveResult[] results = invocation.multiResolve(false);
        final GrArgumentList argList = invocation.getArgumentList();
        if (results.length > 0) {
            String message = GroovyBundle.message("ambiguous.constructor.call");
            myHolder.createWarningAnnotation(argList, message);
        } else {
            final PsiClass clazz = invocation.getDelegatedClass();
            if (clazz != null) {
                //default constructor invocation
                PsiType[] argumentTypes = PsiUtil.getArgumentTypes(invocation.getInvokedExpression(), true);
                if (argumentTypes != null && argumentTypes.length > 0) {
                    String message = GroovyBundle.message("cannot.apply.default.constructor", clazz.getName());
                    myHolder.createWarningAnnotation(argList, message);
                }
            }
        }
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)

Example 35 with GroovyResolveResult

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

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