protected void invokePut(
      MethodVisitor mv,
      String wrapperName,
      ClassDefinition core,
      String fieldName,
      FieldDefinition field) {
    mv.visitLdcInsn(fieldName);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(
        INVOKEVIRTUAL,
        Type.getInternalName(String.class),
        "equals",
        "(" + Type.getDescriptor(Object.class) + ")Z");
    Label l1 = new Label();
    mv.visitJumpInsn(IFEQ, l1);

    mv.visitVarInsn(ALOAD, 2);
    if (BuildUtils.isPrimitive(field.getTypeName())) {
      TraitFactory.primitiveValue(mv, field.getTypeName());
      mv.visitVarInsn(BuildUtils.storeType(field.getTypeName()), 3);
      TraitFactory.invokeInjector(mv, wrapperName, trait, core, field, false, 3);
    } else {
      TraitFactory.invokeInjector(mv, wrapperName, trait, core, field, false, 2);
    }

    mv.visitVarInsn(ALOAD, 2);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l1);
  }
  private void invokeRemove(
      MethodVisitor mv,
      String wrapperName,
      ClassDefinition core,
      String fieldName,
      FieldDefinition field) {
    mv.visitLdcInsn(fieldName);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(
        INVOKEVIRTUAL,
        Type.getInternalName(String.class),
        "equals",
        "(" + Type.getDescriptor(Object.class) + ")Z");
    Label l1 = new Label();
    mv.visitJumpInsn(IFEQ, l1);

    TraitFactory.invokeExtractor(mv, wrapperName, trait, core, field);

    if (BuildUtils.isPrimitive(field.getTypeName())) {
      TraitFactory.valueOf(mv, field.getTypeName());
    }
    mv.visitVarInsn(ASTORE, 2);

    TraitFactory.invokeInjector(mv, wrapperName, trait, core, field, true, 1);

    mv.visitVarInsn(ALOAD, 2);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l1);
  }
  private void buildClear(
      ClassWriter cw,
      String wrapperName,
      String coreName,
      ClassDefinition trait,
      ClassDefinition core,
      BitSet mask) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);

    boolean hasPrimitiveFields = false;
    boolean hasObjectFields = false;
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null);
    mv.visitCode();

    for (FieldDefinition field : core.getFieldsDefinitions()) {
      if (field.isKey()) continue;
      if (BuildUtils.isPrimitive(field.getTypeName())) {
        hasPrimitiveFields = true;
      } else {
        hasObjectFields = true;
      }
      TraitFactory.invokeInjector(mv, wrapperName, trait, core, field, true, 1);
    }

    int stack = 2;
    if (hasPrimitiveFields) {
      stack++;
    }
    if (hasObjectFields) {
      stack++;
    }

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "clear", "()V");

    int num = initSoftFields(mv, trait, core, internalWrapper, mask, 0);
    stack += num;

    mv.visitInsn(RETURN);
    //        mv.visitMaxs( stack , 1 );
    mv.visitMaxs(0, 0);
    mv.visitEnd();
  }
  private void buildValues(
      ClassVisitor cw,
      String wrapperName,
      String coreName,
      ClassDefinition trait,
      ClassDefinition core,
      BitSet mask) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);

    MethodVisitor mv =
        cw.visitMethod(
            ACC_PUBLIC,
            "values",
            "()" + Type.getDescriptor(Collection.class),
            "()Ljava/util/Collection<Ljava/lang/Object;>;",
            null);
    mv.visitCode();

    mv.visitTypeInsn(NEW, Type.getInternalName(ArrayList.class));
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(ArrayList.class), "<init>", "()V");
    mv.visitVarInsn(ASTORE, 1);

    for (FieldDefinition field : core.getFieldsDefinitions()) {
      mv.visitVarInsn(ALOAD, 1);

      TraitFactory.invokeExtractor(mv, wrapperName, trait, core, field);

      if (BuildUtils.isPrimitive(field.getTypeName())) {
        TraitFactory.valueOf(mv, field.getTypeName());
      }

      mv.visitMethodInsn(
          INVOKEINTERFACE,
          Type.getInternalName(Collection.class),
          "add",
          "(" + Type.getDescriptor(Object.class) + ")Z");
      mv.visitInsn(POP);
    }

    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitMethodInsn(
        INVOKEINTERFACE,
        Type.getInternalName(Map.class),
        "values",
        "()" + Type.getDescriptor(Collection.class));
    mv.visitMethodInsn(
        INVOKEINTERFACE,
        Type.getInternalName(Collection.class),
        "addAll",
        "(" + Type.getDescriptor(Collection.class) + ")Z");
    mv.visitInsn(POP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ARETURN);

    //        mv.visitMaxs( core.getFieldsDefinitions().size() > 0 ? 3 : 2, 2);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
  }
  private void buildContainsValue(
      ClassWriter cw,
      String wrapperName,
      String coreName,
      ClassDefinition trait,
      ClassDefinition core,
      BitSet mask) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);

    MethodVisitor mv =
        cw.visitMethod(
            ACC_PUBLIC, "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", null, null);
    mv.visitCode();

    // null check
    mv.visitVarInsn(ALOAD, 1);
    Label l99 = new Label();
    mv.visitJumpInsn(IFNONNULL, l99);

    for (FieldDefinition field : core.getFieldsDefinitions()) {
      if (!BuildUtils.isPrimitive(field.getTypeName())) {
        TraitFactory.invokeExtractor(mv, wrapperName, trait, core, field);
        Label l1 = new Label();
        mv.visitJumpInsn(IFNONNULL, l1);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitLabel(l1);
      }
    }

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(
        INVOKEINTERFACE,
        Type.getInternalName(Map.class),
        "containsValue",
        "(" + Type.getDescriptor(Object.class) + ")Z");
    mv.visitInsn(IRETURN);
    mv.visitLabel(l99);

    // non-null values check
    for (FieldDefinition field : core.getFieldsDefinitions()) {

      mv.visitVarInsn(ALOAD, 1);

      TraitFactory.invokeExtractor(mv, wrapperName, trait, core, field);

      if (BuildUtils.isPrimitive(field.getTypeName())) {
        TraitFactory.valueOf(mv, field.getTypeName());
      }
      mv.visitMethodInsn(
          INVOKEVIRTUAL,
          Type.getInternalName(Object.class),
          "equals",
          "(" + Type.getDescriptor(Object.class) + ")Z");

      Label l0 = new Label();
      mv.visitJumpInsn(IFEQ, l0);
      mv.visitInsn(ICONST_1);
      mv.visitInsn(IRETURN);
      mv.visitLabel(l0);
    }

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(
        INVOKEINTERFACE,
        Type.getInternalName(Map.class),
        "containsValue",
        "(" + Type.getDescriptor(Object.class) + ")Z");
    mv.visitInsn(IRETURN);
    //        mv.visitMaxs( core.getFieldsDefinitions().size() > 0 ? 3 : 2, 2 );
    mv.visitMaxs(0, 0);
    mv.visitEnd();
  }
  private int initSoftFields(
      MethodVisitor mv,
      ClassDefinition trait,
      ClassDefinition core,
      String internalWrapper,
      BitSet mask,
      int varNum) {
    int j = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
      boolean isSoftField = TraitRegistry.isSoftField(field, j++, mask);
      if (isSoftField) {

        mv.visitVarInsn(ALOAD, varNum);
        mv.visitLdcInsn(field.resolveAlias());
        mv.visitMethodInsn(
            INVOKEINTERFACE,
            Type.getInternalName(Map.class),
            "containsKey",
            Type.getMethodDescriptor(
                Type.getType(boolean.class), new Type[] {Type.getType(Object.class)}));
        Label l0 = new Label();
        mv.visitJumpInsn(IFNE, l0);

        mv.visitVarInsn(ALOAD, varNum);
        mv.visitLdcInsn(field.resolveAlias());
        mv.visitInsn(BuildUtils.zero(field.getTypeName()));
        if (BuildUtils.isPrimitive(field.getTypeName())) {
          TraitFactory.valueOf(mv, field.getTypeName());
        }
        mv.visitMethodInsn(
            INVOKEINTERFACE,
            Type.getInternalName(Map.class),
            "put",
            Type.getMethodDescriptor(
                Type.getType(Object.class),
                new Type[] {Type.getType(Object.class), Type.getType(Object.class)}));
        mv.visitInsn(POP);

        if (core.isFullTraiting()) {
          mv.visitVarInsn(ALOAD, 0);
          mv.visitFieldInsn(
              GETFIELD, internalWrapper, "object", Type.getDescriptor(core.getDefinedClass()));
          mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitableBean.class));
          mv.visitMethodInsn(
              INVOKEINTERFACE,
              Type.getInternalName(TraitableBean.class),
              "_getFieldTMS",
              Type.getMethodDescriptor(Type.getType(TraitFieldTMS.class), new Type[] {}));
          mv.visitVarInsn(ASTORE, 1);
          mv.visitVarInsn(ALOAD, 1);
          mv.visitLdcInsn(field.resolveAlias());
          mv.visitMethodInsn(
              INVOKEINTERFACE,
              Type.getInternalName(TraitFieldTMS.class),
              "isManagingField",
              Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {Type.getType(String.class)}));
          Label l1 = new Label();
          mv.visitJumpInsn(IFNE, l1);
          mv.visitVarInsn(ALOAD, 1);
          mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(core.getClassName())));
          mv.visitLdcInsn(field.resolveAlias());
          mv.visitMethodInsn(
              INVOKEINTERFACE,
              Type.getInternalName(TraitFieldTMS.class),
              "registerField",
              Type.getMethodDescriptor(
                  Type.VOID_TYPE,
                  new Type[] {Type.getType(Class.class), Type.getType(String.class)}));
          mv.visitLabel(l1);
        }

        mv.visitLabel(l0);
      }
    }
    return 0;
  }
  private void buildRemove(
      ClassWriter cw,
      String wrapperName,
      String coreName,
      ClassDefinition trait,
      ClassDefinition core,
      BitSet mask) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);

    MethodVisitor mv =
        cw.visitMethod(
            ACC_PUBLIC,
            "remove",
            "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class),
            null,
            null);
    mv.visitCode();

    for (FieldDefinition field : core.getFieldsDefinitions()) {
      invokeRemove(mv, wrapperName, core, field.getName(), field);
    }

    int j = 0;
    int stack = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
      boolean isSoftField = TraitRegistry.isSoftField(field, j++, mask);
      if (isSoftField) {
        mv.visitLdcInsn(field.getName());
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(
            INVOKEVIRTUAL,
            Type.getInternalName(String.class),
            "equals",
            "(" + Type.getDescriptor(Object.class) + ")Z");
        Label l2 = new Label();
        mv.visitJumpInsn(IFEQ, l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
        mv.visitLdcInsn(field.getName());
        mv.visitMethodInsn(
            INVOKEINTERFACE,
            Type.getInternalName(Map.class),
            "get",
            "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class));
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
        mv.visitLdcInsn(field.getName());
        mv.visitInsn(BuildUtils.zero(field.getTypeName()));
        if (BuildUtils.isPrimitive(field.getTypeName())) {
          TraitFactory.valueOf(mv, field.getTypeName());
        }
        mv.visitMethodInsn(
            INVOKEINTERFACE,
            Type.getInternalName(Map.class),
            "put",
            "("
                + Type.getDescriptor(Object.class)
                + Type.getDescriptor(Object.class)
                + ")"
                + Type.getDescriptor(Object.class));
        mv.visitInsn(POP);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l2);
      }
    }

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(
        INVOKEINTERFACE,
        Type.getInternalName(Map.class),
        "remove",
        "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class));
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitInsn(ARETURN);
    //        mv.visitMaxs( 4 + stack, 3 );
    mv.visitMaxs(0, 0);
    mv.visitEnd();
  }