Search in sources :

Example 1 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class ResolveSessionUtils method getClassOrObjectDescriptorsByFqName.

@NotNull
public static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName(@NotNull ModuleDescriptor module, @NotNull FqName fqName, @NotNull Predicate<ClassDescriptor> filter) {
    if (fqName.isRoot())
        return Collections.emptyList();
    Collection<ClassDescriptor> result = new ArrayList<ClassDescriptor>(1);
    FqName packageFqName = fqName.parent();
    while (true) {
        PackageViewDescriptor packageDescriptor = module.getPackage(packageFqName);
        if (!packageDescriptor.isEmpty()) {
            FqName relativeClassFqName = FqNamesUtilKt.tail(fqName, packageFqName);
            ClassDescriptor classDescriptor = findClassByRelativePath(packageDescriptor.getMemberScope(), relativeClassFqName);
            if (classDescriptor != null && filter.apply(classDescriptor)) {
                result.add(classDescriptor);
            }
        }
        if (packageFqName.isRoot()) {
            break;
        }
        packageFqName = packageFqName.parent();
    }
    return result;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ArrayList(java.util.ArrayList) PackageViewDescriptor(org.jetbrains.kotlin.descriptors.PackageViewDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class CommonSupertypes method computeSupertypeProjections.

// constructor - type constructor of a supertype to be instantiated
// types - instantiations of constructor occurring as supertypes of classes we are trying to intersect
@NotNull
private static SimpleType computeSupertypeProjections(@NotNull TypeConstructor constructor, @NotNull Set<SimpleType> types, int recursionDepth, int maxDepth) {
    assert !types.isEmpty();
    if (types.size() == 1) {
        return types.iterator().next();
    }
    List<TypeParameterDescriptor> parameters = constructor.getParameters();
    List<TypeProjection> newProjections = new ArrayList<TypeProjection>(parameters.size());
    for (TypeParameterDescriptor parameterDescriptor : parameters) {
        Set<TypeProjection> typeProjections = new HashSet<TypeProjection>();
        for (KotlinType type : types) {
            typeProjections.add(type.getArguments().get(parameterDescriptor.getIndex()));
        }
        newProjections.add(computeSupertypeProjection(parameterDescriptor, typeProjections, recursionDepth, maxDepth));
    }
    boolean nullable = false;
    for (KotlinType type : types) {
        nullable |= type.isMarkedNullable();
    }
    ClassifierDescriptor classifier = constructor.getDeclarationDescriptor();
    MemberScope newScope;
    if (classifier instanceof ClassDescriptor) {
        newScope = ((ClassDescriptor) classifier).getMemberScope(newProjections);
    } else if (classifier instanceof TypeParameterDescriptor) {
        newScope = classifier.getDefaultType().getMemberScope();
    } else {
        newScope = ErrorUtils.createErrorScope("A scope for common supertype which is not a normal classifier", true);
    }
    return KotlinTypeFactory.simpleType(Annotations.Companion.getEMPTY(), constructor, newProjections, nullable, newScope);
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class FieldInfo method createForSingleton.

@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor classDescriptor, @NotNull KotlinTypeMapper typeMapper) {
    if (!classDescriptor.getKind().isSingleton() || DescriptorUtils.isEnumEntry(classDescriptor)) {
        throw new UnsupportedOperationException("Can't create singleton field for class: " + classDescriptor);
    }
    if (isNonCompanionObject(classDescriptor) || CompanionObjectMapping.INSTANCE.isMappedIntrinsicCompanionObject(classDescriptor)) {
        return createSingletonViaInstance(classDescriptor, typeMapper);
    }
    ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
    assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor;
    Type ownerType = typeMapper.mapType(ownerDescriptor);
    return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), classDescriptor.getName().asString(), true);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class CompileTimeConstantUtils method isAcceptableTypeForAnnotationParameter.

private static boolean isAcceptableTypeForAnnotationParameter(@NotNull KotlinType parameterType) {
    ClassDescriptor typeDescriptor = TypeUtils.getClassDescriptor(parameterType);
    if (typeDescriptor == null) {
        return false;
    }
    if (isEnumClass(typeDescriptor) || isAnnotationClass(typeDescriptor) || KotlinBuiltIns.isKClass(typeDescriptor) || KotlinBuiltIns.isPrimitiveArray(parameterType) || KotlinBuiltIns.isPrimitiveType(parameterType) || KotlinBuiltIns.isString(parameterType)) {
        return true;
    }
    if (KotlinBuiltIns.isArray(parameterType)) {
        List<TypeProjection> arguments = parameterType.getArguments();
        if (arguments.size() == 1) {
            KotlinType arrayType = arguments.get(0).getType();
            if (arrayType.isMarkedNullable()) {
                return false;
            }
            ClassDescriptor arrayTypeDescriptor = TypeUtils.getClassDescriptor(arrayType);
            if (arrayTypeDescriptor != null) {
                return isEnumClass(arrayTypeDescriptor) || isAnnotationClass(arrayTypeDescriptor) || KotlinBuiltIns.isKClass(arrayTypeDescriptor) || KotlinBuiltIns.isString(arrayType);
            }
        }
    }
    return false;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) TypeProjection(org.jetbrains.kotlin.types.TypeProjection) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 5 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class AbstractClassTypeConstructor method hashCode.

@Override
public final int hashCode() {
    int currentHashCode = hashCode;
    if (currentHashCode != 0)
        return currentHashCode;
    ClassifierDescriptor descriptor = getDeclarationDescriptor();
    if (descriptor instanceof ClassDescriptor && hasMeaningfulFqName(descriptor)) {
        currentHashCode = DescriptorUtils.getFqName(descriptor).hashCode();
    } else {
        currentHashCode = System.identityHashCode(this);
    }
    hashCode = currentHashCode;
    return currentHashCode;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor)

Aggregations

ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)17 NotNull (org.jetbrains.annotations.NotNull)5 ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)5 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)3 MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)3 ArrayList (java.util.ArrayList)2 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)2 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)2 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)2 KtClass (org.jetbrains.kotlin.psi.KtClass)2 KtDeclaration (org.jetbrains.kotlin.psi.KtDeclaration)2 KtFile (org.jetbrains.kotlin.psi.KtFile)2 Type (org.jetbrains.org.objectweb.asm.Type)2 Disposable (com.intellij.openapi.Disposable)1 PsiMethod (com.intellij.psi.PsiMethod)1 File (java.io.File)1 ZipEntry (java.util.zip.ZipEntry)1 TestRunner (junit.textui.TestRunner)1 Unit (kotlin.Unit)1 Nullable (org.jetbrains.annotations.Nullable)1