Search in sources :

Example 1 with JvmClassSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateDeclaration.

@Override
protected void generateDeclaration() {
    getSuperClass();
    JvmClassSignature signature = signature();
    boolean isAbstract = false;
    boolean isInterface = false;
    boolean isFinal = false;
    boolean isAnnotation = false;
    boolean isEnum = false;
    ClassKind kind = descriptor.getKind();
    Modality modality = descriptor.getModality();
    if (modality == Modality.ABSTRACT || modality == Modality.SEALED) {
        isAbstract = true;
    }
    if (kind == ClassKind.INTERFACE) {
        isAbstract = true;
        isInterface = true;
    } else if (kind == ClassKind.ANNOTATION_CLASS) {
        isAbstract = true;
        isInterface = true;
        isAnnotation = true;
    } else if (kind == ClassKind.ENUM_CLASS) {
        isAbstract = hasAbstractMembers(descriptor);
        isEnum = true;
    }
    if (modality != Modality.OPEN && !isAbstract) {
        isFinal = kind == ClassKind.OBJECT || // Light-class mode: Do not make enum classes final since PsiClass corresponding to enum is expected to be inheritable from
        !(kind == ClassKind.ENUM_CLASS && !state.getClassBuilderMode().generateBodies);
    }
    int access = 0;
    if (!state.getClassBuilderMode().generateBodies && !DescriptorUtils.isTopLevelDeclaration(descriptor)) {
        // !ClassBuilderMode.generateBodies means we are generating light classes & looking at a nested or inner class
        // Light class generation is implemented so that Cls-classes only read bare code of classes,
        // without knowing whether these classes are inner or not (see ClassStubBuilder.EMPTY_STRATEGY)
        // Thus we must write full accessibility flags on inner classes in this mode
        access |= getVisibilityAccessFlag(descriptor);
        // Same for STATIC
        if (!descriptor.isInner()) {
            access |= ACC_STATIC;
        }
    } else {
        access |= getVisibilityAccessFlagForClass(descriptor);
    }
    if (isAbstract) {
        access |= ACC_ABSTRACT;
    }
    if (isInterface) {
        // ACC_SUPER
        access |= ACC_INTERFACE;
    } else {
        access |= ACC_SUPER;
    }
    if (isFinal) {
        access |= ACC_FINAL;
    }
    if (isAnnotation) {
        access |= ACC_ANNOTATION;
    }
    if (KotlinBuiltIns.isDeprecated(descriptor)) {
        access |= ACC_DEPRECATED;
    }
    if (isEnum) {
        for (KtDeclaration declaration : myClass.getDeclarations()) {
            if (declaration instanceof KtEnumEntry) {
                if (enumEntryNeedSubclass(bindingContext, (KtEnumEntry) declaration)) {
                    access &= ~ACC_FINAL;
                }
            }
        }
        access |= ACC_ENUM;
    }
    v.defineClass(myClass.getPsiOrParent(), state.getClassFileVersion(), access, signature.getName(), signature.getJavaGenericSignature(), signature.getSuperclassName(), ArrayUtil.toStringArray(signature.getInterfaces()));
    v.visitSource(myClass.getContainingKtFile().getName(), null);
    InlineCodegenUtil.initDefaultSourceMappingIfNeeded(context, this, state);
    writeEnclosingMethod();
    AnnotationCodegen.forClass(v.getVisitor(), this, typeMapper).genAnnotations(descriptor, null);
    generateEnumEntries();
}
Also used : JvmClassSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature)

Example 2 with JvmClassSignature

use of org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature in project kotlin by JetBrains.

the class ImplementationBodyCodegen method signature.

@NotNull
public static JvmClassSignature signature(@NotNull ClassDescriptor descriptor, @NotNull Type classAsmType, @NotNull SuperClassInfo superClassInfo, @NotNull KotlinTypeMapper typeMapper) {
    JvmSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS);
    typeMapper.writeFormalTypeParameters(descriptor.getDeclaredTypeParameters(), sw);
    sw.writeSuperclass();
    if (superClassInfo.getKotlinType() == null) {
        sw.writeClassBegin(superClassInfo.getType());
        sw.writeClassEnd();
    } else {
        typeMapper.mapSupertype(superClassInfo.getKotlinType(), sw);
    }
    sw.writeSuperclassEnd();
    LinkedHashSet<String> superInterfaces = new LinkedHashSet<String>();
    Set<String> kotlinMarkerInterfaces = new LinkedHashSet<String>();
    for (KotlinType supertype : descriptor.getTypeConstructor().getSupertypes()) {
        if (isJvmInterface(supertype.getConstructor().getDeclarationDescriptor())) {
            sw.writeInterface();
            Type jvmInterfaceType = typeMapper.mapSupertype(supertype, sw);
            sw.writeInterfaceEnd();
            String jvmInterfaceInternalName = jvmInterfaceType.getInternalName();
            superInterfaces.add(jvmInterfaceInternalName);
            FqName kotlinInterfaceName = DescriptorUtils.getFqName(supertype.getConstructor().getDeclarationDescriptor()).toSafe();
            String kotlinMarkerInterfaceInternalName = KOTLIN_MARKER_INTERFACES.get(kotlinInterfaceName);
            if (kotlinMarkerInterfaceInternalName != null) {
                kotlinMarkerInterfaces.add(kotlinMarkerInterfaceInternalName);
            }
        }
    }
    for (String kotlinMarkerInterface : kotlinMarkerInterfaces) {
        sw.writeInterface();
        sw.writeAsmType(getObjectType(kotlinMarkerInterface));
        sw.writeInterfaceEnd();
    }
    superInterfaces.addAll(kotlinMarkerInterfaces);
    return new JvmClassSignature(classAsmType.getInternalName(), superClassInfo.getType().getInternalName(), new ArrayList<String>(superInterfaces), sw.makeJavaGenericSignature());
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) JvmSignatureWriter(org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter) BothSignatureWriter(org.jetbrains.kotlin.codegen.signature.BothSignatureWriter) FqName(org.jetbrains.kotlin.name.FqName) KotlinType(org.jetbrains.kotlin.types.KotlinType) JvmClassSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature) NotNull(org.jetbrains.annotations.NotNull) BindingContextUtils.getNotNull(org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)

Aggregations

JvmClassSignature (org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature)2 NotNull (org.jetbrains.annotations.NotNull)1 BothSignatureWriter (org.jetbrains.kotlin.codegen.signature.BothSignatureWriter)1 JvmSignatureWriter (org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter)1 FqName (org.jetbrains.kotlin.name.FqName)1 BindingContextUtils.getNotNull (org.jetbrains.kotlin.resolve.BindingContextUtils.getNotNull)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 Type (org.jetbrains.org.objectweb.asm.Type)1 Type.getObjectType (org.jetbrains.org.objectweb.asm.Type.getObjectType)1