Search in sources :

Example 21 with GrArgumentList

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

the class JavaStylePropertiesUtil method isGetterInvocation.

private static boolean isGetterInvocation(@NotNull GrMethodCall call) {
    GrExpression expr = call.getInvokedExpression();
    if (!(expr instanceof GrReferenceExpression))
        return false;
    PsiMethod method = call.resolveMethod();
    if (!GroovyPropertyUtils.isSimplePropertyGetter(method))
        return false;
    LOG.assertTrue(method != null);
    if (!GroovyNamesUtil.isValidReference(GroovyPropertyUtils.getPropertyNameByGetterName(method.getName(), true), ((GrReferenceExpression) expr).getQualifier() != null, call.getProject())) {
        return false;
    }
    GrArgumentList args = call.getArgumentList();
    if (args.getAllArguments().length != 0) {
        return false;
    }
    GrExpression ref = genRefForGetter(call, ((GrReferenceExpression) expr).getReferenceName());
    if (ref instanceof GrReferenceExpression) {
        PsiElement resolved = ((GrReferenceExpression) ref).resolve();
        PsiManager manager = call.getManager();
        if (manager.areElementsEquivalent(resolved, method) || areEquivalentAccessors(method, resolved, manager)) {
            return true;
        }
    }
    return false;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) PsiManager(com.intellij.psi.PsiManager) PsiElement(com.intellij.psi.PsiElement)

Example 22 with GrArgumentList

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

the class GroovyConstructorNamedArgumentProvider method getNamedArguments.

@Override
public void getNamedArguments(@NotNull GrCall call, @NotNull GroovyResolveResult resolveResult, @Nullable String argumentName, boolean forCompletion, @NotNull Map<String, NamedArgumentDescriptor> result) {
    if (!(call instanceof GrNewExpression))
        return;
    PsiElement resolve = resolveResult.getElement();
    if (resolve != null) {
        if (!(resolve instanceof PsiMethod))
            return;
        PsiMethod method = (PsiMethod) resolve;
        if (!method.isConstructor())
            return;
    }
    GrNewExpression newCall = (GrNewExpression) call;
    GrArgumentList argumentList = newCall.getArgumentList();
    if (argumentList == null)
        return;
    GrExpression[] expressionArguments = argumentList.getExpressionArguments();
    if (expressionArguments.length > 1 || (expressionArguments.length == 1 && !(expressionArguments[0] instanceof GrReferenceExpression))) {
        return;
    }
    for (GroovyResolveResult newResult : newCall.multiResolveClass()) {
        PsiElement element = newResult.getElement();
        if (!(element instanceof PsiClass))
            continue;
        PsiClass aClass = (PsiClass) element;
        if (!isClassHasConstructorWithMap(aClass))
            continue;
        PsiClassType classType = JavaPsiFacade.getElementFactory(aClass.getProject()).createType(aClass);
        processClass(call, classType, argumentName, result);
    }
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) 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)

Example 23 with GrArgumentList

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

the class GrNewExpressionImpl method resolveImpl.

private GroovyResolveResult[] resolveImpl(boolean incompleteCode) {
    GrCodeReferenceElement ref = getReferenceElement();
    if (ref == null)
        return GroovyResolveResult.EMPTY_ARRAY;
    GroovyResolveResult classCandidate = inferClassCandidate(ref);
    if (classCandidate == null)
        return GroovyResolveResult.EMPTY_ARRAY;
    assert classCandidate.getElement() instanceof PsiClass;
    if (incompleteCode) {
        return PsiUtil.getConstructorCandidates(ref, classCandidate, null);
    }
    final GrArgumentList argumentList = getArgumentList();
    if (argumentList == null)
        return GroovyResolveResult.EMPTY_ARRAY;
    if (argumentList.getNamedArguments().length > 0 && argumentList.getExpressionArguments().length == 0) {
        PsiType mapType = GrMapType.createFromNamedArgs(argumentList, getNamedArguments());
        //one Map parameter, actually
        GroovyResolveResult[] constructorResults = PsiUtil.getConstructorCandidates(ref, classCandidate, new PsiType[] { mapType });
        for (GroovyResolveResult result : constructorResults) {
            final PsiElement resolved = result.getElement();
            if (resolved instanceof PsiMethod) {
                PsiMethod constructor = (PsiMethod) resolved;
                final PsiParameter[] parameters = constructor.getParameterList().getParameters();
                if (parameters.length == 1 && InheritanceUtil.isInheritor(parameters[0].getType(), CommonClassNames.JAVA_UTIL_MAP)) {
                    return constructorResults;
                }
            }
        }
        final GroovyResolveResult[] emptyConstructors = PsiUtil.getConstructorCandidates(ref, classCandidate, PsiType.EMPTY_ARRAY);
        if (emptyConstructors.length > 0) {
            return emptyConstructors;
        }
    }
    PsiType[] types = PsiUtil.getArgumentTypes(ref, true);
    if (types != null) {
        types = GrInnerClassConstructorUtil.addEnclosingArgIfNeeded(types, this, (PsiClass) classCandidate.getElement());
    }
    return PsiUtil.getConstructorCandidates(ref, classCandidate, types);
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)

Example 24 with GrArgumentList

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

the class DefaultCallExpressionTypeCalculator method getClosureMethodsReturnType.

@Nullable
private static PsiType getClosureMethodsReturnType(GrMethodCall callExpression, GrReferenceExpression refExpr, PsiMethod resolved) {
    PsiClass clazz = resolved.getContainingClass();
    if (clazz == null || !GroovyCommonClassNames.GROOVY_LANG_CLOSURE.equals(clazz.getQualifiedName()))
        return null;
    if (!CLOSURE_METHODS.contains(resolved.getName()))
        return null;
    GrExpression qualifier = refExpr.getQualifierExpression();
    if (qualifier == null)
        return null;
    PsiType qType = qualifier.getType();
    if (!(qType instanceof GrClosureType))
        return null;
    if ("call".equals(resolved.getName())) {
        return GrClosureSignatureUtil.getReturnType(((GrClosureType) qType).getSignature(), callExpression);
    } else if ("curry".equals(resolved.getName()) || "trampoline".equals(resolved.getName())) {
        return ((GrClosureType) qType).curry(PsiUtil.getArgumentTypes(refExpr, false), 0, callExpression);
    } else if ("memoize".equals(resolved.getName())) {
        return qType;
    } else if ("rcurry".equals(resolved.getName())) {
        return ((GrClosureType) qType).curry(PsiUtil.getArgumentTypes(refExpr, false), -1, callExpression);
    } else if ("ncurry".equals(resolved.getName())) {
        final GrArgumentList argList = callExpression.getArgumentList();
        final GrExpression[] arguments = argList.getExpressionArguments();
        if (arguments.length > 0) {
            final GrExpression first = arguments[0];
            if (first instanceof GrLiteral) {
                final Object value = ((GrLiteral) first).getValue();
                if (value instanceof Integer) {
                    final PsiType[] argTypes = PsiUtil.getArgumentTypes(refExpr, false);
                    if (argTypes != null) {
                        return ((GrClosureType) qType).curry(ArrayUtil.remove(argTypes, 0), (Integer) value, callExpression);
                    }
                }
            }
        }
        return qType;
    }
    return null;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with GrArgumentList

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

the class GrIndexPropertyImpl method inferType.

private PsiType inferType(@Nullable Boolean isSetter) {
    PsiType arrayType = inferArrayType();
    if (arrayType != null) {
        return arrayType;
    }
    GrExpression selected = getInvokedExpression();
    PsiType thisType = selected.getType();
    if (thisType == null) {
        thisType = TypesUtil.getJavaLangObject(this);
    }
    GrArgumentList argList = getArgumentList();
    PsiType[] argTypes = PsiUtil.getArgumentTypes(argList);
    if (argTypes == null)
        return null;
    final PsiManager manager = getManager();
    final GlobalSearchScope resolveScope = getResolveScope();
    if (PsiImplUtil.isSimpleArrayAccess(thisType, argTypes, this, isSetter != null ? isSetter.booleanValue() : PsiUtil.isLValue(this))) {
        return TypesUtil.boxPrimitiveType(((PsiArrayType) thisType).getComponentType(), manager, resolveScope);
    }
    final GroovyResolveResult[] candidates;
    if (isSetter != null) {
        candidates = isSetter.booleanValue() ? multiResolveSetter(false) : multiResolveGetter(false);
    } else {
        candidates = multiResolve(false);
    }
    //don't use short PsiUtil.getArgumentTypes(...) because it use incorrect 'isSetter' value
    PsiType[] args = PsiUtil.getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, true, null);
    final GroovyResolveResult candidate = PsiImplUtil.extractUniqueResult(candidates);
    final PsiElement element = candidate.getElement();
    if (element instanceof PsiNamedElement) {
        final String name = ((PsiNamedElement) element).getName();
        if ("putAt".equals(name) && args != null) {
            args = ArrayUtil.append(args, TypeInferenceHelper.getInitializerTypeFor(this), PsiType.class);
        }
    }
    PsiType overloadedOperatorType = ResolveUtil.extractReturnTypeFromCandidate(candidate, this, args);
    PsiType componentType = extractMapValueType(thisType, args, manager, resolveScope);
    if (overloadedOperatorType != null && (componentType == null || !TypesUtil.isAssignableByMethodCallConversion(overloadedOperatorType, componentType, selected))) {
        return TypesUtil.boxPrimitiveType(overloadedOperatorType, manager, resolveScope);
    }
    return componentType;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Aggregations

GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)80 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 PsiElement (com.intellij.psi.PsiElement)21 Nullable (org.jetbrains.annotations.Nullable)20 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)15 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)14 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)14 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)14 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)14 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)12 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)9 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)8 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)8 NotNull (org.jetbrains.annotations.NotNull)7 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)6 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)6 ASTNode (com.intellij.lang.ASTNode)5