Search in sources :

Example 1 with FieldVisitor

use of dyvilx.tools.asm.FieldVisitor in project Dyvil by Dyvil.

the class AnonymousClassLMF method spinInnerClass.

private Class<?> spinInnerClass() throws LambdaConversionException {
    String samIntf = TypeConverter.getInternalName(this.samBase);
    this.cw.visit(CLASSFILE_VERSION, ASMConstants.ACC_SUPER | FINAL | SYNTHETIC, this.lambdaClassName, null, "java/lang/Object", new String[] { samIntf });
    if (this.parameterCount != 0) {
        for (int i = 0; i < this.parameterCount; i++) {
            FieldVisitor fv = this.cw.visitField(PRIVATE | FINAL, this.argNames[i], this.argDescs[i], null, null);
            fv.visitEnd();
        }
        this.generateFactory();
    }
    this.generateConstructor();
    this.generateToString();
    this.generateSAM();
    this.cw.visitEnd();
    final byte[] bytes = this.cw.toByteArray();
    BytecodeDump.dump(bytes, this.lambdaClassName);
    // Define the generated class in this VM.
    return UNSAFE.defineAnonymousClass(this.targetClass, bytes, null);
}
Also used : FieldVisitor(dyvilx.tools.asm.FieldVisitor)

Example 2 with FieldVisitor

use of dyvilx.tools.asm.FieldVisitor in project Dyvil by Dyvil.

the class AnnotationProxyFactory method spinInnerClass.

private Class<?> spinInnerClass() {
    final String annotationItf = this.annotationType.getName().replace('.', '/');
    final ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classWriter.visit(CLASSFILE_VERSION, ASMConstants.ACC_SUPER | FINAL | SYNTHETIC, this.className, null, "java/lang/Object", new String[] { annotationItf });
    if (this.parameterCount > 0) {
        for (int i = 0; i < this.parameterCount; i++) {
            FieldVisitor fv = classWriter.visitField(PRIVATE | FINAL, this.argNames[i], this.argDescs[i], null, null);
            fv.visitEnd();
        }
        this.generateFactory(classWriter);
    }
    this.generateConstructor(classWriter);
    this.generateAnnotationType(classWriter);
    this.generateMethods(classWriter);
    this.generateToString(classWriter);
    classWriter.visitEnd();
    final byte[] bytes = classWriter.toByteArray();
    BytecodeDump.dump(bytes, this.className);
    // Define the generated class in this VM.
    return UNSAFE.defineAnonymousClass(this.targetClass, bytes, null);
}
Also used : FieldVisitor(dyvilx.tools.asm.FieldVisitor) ClassWriter(dyvilx.tools.asm.ClassWriter)

Example 3 with FieldVisitor

use of dyvilx.tools.asm.FieldVisitor in project Dyvil by Dyvil.

the class Field method write.

@Override
public void write(ClassWriter writer) throws BytecodeException {
    final long flags = ModifierUtil.getFlags(this);
    final int modifiers = ModifierUtil.getJavaModifiers(flags);
    final String name = this.getInternalName();
    final String descriptor = this.getDescriptor();
    final String signature = this.getType().needsSignature() ? this.getSignature() : null;
    final Object value = this.getObjectValue();
    final FieldVisitor fieldVisitor = writer.visitField(modifiers, name, descriptor, signature, value);
    this.writeAnnotations(fieldVisitor, flags);
    fieldVisitor.visitEnd();
    if (this.property != null) {
        this.property.write(writer);
    }
    if (!this.hasModifier(Modifiers.LAZY)) {
        return;
    }
    final String lazyName = name + "$lazy";
    final String ownerClass = this.enclosingClass.getInternalName();
    final boolean isStatic = (flags & Modifiers.STATIC) != 0;
    writer.visitField(isStatic ? Modifiers.PRIVATE | Modifiers.STATIC : Modifiers.PRIVATE, lazyName, "Z", null, null);
    final MethodWriter access = new MethodWriterImpl(writer, writer.visitMethod(modifiers, lazyName, "()" + descriptor, null, null));
    access.visitCode();
    // Get the $lazy flag
    int getOpcode;
    if (!isStatic) {
        access.setLocalType(0, ownerClass);
        access.visitVarInsn(Opcodes.ALOAD, 0);
        access.visitFieldInsn(getOpcode = Opcodes.GETFIELD, ownerClass, lazyName, "Z");
    } else {
        access.visitFieldInsn(getOpcode = Opcodes.GETSTATIC, ownerClass, lazyName, "Z");
    }
    Label label = new Label();
    final int returnOpcode = this.type.getReturnOpcode();
    // if (this.fieldName$lazy) {
    access.visitJumpInsn(Opcodes.IFEQ, label);
    if (!isStatic) {
        access.visitVarInsn(Opcodes.ALOAD, 0);
    }
    // this.fieldName ->
    access.visitFieldInsn(getOpcode, ownerClass, name, descriptor);
    // return
    access.visitInsn(returnOpcode);
    // }
    access.visitTargetLabel(label);
    if (!isStatic) {
        access.visitVarInsn(Opcodes.ALOAD, 0);
        access.visitInsn(Opcodes.DUP);
        access.visitLdcInsn(1);
        access.visitFieldInsn(Opcodes.PUTFIELD, ownerClass, lazyName, "Z");
        this.value.writeExpression(access, this.type);
        access.visitInsn(Opcodes.AUTO_DUP_X1);
        access.visitFieldInsn(Opcodes.PUTFIELD, ownerClass, name, descriptor);
    } else {
        access.visitLdcInsn(1);
        access.visitFieldInsn(Opcodes.PUTSTATIC, ownerClass, lazyName, "Z");
        this.value.writeExpression(access, this.type);
        access.visitInsn(Opcodes.AUTO_DUP);
        access.visitFieldInsn(Opcodes.PUTSTATIC, ownerClass, name, descriptor);
    }
    access.visitInsn(returnOpcode);
    access.visitEnd();
}
Also used : MethodWriterImpl(dyvilx.tools.compiler.backend.MethodWriterImpl) MethodWriter(dyvilx.tools.compiler.backend.MethodWriter) Label(dyvilx.tools.asm.Label) FieldVisitor(dyvilx.tools.asm.FieldVisitor)

Aggregations

FieldVisitor (dyvilx.tools.asm.FieldVisitor)3 ClassWriter (dyvilx.tools.asm.ClassWriter)1 Label (dyvilx.tools.asm.Label)1 MethodWriter (dyvilx.tools.compiler.backend.MethodWriter)1 MethodWriterImpl (dyvilx.tools.compiler.backend.MethodWriterImpl)1