Search in sources :

Example 1 with GrGdkMethod

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

the class EachWithIndexClosureCompleter method getParameterInfos.

@Nullable
@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context, PsiMethod method, PsiSubstitutor substitutor, PsiElement place) {
    final String name = method.getName();
    if (!"eachWithIndex".equals(name))
        return null;
    if (method instanceof GrGdkMethod)
        method = ((GrGdkMethod) method).getStaticMethod();
    final PsiClass containingClass = method.getContainingClass();
    if (containingClass == null)
        return null;
    final String qname = containingClass.getQualifiedName();
    if (!GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname))
        return null;
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    if (parameters.length != 2)
        return null;
    final PsiType type = parameters[0].getType();
    final PsiType collection = substitutor.substitute(type);
    final PsiType iterable = getIteratedType(place, collection);
    if (iterable != null) {
        return Arrays.asList(new ClosureParameterInfo(iterable.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
    }
    if (InheritanceUtil.isInheritor(collection, CommonClassNames.JAVA_UTIL_MAP)) {
        final PsiType[] typeParams = ((PsiClassType) collection).getParameters();
        final Project project = context.getProject();
        final PsiClass entry = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_UTIL_MAP_ENTRY, place.getResolveScope());
        if (entry == null)
            return null;
        final PsiClassType entryType = JavaPsiFacade.getElementFactory(project).createType(entry, typeParams);
        return Arrays.asList(new ClosureParameterInfo(entryType.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
    }
    return Arrays.asList(new ClosureParameterInfo(collection.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
Also used : Project(com.intellij.openapi.project.Project) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) ClosureParameterInfo(org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureParameterInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GrGdkMethod

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

the class GrMainCompletionProvider method assignPriority.

private static int assignPriority(LookupElement lookupElement, PsiType qualifierType) {
    Object object = lookupElement.getObject();
    PsiSubstitutor substitutor = null;
    GroovyResolveResult resolveResult = null;
    if (object instanceof GroovyResolveResult) {
        resolveResult = (GroovyResolveResult) object;
        substitutor = resolveResult.getSubstitutor();
        object = ((GroovyResolveResult) object).getElement();
    }
    // default groovy methods
    if (object instanceof GrGdkMethod && GroovyCompletionUtil.skipDefGroovyMethod((GrGdkMethod) object, substitutor, qualifierType)) {
        return -1;
    }
    // operator methods
    if (object instanceof PsiMethod && PsiUtil.OPERATOR_METHOD_NAMES.contains(((PsiMethod) object).getName()) && !checkForIterator((PsiMethod) object)) {
        return -3;
    }
    // accessors if there is no get, set, is prefix
    if (object instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod) object)) {
        return -1;
    }
    // inaccessible elements
    if (resolveResult != null && !resolveResult.isAccessible()) {
        return -2;
    }
    return 0;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)

Example 3 with GrGdkMethod

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

Example 4 with GrGdkMethod

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

the class GdkMethodHolder method processMethods.

public boolean processMethods(PsiScopeProcessor processor, @NotNull ResolveState state, PsiType qualifierType, Project project) {
    if (qualifierType == null)
        return true;
    NameHint nameHint = processor.getHint(NameHint.KEY);
    String name = nameHint == null ? null : nameHint.getName(state);
    final MultiMap<String, PsiMethod> map = name != null ? myOriginalMethodsByNameAndType.get(name) : myOriginalMethodByType.getValue();
    if (map.isEmpty()) {
        return true;
    }
    for (String superType : ResolveUtil.getAllSuperTypes(qualifierType, project)) {
        for (PsiMethod method : map.get(superType)) {
            String info = GdkMethodUtil.generateOriginInfo(method);
            GrGdkMethod gdk = GrGdkMethodImpl.createGdkMethod(method, myStatic, info);
            if (!processor.execute(gdk, state)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) NameHint(com.intellij.psi.scope.NameHint)

Example 5 with GrGdkMethod

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

the class GroovyTypeCheckVisitor method highlightInapplicableMethodUsage.

private void highlightInapplicableMethodUsage(@NotNull GroovyResolveResult methodResolveResult, @NotNull CallInfo info, @NotNull PsiMethod method) {
    final PsiClass containingClass = method instanceof GrGdkMethod ? ((GrGdkMethod) method).getStaticMethod().getContainingClass() : method.getContainingClass();
    PsiType[] argumentTypes = info.getArgumentTypes();
    if (containingClass == null) {
        registerCannotApplyError(method.getName(), info);
        return;
    }
    final String typesString = buildArgTypesList(argumentTypes);
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(method.getProject());
    final PsiClassType containingType = factory.createType(containingClass, methodResolveResult.getSubstitutor());
    final String canonicalText = containingType.getInternalCanonicalText();
    String message = method.isConstructor() ? GroovyBundle.message("cannot.apply.constructor", method.getName(), canonicalText, typesString) : GroovyBundle.message("cannot.apply.method1", method.getName(), canonicalText, typesString);
    registerError(info.getElementToHighlight(), message, genCastFixes(GrClosureSignatureUtil.createSignature(methodResolveResult), argumentTypes, info.getArgumentList()), ProblemHighlightType.GENERIC_ERROR);
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)

Aggregations

GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)22 Nullable (org.jetbrains.annotations.Nullable)8 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)5 NotNull (org.jetbrains.annotations.NotNull)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 PsiMethod (com.intellij.psi.PsiMethod)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)3 NameHint (com.intellij.psi.scope.NameHint)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 GrTupleType (org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType)2 ASTNode (com.intellij.lang.ASTNode)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 com.intellij.psi (com.intellij.psi)1