Search in sources :

Example 1 with FieldVisitor

use of org.jetbrains.org.objectweb.asm.FieldVisitor in project kotlin by JetBrains.

the class FileBasedKotlinClass method visitMembers.

@Override
public void visitMembers(@NotNull final MemberVisitor memberVisitor, @Nullable byte[] cachedContents) {
    byte[] fileContents = cachedContents != null ? cachedContents : getFileContents();
    new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {

        @Override
        public FieldVisitor visitField(int access, @NotNull String name, @NotNull String desc, String signature, Object value) {
            final AnnotationVisitor v = memberVisitor.visitField(Name.identifier(name), desc, value);
            if (v == null)
                return null;
            return new FieldVisitor(ASM5) {

                @Override
                public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
                    return convertAnnotationVisitor(v, desc, innerClasses);
                }

                @Override
                public void visitEnd() {
                    v.visitEnd();
                }
            };
        }

        @Override
        public MethodVisitor visitMethod(int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions) {
            final MethodAnnotationVisitor v = memberVisitor.visitMethod(Name.identifier(name), desc);
            if (v == null)
                return null;
            return new MethodVisitor(ASM5) {

                @Override
                public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
                    return convertAnnotationVisitor(v, desc, innerClasses);
                }

                @Override
                public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitParameterAnnotation(int parameter, @NotNull String desc, boolean visible) {
                    AnnotationArgumentVisitor av = v.visitParameterAnnotation(parameter, resolveNameByDesc(desc, innerClasses), SourceElement.NO_SOURCE);
                    return av == null ? null : convertAnnotationVisitor(av, innerClasses);
                }

                @Override
                public void visitEnd() {
                    v.visitEnd();
                }
            };
        }
    }, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
}
Also used : ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) FieldVisitor(org.jetbrains.org.objectweb.asm.FieldVisitor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) ReadKotlinClassHeaderAnnotationVisitor(org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader)

Example 2 with FieldVisitor

use of org.jetbrains.org.objectweb.asm.FieldVisitor in project kotlin by JetBrains.

the class PropertyCodegen method generateBackingField.

private void generateBackingField(KtNamedDeclaration element, PropertyDescriptor propertyDescriptor, boolean isDelegate, KotlinType kotlinType, Object defaultValue, Annotations annotations) {
    int modifiers = getDeprecatedAccessFlag(propertyDescriptor);
    for (AnnotationCodegen.JvmFlagAnnotation flagAnnotation : AnnotationCodegen.FIELD_FLAGS) {
        if (flagAnnotation.hasAnnotation(propertyDescriptor.getOriginal())) {
            modifiers |= flagAnnotation.getJvmFlag();
        }
    }
    if (kind == OwnerKind.PACKAGE) {
        modifiers |= ACC_STATIC;
    }
    if (!propertyDescriptor.isLateInit() && (!propertyDescriptor.isVar() || isDelegate)) {
        modifiers |= ACC_FINAL;
    }
    if (AnnotationUtilKt.hasJvmSyntheticAnnotation(propertyDescriptor)) {
        modifiers |= ACC_SYNTHETIC;
    }
    Type type = typeMapper.mapType(kotlinType);
    ClassBuilder builder = v;
    FieldOwnerContext backingFieldContext = context;
    if (AsmUtil.isInstancePropertyWithStaticBackingField(propertyDescriptor)) {
        modifiers |= ACC_STATIC;
        if (JvmAbi.isPropertyWithBackingFieldInOuterClass(propertyDescriptor)) {
            ImplementationBodyCodegen codegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
            builder = codegen.v;
            backingFieldContext = codegen.context;
        }
    }
    modifiers |= getVisibilityForBackingField(propertyDescriptor, isDelegate);
    if (AsmUtil.isPropertyWithBackingFieldCopyInOuterClass(propertyDescriptor)) {
        ImplementationBodyCodegen parentBodyCodegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
        parentBodyCodegen.addCompanionObjectPropertyToCopy(propertyDescriptor, defaultValue);
    }
    String name = backingFieldContext.getFieldName(propertyDescriptor, isDelegate);
    v.getSerializationBindings().put(FIELD_FOR_PROPERTY, propertyDescriptor, Pair.create(type, name));
    FieldVisitor fv = builder.newField(JvmDeclarationOriginKt.OtherOrigin(element, propertyDescriptor), modifiers, name, type.getDescriptor(), isDelegate ? null : typeMapper.mapFieldSignature(kotlinType, propertyDescriptor), defaultValue);
    Annotated fieldAnnotated = new AnnotatedWithFakeAnnotations(propertyDescriptor, annotations);
    AnnotationCodegen.forField(fv, memberCodegen, typeMapper).genAnnotations(fieldAnnotated, type, isDelegate ? AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD : AnnotationUseSiteTarget.FIELD);
}
Also used : Annotated(org.jetbrains.kotlin.descriptors.annotations.Annotated) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type(org.jetbrains.org.objectweb.asm.Type) AnnotatedWithFakeAnnotations(org.jetbrains.kotlin.codegen.annotation.AnnotatedWithFakeAnnotations) FieldVisitor(org.jetbrains.org.objectweb.asm.FieldVisitor)

Example 3 with FieldVisitor

use of org.jetbrains.org.objectweb.asm.FieldVisitor in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateCompanionObjectBackingFieldCopies.

private void generateCompanionObjectBackingFieldCopies() {
    if (companionObjectPropertiesToCopy == null)
        return;
    for (PropertyAndDefaultValue info : companionObjectPropertiesToCopy) {
        PropertyDescriptor property = info.descriptor;
        Type type = typeMapper.mapType(property);
        int modifiers = ACC_STATIC | ACC_FINAL | ACC_PUBLIC;
        FieldVisitor fv = v.newField(JvmDeclarationOriginKt.Synthetic(DescriptorToSourceUtils.descriptorToDeclaration(property), property), modifiers, context.getFieldName(property, false), type.getDescriptor(), typeMapper.mapFieldSignature(property.getType(), property), info.defaultValue);
        AnnotationCodegen.forField(fv, this, typeMapper).genAnnotations(property, type);
        // TODO: test this code
        if (state.getClassBuilderMode().generateBodies && info.defaultValue == null) {
            ExpressionCodegen codegen = createOrGetClInitCodegen();
            int companionObjectIndex = putCompanionObjectInLocalVar(codegen);
            StackValue.local(companionObjectIndex, OBJECT_TYPE).put(OBJECT_TYPE, codegen.v);
            copyFieldFromCompanionObject(property);
        }
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) FieldVisitor(org.jetbrains.org.objectweb.asm.FieldVisitor)

Example 4 with FieldVisitor

use of org.jetbrains.org.objectweb.asm.FieldVisitor in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateEnumEntries.

private void generateEnumEntries() {
    if (descriptor.getKind() != ClassKind.ENUM_CLASS)
        return;
    List<KtEnumEntry> enumEntries = CollectionsKt.filterIsInstance(element.getDeclarations(), KtEnumEntry.class);
    for (KtEnumEntry enumEntry : enumEntries) {
        ClassDescriptor descriptor = getNotNull(bindingContext, BindingContext.CLASS, enumEntry);
        int isDeprecated = KotlinBuiltIns.isDeprecated(descriptor) ? ACC_DEPRECATED : 0;
        FieldVisitor fv = v.newField(JvmDeclarationOriginKt.OtherOrigin(enumEntry, descriptor), ACC_PUBLIC | ACC_ENUM | ACC_STATIC | ACC_FINAL | isDeprecated, descriptor.getName().asString(), classAsmType.getDescriptor(), null, null);
        AnnotationCodegen.forField(fv, this, typeMapper).genAnnotations(descriptor, null);
    }
    initializeEnumConstants(enumEntries);
}
Also used : JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) FieldVisitor(org.jetbrains.org.objectweb.asm.FieldVisitor)

Aggregations

FieldVisitor (org.jetbrains.org.objectweb.asm.FieldVisitor)4 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 Type (org.jetbrains.org.objectweb.asm.Type)2 AnnotatedWithFakeAnnotations (org.jetbrains.kotlin.codegen.annotation.AnnotatedWithFakeAnnotations)1 Annotated (org.jetbrains.kotlin.descriptors.annotations.Annotated)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 ReadKotlinClassHeaderAnnotationVisitor (org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor)1 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)1 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1 Type.getObjectType (org.jetbrains.org.objectweb.asm.Type.getObjectType)1