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 the _ebean_newInstance() method.
014 */
015public class MethodNewInstance {
016
017  /**
018  * Add the _ebean_newInstance() method.
019  */
020  public static void addMethod(ClassVisitor cv, ClassMeta classMeta) {
021
022    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_ebean_newInstance", "()Ljava/lang/Object;", null, null);
023    mv.visitCode();
024    Label l0 = new Label();
025    mv.visitLabel(l0);
026    mv.visitLineNumber(10, l0);
027    mv.visitTypeInsn(Opcodes.NEW, classMeta.getClassName());
028    mv.visitInsn(Opcodes.DUP);
029    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classMeta.getClassName(), INIT, NOARG_VOID, false);
030    mv.visitInsn(Opcodes.ARETURN);
031
032    Label l1 = new Label();
033    mv.visitLabel(l1);
034    mv.visitLocalVariable("this", "L" + classMeta.getClassName() + ";", null, l0, l1, 0);
035    mv.visitMaxs(2, 1);
036    mv.visitEnd();
037  }
038}