Search in sources :

Example 1 with CaptureField

use of dyvilx.tools.compiler.ast.field.capture.CaptureField in project Dyvil by Dyvil.

the class AnonymousClassMetadata method write.

@Override
public void write(ClassWriter writer) throws BytecodeException {
    final CaptureHelper<CaptureField> captureHelper = this.theClass.captureHelper;
    final FieldThis thisField = this.theClass.thisField;
    final IConstructor constructor = this.theClass.constructor;
    captureHelper.writeCaptureFields(writer);
    final MethodWriter initWriter = new MethodWriterImpl(writer, writer.visitMethod(Modifiers.MANDATED, "<init>", this.theClass.getConstructorDesc(), null, null));
    final ParameterList parameterList = constructor.getParameters();
    final int parameterCount = parameterList.size();
    // Signature & Parameter Data
    initWriter.setThisType(this.theClass.getInternalName());
    parameterList.write(initWriter);
    int index = initWriter.localCount();
    int thisIndex = index;
    if (thisField != null) {
        thisField.writeField(writer);
        index = initWriter.visitParameter(index, thisField.getName(), thisField.getTargetClass().getThisType(), Modifiers.MANDATED);
    }
    captureHelper.writeCaptureParameters(initWriter, index);
    // Constructor Body
    initWriter.visitCode();
    initWriter.visitVarInsn(Opcodes.ALOAD, 0);
    for (int i = 0; i < parameterCount; i++) {
        parameterList.get(i).writeGet(initWriter);
    }
    constructor.writeInvoke(initWriter, 0);
    if (thisField != null) {
        initWriter.visitVarInsn(Opcodes.ALOAD, 0);
        initWriter.visitVarInsn(Opcodes.ALOAD, thisIndex);
        initWriter.visitFieldInsn(Opcodes.PUTFIELD, this.theClass.getInternalName(), thisField.getName(), thisField.getDescriptor());
    }
    captureHelper.writeFieldAssignments(initWriter);
    this.theClass.writeClassInit(initWriter);
    initWriter.visitEnd(Types.VOID);
}
Also used : MethodWriterImpl(dyvilx.tools.compiler.backend.MethodWriterImpl) IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) MethodWriter(dyvilx.tools.compiler.backend.MethodWriter) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) CaptureField(dyvilx.tools.compiler.ast.field.capture.CaptureField)

Aggregations

IConstructor (dyvilx.tools.compiler.ast.constructor.IConstructor)1 CaptureField (dyvilx.tools.compiler.ast.field.capture.CaptureField)1 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)1 MethodWriter (dyvilx.tools.compiler.backend.MethodWriter)1 MethodWriterImpl (dyvilx.tools.compiler.backend.MethodWriterImpl)1