001package io.ebean.enhance.entity;
002
003import io.ebean.enhance.asm.MethodVisitor;
004
005import java.util.ArrayList;
006import java.util.List;
007
008/**
009 * Code that initialises a transient field.
010 * <p>
011 * Added into a default constructor when that is added to an entity bean.
012 */
013public final class CapturedInitCode {
014
015  private final String name;
016  private final String type;
017  private final List<DeferredCode> code;
018
019  CapturedInitCode(List<DeferredCode> codes, int opcode, String owner, String name, String desc, String type) {
020    this.name = name;
021    this.type = type;
022    this.code = new ArrayList<>(codes);
023    this.code.add(new PutField(opcode, owner, name, desc));
024  }
025
026  /**
027   * Return the field name.
028   */
029  public String name() {
030    return name;
031  }
032
033  /**
034   * Return the concrete type the field is initialised to.
035   */
036  public String type() {
037    return type;
038  }
039
040  /**
041   * Write the code that initialises the field.
042   */
043  public void write(ConstructorAdapter mv) {
044    for (DeferredCode deferredCode : code) {
045      deferredCode.write(mv);
046    }
047  }
048
049  public String mismatch(String type2) {
050    return "type1:" + type + " type2:" + type2;
051  }
052
053  private static class PutField implements DeferredCode {
054    final int opcode;
055    final String owner, name, desc;
056
057    public PutField(int opcode, String owner, String name, String desc) {
058      this.opcode = opcode;
059      this.owner = owner;
060      this.name = name;
061      this.desc = desc;
062    }
063
064    @Override
065    public void write(MethodVisitor mv) {
066      mv.visitFieldInsn(opcode, owner, name, desc);
067    }
068  }
069
070}