Search in sources :

Example 6 with PsiClassReferenceType

use of com.intellij.psi.impl.source.PsiClassReferenceType in project android by JetBrains.

the class AndroidImplicitUsagesProvider method isImplicitConstructorUsage.

public boolean isImplicitConstructorUsage(PsiMethod method) {
    if (!method.isConstructor()) {
        return false;
    }
    if (!method.hasModifierProperty(PsiModifier.PUBLIC)) {
        return false;
    }
    PsiParameterList parameterList = method.getParameterList();
    int parameterCount = parameterList.getParametersCount();
    if (parameterCount == 0) {
        // Some Android classes need default constructors, and are invoked by inflaters
        final PsiClass aClass = method.getContainingClass();
        if (aClass != null) {
            if (InheritanceUtil.isInheritor(aClass, CLASS_FRAGMENT) || InheritanceUtil.isInheritor(aClass, CLASS_V4_FRAGMENT) || InheritanceUtil.isInheritor(aClass, CLASS_BACKUP_AGENT)) {
                // perhaps due to the XML DOM bindings?
                return true;
            }
        }
        return false;
    }
    //   ActionProvider(android.content.Context context)
    if (parameterCount < 1 || parameterCount > 3) {
        return false;
    }
    PsiParameter[] parameters = parameterList.getParameters();
    PsiType type = parameters[0].getType();
    if (!(type instanceof PsiClassReferenceType)) {
        return false;
    }
    PsiClassReferenceType classType = (PsiClassReferenceType) type;
    PsiClass resolvedParameter = classType.resolve();
    if (resolvedParameter == null || !CLASS_CONTEXT.equals(resolvedParameter.getQualifiedName())) {
        return false;
    }
    if (parameterCount > 1) {
        type = parameters[1].getType();
        if (!(type instanceof PsiClassReferenceType)) {
            return false;
        }
        classType = (PsiClassReferenceType) type;
        resolvedParameter = classType.resolve();
        if (resolvedParameter == null || !CLASS_ATTRIBUTE_SET.equals(resolvedParameter.getQualifiedName())) {
            return false;
        }
        if (parameterCount > 2) {
            type = parameters[2].getType();
            if (!PsiType.INT.equals(type)) {
                return false;
            }
        }
    }
    final PsiClass aClass = PsiTreeUtil.getParentOfType(method, PsiClass.class);
    if (aClass == null) {
        return false;
    }
    PsiClass viewBaseClass = JavaPsiFacade.getInstance(aClass.getProject()).findClass(CLASS_VIEW, method.getResolveScope());
    if (viewBaseClass == null) {
        return false;
    }
    return aClass.isInheritor(viewBaseClass, true) || parameterCount == 1 && InheritanceUtil.isInheritor(aClass, CLASS_ACTION_PROVIDER);
}
Also used : PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType)

Example 7 with PsiClassReferenceType

use of com.intellij.psi.impl.source.PsiClassReferenceType in project intellij-community by JetBrains.

the class PsiClassReferenceListStubImpl method getReferencedTypes.

@NotNull
@Override
public PsiClassType[] getReferencedTypes() {
    if (myTypes != null)
        return myTypes;
    if (myNames.length == 0) {
        myTypes = PsiClassType.EMPTY_ARRAY;
        return myTypes;
    }
    PsiClassType[] types = new PsiClassType[myNames.length];
    final boolean compiled = ((JavaClassReferenceListElementType) getStubType()).isCompiled(this);
    if (compiled) {
        for (int i = 0; i < types.length; i++) {
            types[i] = new PsiClassReferenceType(new ClsJavaCodeReferenceElementImpl(getPsi(), myNames[i]), null);
        }
    } else {
        final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
        int nullCount = 0;
        final PsiReferenceList psi = getPsi();
        for (int i = 0; i < types.length; i++) {
            try {
                final PsiJavaCodeReferenceElement ref = factory.createReferenceFromText(myNames[i], psi);
                ((PsiJavaCodeReferenceElementImpl) ref).setKindWhenDummy(PsiJavaCodeReferenceElementImpl.CLASS_NAME_KIND);
                types[i] = factory.createType(ref);
            } catch (IncorrectOperationException e) {
                types[i] = null;
                nullCount++;
            }
        }
        if (nullCount > 0) {
            PsiClassType[] newTypes = new PsiClassType[types.length - nullCount];
            int cnt = 0;
            for (PsiClassType type : types) {
                if (type != null)
                    newTypes[cnt++] = type;
            }
            types = newTypes;
        }
    }
    myTypes = types;
    return types.clone();
}
Also used : ClsJavaCodeReferenceElementImpl(com.intellij.psi.impl.compiled.ClsJavaCodeReferenceElementImpl) JavaClassReferenceListElementType(com.intellij.psi.impl.java.stubs.JavaClassReferenceListElementType) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) PsiJavaCodeReferenceElementImpl(com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with PsiClassReferenceType

use of com.intellij.psi.impl.source.PsiClassReferenceType in project intellij-community by JetBrains.

the class PsiImplUtil method getType.

@NotNull
public static PsiType getType(@NotNull PsiClassObjectAccessExpression classAccessExpression) {
    GlobalSearchScope resolveScope = classAccessExpression.getResolveScope();
    PsiManager manager = classAccessExpression.getManager();
    final PsiClass classClass = JavaPsiFacade.getInstance(manager.getProject()).findClass("java.lang.Class", resolveScope);
    if (classClass == null) {
        return new PsiClassReferenceType(new LightClassReference(manager, "Class", "java.lang.Class", resolveScope), null);
    }
    if (!PsiUtil.isLanguageLevel5OrHigher(classAccessExpression)) {
        //Raw java.lang.Class
        return JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createType(classClass);
    }
    PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
    PsiType operandType = classAccessExpression.getOperand().getType();
    if (operandType instanceof PsiPrimitiveType && !PsiType.NULL.equals(operandType)) {
        if (PsiType.VOID.equals(operandType)) {
            operandType = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createTypeByFQClassName("java.lang.Void", classAccessExpression.getResolveScope());
        } else {
            operandType = ((PsiPrimitiveType) operandType).getBoxedType(classAccessExpression);
        }
    }
    final PsiTypeParameter[] typeParameters = classClass.getTypeParameters();
    if (typeParameters.length == 1) {
        substitutor = substitutor.put(typeParameters[0], operandType);
    }
    return new PsiImmediateClassType(classClass, substitutor);
}
Also used : LightClassReference(com.intellij.psi.impl.light.LightClassReference) PsiImmediateClassType(com.intellij.psi.impl.source.PsiImmediateClassType) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PsiClassReferenceType

use of com.intellij.psi.impl.source.PsiClassReferenceType in project intellij-community by JetBrains.

the class PsiImplUtil method buildTypeFromTypeString.

public static PsiType buildTypeFromTypeString(@NotNull final String typeName, @NotNull final PsiElement context, @NotNull final PsiFile psiFile) {
    final PsiManager psiManager = psiFile.getManager();
    if (typeName.indexOf('<') != -1 || typeName.indexOf('[') != -1 || typeName.indexOf('.') == -1) {
        try {
            return JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createTypeFromText(typeName, context);
        }// invalid syntax will produce unresolved class type
         catch (Exception ignored) {
        }
    }
    PsiClass aClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(typeName, context.getResolveScope());
    PsiType resultType;
    if (aClass == null) {
        final LightClassReference ref = new LightClassReference(psiManager, PsiNameHelper.getShortClassName(typeName), typeName, PsiSubstitutor.EMPTY, psiFile);
        resultType = new PsiClassReferenceType(ref, null);
    } else {
        PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory();
        PsiSubstitutor substitutor = factory.createRawSubstitutor(aClass);
        resultType = factory.createType(aClass, substitutor);
    }
    return resultType;
}
Also used : LightClassReference(com.intellij.psi.impl.light.LightClassReference) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType)

Example 10 with PsiClassReferenceType

use of com.intellij.psi.impl.source.PsiClassReferenceType in project intellij-community by JetBrains.

the class NamedDomainObjectCollectionTypeEnhancer method getReferenceType.

@Override
public PsiType getReferenceType(GrReferenceExpression ref, @Nullable PsiElement resolved) {
    if (resolved != null)
        return null;
    GrExpression qualifierExpression = ref.getQualifierExpression();
    if (qualifierExpression == null)
        return null;
    PsiType namedDomainCollectionType = GradleResolverUtil.getTypeOf(qualifierExpression);
    if (!InheritanceUtil.isInheritor(namedDomainCollectionType, GradleCommonClassNames.GRADLE_API_NAMED_DOMAIN_OBJECT_COLLECTION)) {
        return null;
    }
    PsiElement qResolved;
    if (qualifierExpression instanceof GrReferenceExpression) {
        qResolved = ((GrReferenceExpression) qualifierExpression).resolve();
    } else if (qualifierExpression instanceof GrMethodCall) {
        qResolved = ((GrMethodCall) qualifierExpression).resolveMethod();
    } else {
        return null;
    }
    String key = ref.getReferenceName();
    if (key == null)
        return null;
    for (GroovyMapContentProvider provider : GroovyMapContentProvider.EP_NAME.getExtensions()) {
        PsiType type = provider.getValueType(qualifierExpression, qResolved, key);
        if (type != null) {
            return type;
        }
    }
    if (namedDomainCollectionType instanceof PsiClassReferenceType) {
        final PsiClassReferenceType referenceType = (PsiClassReferenceType) namedDomainCollectionType;
        final String fqName = TypesUtil.getQualifiedName(referenceType);
        if (GradleCommonClassNames.GRADLE_API_SOURCE_SET_CONTAINER.equals(fqName)) {
            final GroovyPsiManager psiManager = GroovyPsiManager.getInstance(ref.getProject());
            return psiManager.createTypeByFQClassName(GradleCommonClassNames.GRADLE_API_SOURCE_SET, ref.getResolveScope());
        } else if (GradleCommonClassNames.GRADLE_API_CONFIGURATION_CONTAINER.equals(fqName)) {
            final GroovyPsiManager psiManager = GroovyPsiManager.getInstance(ref.getProject());
            return psiManager.createTypeByFQClassName(GradleCommonClassNames.GRADLE_API_CONFIGURATION, ref.getResolveScope());
        } else if (GradleCommonClassNames.GRADLE_API_TASK_CONTAINER.equals(fqName)) {
            final GroovyPsiManager psiManager = GroovyPsiManager.getInstance(ref.getProject());
            return psiManager.createTypeByFQClassName(GradleCommonClassNames.GRADLE_API_TASK, ref.getResolveScope());
        } else if (GradleCommonClassNames.GRADLE_API_DISTRIBUTION_CONTAINER.equals(fqName)) {
            final GroovyPsiManager psiManager = GroovyPsiManager.getInstance(ref.getProject());
            return psiManager.createTypeByFQClassName(GradleCommonClassNames.GRADLE_API_DISTRIBUTION, ref.getResolveScope());
        } else {
            GradleExtensionsSettings.GradleExtensionsData extensionsData = GradleExtensionsContributor.Companion.getExtensionsFor(ref);
            if (extensionsData != null) {
                for (GradleExtensionsSettings.GradleExtension extension : extensionsData.extensions) {
                    if (StringUtil.isNotEmpty(extension.namedObjectTypeFqn) && extension.rootTypeFqn.equals(fqName)) {
                        final GroovyPsiManager psiManager = GroovyPsiManager.getInstance(ref.getProject());
                        return psiManager.createTypeByFQClassName(extension.namedObjectTypeFqn, ref.getResolveScope());
                    }
                }
            }
        }
    }
    return null;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GroovyPsiManager(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiManager) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyMapContentProvider(org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider) PsiElement(com.intellij.psi.PsiElement) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) PsiType(com.intellij.psi.PsiType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

PsiClassReferenceType (com.intellij.psi.impl.source.PsiClassReferenceType)10 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 LightClassReference (com.intellij.psi.impl.light.LightClassReference)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 PsiType (com.intellij.psi.PsiType)1 ClsJavaCodeReferenceElementImpl (com.intellij.psi.impl.compiled.ClsJavaCodeReferenceElementImpl)1 JavaClassReferenceListElementType (com.intellij.psi.impl.java.stubs.JavaClassReferenceListElementType)1 JavaStubPsiElement (com.intellij.psi.impl.source.JavaStubPsiElement)1 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1 PsiJavaCodeReferenceElementImpl (com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 IElementType (com.intellij.psi.tree.IElementType)1 SmartList (com.intellij.util.SmartList)1 FieldEntity (org.gsonformat.intellij.entity.FieldEntity)1 IterableFieldEntity (org.gsonformat.intellij.entity.IterableFieldEntity)1 GroovyMapContentProvider (org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider)1