001package io.ebean.enhance.querybean;
002
003import io.ebean.enhance.asm.ClassVisitor;
004import io.ebean.enhance.asm.Label;
005import io.ebean.enhance.asm.Opcodes;
006
007import java.util.List;
008
009import static io.ebean.enhance.common.EnhanceConstants.INIT;
010
011/**
012 * Overrides the constructor to initialise all the fields (for use with 'Alias' select()/fetch() use).
013 */
014public class TypeQueryConstructorForAlias extends BaseConstructorAdapter implements Opcodes, Constants {
015
016  private final ClassInfo classInfo;
017
018  private final ClassVisitor cv;
019
020  /**
021  * Construct for a query bean class and a class visitor.
022  */
023  public TypeQueryConstructorForAlias(ClassInfo classInfo, ClassVisitor cv) {
024    super();
025    this.cv = cv;
026    this.classInfo = classInfo;
027  }
028
029  /**
030  * Write the constructor initialising all the fields eagerly.
031  */
032  @Override
033  public void visitCode() {
034
035    mv = cv.visitMethod(ACC_PRIVATE, INIT, "(Z)V", null, null);
036    mv.visitCode();
037    Label l0 = new Label();
038    mv.visitLabel(l0);
039    mv.visitLineNumber(1, l0);
040    mv.visitVarInsn(ALOAD, 0);
041    mv.visitVarInsn(ILOAD, 1);
042    mv.visitMethodInsn(INVOKESPECIAL, TQ_ROOT_BEAN, INIT, "(Z)V", false);
043    Label l1 = new Label();
044    mv.visitLabel(l1);
045    mv.visitLineNumber(2, l1);
046    mv.visitVarInsn(ALOAD, 0);
047    mv.visitVarInsn(ALOAD, 0);
048    mv.visitMethodInsn(INVOKEVIRTUAL, classInfo.getClassName(), "setRoot", "(Ljava/lang/Object;)V", false);
049
050
051    // init all the properties
052    List<FieldInfo> fields = classInfo.getFields();
053    if (fields != null) {
054      for (FieldInfo field : fields) {
055        field.writeFieldInit(mv);
056      }
057    }
058
059    Label l2 = new Label();
060    mv.visitLabel(l2);
061    mv.visitLineNumber(3, l2);
062    mv.visitInsn(RETURN);
063    Label l12 = new Label();
064    mv.visitLabel(l12);
065    mv.visitLocalVariable("this", "L" + classInfo.getClassName() + ";", null, l0, l12, 0);
066    mv.visitLocalVariable("alias", "Z", null, l0, l12, 1);
067    mv.visitMaxs(5, 2);
068    mv.visitEnd();
069  }
070
071}