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;
  }
  public byte[] buildClass(ClassDefinition core, ClassLoader classLoader)
      throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException,
          NoSuchMethodException, IllegalAccessException, InvocationTargetException,
          InstantiationException, NoSuchFieldException {

    FieldVisitor fv;
    MethodVisitor mv;

    // get the method bitmask
    BitSet mask = traitRegistry.getFieldMask(trait.getName(), core.getDefinedClass().getName());

    String name = TraitFactory.getPropertyWrapperName(trait, core);

    String internalWrapper = BuildUtils.getInternalType(name);
    String descrCore = Type.getDescriptor(core.getDefinedClass());
    String internalCore = Type.getInternalName(core.getDefinedClass());

    ClassWriter cw =
        createClassWriter(
            classLoader,
            ACC_PUBLIC + ACC_SUPER,
            internalWrapper,
            Type.getDescriptor(Object.class)
                + Type.getDescriptor(Map.class)
                + Type.getDescriptor(MapWrapper.class),
            //
            // "Ljava/lang/Object;Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;Lorg/drools/factmodel/traits/MapWrapper;",
            Type.getInternalName(Object.class),
            new String[] {
              Type.getInternalName(Map.class),
              Type.getInternalName(MapWrapper.class),
              Type.getInternalName(Serializable.class)
            });

    cw.visitInnerClass(
        Type.getInternalName(Map.Entry.class),
        Type.getInternalName(Map.class),
        "Entry",
        ACC_PUBLIC + ACC_STATIC + ACC_ABSTRACT + ACC_INTERFACE);

    {
      fv = cw.visitField(0, "object", descrCore, null, null);
      fv.visitEnd();
    }

    {
      fv =
          cw.visitField(
              0,
              "map",
              Type.getDescriptor(Map.class),
              "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;",
              null);
      fv.visitEnd();
    }

    {
      mv =
          cw.visitMethod(
              ACC_PUBLIC,
              "<init>",
              "(" + descrCore + Type.getDescriptor(Map.class) + ")V",
              "(" + descrCore + "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)V",
              null);
      mv.visitCode();
      mv.visitVarInsn(ALOAD, 0);
      mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V");
      mv.visitVarInsn(ALOAD, 0);
      mv.visitVarInsn(ALOAD, 1);
      mv.visitFieldInsn(PUTFIELD, internalWrapper, "object", descrCore);
      mv.visitVarInsn(ALOAD, 0);
      mv.visitVarInsn(ALOAD, 2);
      mv.visitFieldInsn(PUTFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));

      mv.visitVarInsn(ALOAD, 1);
      mv.visitVarInsn(ALOAD, 2);
      mv.visitMethodInsn(
          INVOKEVIRTUAL,
          internalCore,
          "_setDynamicProperties",
          "(" + Type.getDescriptor(Map.class) + ")V");

      initSoftFields(mv, trait, core, internalWrapper, mask, 2);

      mv.visitInsn(RETURN);
      mv.visitMaxs(0, 0);
      mv.visitEnd();
    }

    buildSize(cw, name, core.getClassName(), trait, core, mask);

    buildIsEmpty(cw, name, core.getClassName(), trait, core, mask);

    buildGet(cw, name, core.getClassName(), trait, core, mask);

    buildPut(cw, name, core.getClassName(), trait, core, mask);

    buildClear(cw, name, core.getClassName(), trait, core, mask);

    buildRemove(cw, name, core.getClassName(), trait, core, mask);

    buildContainsKey(cw, name, core.getClassName(), trait, core, mask);

    buildContainsValue(cw, name, core.getClassName(), trait, core, mask);

    buildKeyset(cw, name, core.getClassName(), trait, core, mask);

    buildValues(cw, name, core.getClassName(), trait, core, mask);

    buildEntryset(cw, name, core.getClassName(), trait, core, mask);

    buildCommonMethods(cw, name);

    cw.visitEnd();

    return cw.toByteArray();
  }
  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();
  }