001package io.ebean.enhance.entity;
002
003import io.ebean.enhance.asm.ClassVisitor;
004import io.ebean.enhance.asm.Label;
005import io.ebean.enhance.asm.MethodVisitor;
006import io.ebean.enhance.asm.Opcodes;
007import io.ebean.enhance.common.ClassMeta;
008
009import static io.ebean.enhance.common.EnhanceConstants.INIT;
010import static io.ebean.enhance.common.EnhanceConstants.NOARG_VOID;
011
012/**
013 * Adds a default constructor for the cases when there is not one already defined.
014 */
015public class DefaultConstructor {
016
017    /**
018    * Adds a default constructor.
019    */
020    public static void add(ClassVisitor cw, ClassMeta classMeta) {
021
022        if (classMeta.isLog(3)) {
023            classMeta.log("... adding default constructor, super class: "+classMeta.getSuperClassName());
024        }
025
026        MethodVisitor underlyingMV = cw.visitMethod(Opcodes.ACC_PUBLIC, INIT, NOARG_VOID, null, null);
027
028        ConstructorAdapter mv = new ConstructorAdapter(underlyingMV, classMeta, NOARG_VOID);
029
030        mv.visitCode();
031        Label l0 = new Label();
032        mv.visitLabel(l0);
033        mv.visitLineNumber(1, l0);
034        mv.visitVarInsn(Opcodes.ALOAD, 0);
035        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classMeta.getSuperClassName(), INIT, NOARG_VOID, false);
036        Label l1 = new Label();
037        mv.visitLabel(l1);
038        mv.visitLineNumber(2, l1);
039        mv.visitInsn(Opcodes.RETURN);
040
041        Label l2 = new Label();
042        mv.visitLabel(l2);
043        mv.visitLocalVariable("this", "L"+classMeta.getClassName()+";", null, l0, l2, 0);
044        mv.visitMaxs(1, 1);
045        mv.visitEnd();
046    }
047}