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