Search in sources :

Example 1 with GrTraitType

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

the class CompleteReferenceExpression method getVariantsFromQualifier.

private void getVariantsFromQualifier(@NotNull GrExpression qualifier) {
    Project project = qualifier.getProject();
    final PsiType qualifierType = TypesUtil.boxPrimitiveType(qualifier.getType(), qualifier.getManager(), qualifier.getResolveScope());
    final ResolveState state = ResolveState.initial();
    if (qualifierType == null || PsiType.VOID.equals(qualifierType)) {
        if (qualifier instanceof GrReferenceExpression) {
            PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
            if (resolved instanceof PsiPackage || resolved instanceof PsiVariable) {
                resolved.processDeclarations(myProcessor, state, null, myRefExpr);
                return;
            }
        }
        getVariantsFromQualifierType(TypesUtil.getJavaLangObject(qualifier), project);
    } else if (qualifierType instanceof PsiIntersectionType) {
        for (PsiType conjunct : ((PsiIntersectionType) qualifierType).getConjuncts()) {
            getVariantsFromQualifierType(conjunct, project);
        }
    } else if (qualifierType instanceof GrTraitType) {
        // Process trait type conjuncts in reversed order because last applied trait matters.
        PsiType[] conjuncts = ((GrTraitType) qualifierType).getConjuncts();
        for (int i = conjuncts.length - 1; i >= 0; i--) {
            getVariantsFromQualifierType(conjuncts[i], project);
        }
    } else {
        getVariantsFromQualifierType(qualifierType, project);
        if (qualifier instanceof GrReferenceExpression && !PsiUtil.isSuperReference(qualifier) && !PsiUtil.isInstanceThisRef(qualifier)) {
            PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
            if (resolved instanceof PsiClass) {
                ////omitted .class
                GlobalSearchScope scope = myRefExpr.getResolveScope();
                PsiClass javaLangClass = PsiUtil.getJavaLangClass(resolved, scope);
                if (javaLangClass != null) {
                    PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
                    PsiTypeParameter[] typeParameters = javaLangClass.getTypeParameters();
                    if (typeParameters.length == 1) {
                        substitutor = substitutor.put(typeParameters[0], qualifierType);
                    }
                    PsiType javaLangClassType = JavaPsiFacade.getElementFactory(myRefExpr.getProject()).createType(javaLangClass, substitutor);
                    ResolveUtil.processAllDeclarations(javaLangClassType, myProcessor, state, myRefExpr);
                }
            }
        }
    }
}
Also used : GrTraitType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTraitType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 2 with GrTraitType

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

the class GrWithTraitTypeCalculator method calculateReturnType.

@Nullable
@Override
protected PsiType calculateReturnType(@NotNull GrMethodCall callExpression, @NotNull PsiMethod resolvedMethod) {
    if (!"withTraits".equals(resolvedMethod.getName()))
        return null;
    if (resolvedMethod instanceof GrGdkMethod) {
        resolvedMethod = ((GrGdkMethod) resolvedMethod).getStaticMethod();
    }
    GrExpression invokedExpression = callExpression.getInvokedExpression();
    if (!(invokedExpression instanceof GrReferenceExpression))
        return null;
    GrExpression originalObject = ((GrReferenceExpression) invokedExpression).getQualifierExpression();
    if (originalObject == null)
        return null;
    PsiType invokedType = originalObject.getType();
    if (!(invokedType instanceof PsiClassType) && !(invokedType instanceof GrTraitType))
        return null;
    PsiClass containingClass = resolvedMethod.getContainingClass();
    if (containingClass == null || !GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(containingClass.getQualifiedName()))
        return null;
    List<PsiType> traits = ContainerUtil.newArrayList();
    GrExpression[] args = callExpression.getArgumentList().getExpressionArguments();
    for (GrExpression arg : args) {
        PsiType type = arg.getType();
        PsiType classItem = PsiUtil.substituteTypeParameter(type, CommonClassNames.JAVA_LANG_CLASS, 0, false);
        PsiClass psiClass = PsiTypesUtil.getPsiClass(classItem);
        if (GrTraitUtil.isTrait(psiClass)) {
            traits.add(classItem);
        }
    }
    return GrTraitType.createTraitType(invokedType, traits);
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrTraitType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTraitType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 GrTraitType (org.jetbrains.plugins.groovy.lang.psi.impl.GrTraitType)2 Project (com.intellij.openapi.project.Project)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 Nullable (org.jetbrains.annotations.Nullable)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)1