Search in sources :

Example 51 with GroovyResolveResult

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

the class GrAnnotationNameValuePairImpl method multiResolveFromAlias.

private static GroovyResolveResult[] multiResolveFromAlias(@NotNull GrAnnotation alias, @NotNull String name, @NotNull PsiAnnotation annotationCollector) {
    List<GroovyResolveResult> result = ContainerUtilRt.newArrayList();
    List<GrAnnotation> annotations = ContainerUtilRt.newArrayList();
    GrAnnotationCollector.collectAnnotations(annotations, alias, annotationCollector);
    for (GrAnnotation annotation : annotations) {
        final PsiElement clazz = annotation.getClassReference().resolve();
        if (clazz instanceof PsiClass && ((PsiClass) clazz).isAnnotationType()) {
            if (GroovyCommonClassNames.GROOVY_TRANSFORM_ANNOTATION_COLLECTOR.equals(((PsiClass) clazz).getQualifiedName()))
                continue;
            for (PsiMethod method : ((PsiClass) clazz).findMethodsByName(name, false)) {
                result.add(new GroovyResolveResultImpl(method, true));
            }
        }
    }
    return result.toArray(new GroovyResolveResult[result.size()]);
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl)

Example 52 with GroovyResolveResult

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

the class ArgumentInstruction method inferMixinType.

@Override
@Nullable
public PsiType inferMixinType() {
    PsiElement element = getElement();
    assert element != null;
    GrCall call = findCall(element);
    GrExpression[] arguments = call.getExpressionArguments();
    boolean hasNamed = PsiImplUtil.hasNamedArguments(call.getArgumentList());
    int index = ArrayUtil.indexOf(arguments, element) + (hasNamed ? 1 : 0);
    GroovyResolveResult[] variants = call.getCallVariants((GrReferenceExpression) element);
    PsiType result = null;
    for (GroovyResolveResult variant : variants) {
        GrClosureSignature signature = GrClosureSignatureUtil.createSignature(variant);
        if (signature == null)
            continue;
        if (GrClosureSignatureUtil.mapParametersToArguments(signature, call) != null && !haveNullParameters(call)) {
            return null;
        }
        GrClosureParameter[] parameters = signature.getParameters();
        if (index >= parameters.length)
            continue;
        result = TypesUtil.getLeastUpperBoundNullable(result, parameters[index].getType(), element.getManager());
    }
    return result;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 53 with GroovyResolveResult

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

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

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

the class GrReferenceExpressionImpl method calculateType.

@Nullable
private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr, boolean forceRValue) {
    final GroovyResolveResult[] results = refExpr.multiResolve(false, forceRValue);
    final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
    final PsiElement resolved = result.getElement();
    for (GrExpressionTypeCalculator calculator : GrExpressionTypeCalculator.EP_NAME.getExtensions()) {
        PsiType type = calculator.calculateType(refExpr, resolved);
        if (type != null)
            return type;
    }
    if (ResolveUtil.isClassReference(refExpr)) {
        GrExpression qualifier = refExpr.getQualifier();
        LOG.assertTrue(qualifier != null);
        return qualifier.getType();
    }
    if (PsiUtil.isCompileStatic(refExpr)) {
        final PsiType type;
        if (resolved instanceof GrField) {
            type = ((GrField) resolved).getType();
        } else if (resolved instanceof GrVariable) {
            type = ((GrVariable) resolved).getDeclaredType();
        } else if (resolved instanceof GrAccessorMethod) {
            type = ((GrAccessorMethod) resolved).getProperty().getType();
        } else {
            type = null;
        }
        if (type != null) {
            return result.getSubstitutor().substitute(type);
        }
    }
    final PsiType nominal = refExpr.getNominalType(forceRValue);
    Boolean reassigned = GrReassignedLocalVarsChecker.isReassignedVar(refExpr);
    if (reassigned != null && reassigned.booleanValue()) {
        return GrReassignedLocalVarsChecker.getReassignedVarType(refExpr, true);
    }
    final PsiType inferred = getInferredTypes(refExpr, resolved);
    if (inferred == null) {
        if (nominal == null) {
            //inside nested closure we could still try to infer from variable initializer. Not sound, but makes sense
            if (resolved instanceof GrVariable) {
                LOG.assertTrue(resolved.isValid());
                return ((GrVariable) resolved).getTypeGroovy();
            }
        }
        return nominal;
    }
    if (nominal == null)
        return inferred;
    if (!TypeConversionUtil.isAssignable(TypeConversionUtil.erasure(nominal), inferred, false)) {
        if (resolved instanceof GrVariable && ((GrVariable) resolved).getTypeElementGroovy() != null) {
            return nominal;
        }
    }
    return inferred;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpressionTypeCalculator(org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrExpressionTypeCalculator) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

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