protected void buildTraitMap()

in drools-mvel/src/main/java/org/drools/mvel/asm/DefaultBeanClassBuilder.java [496:764]


    protected void buildTraitMap(ClassWriter cw, ClassDefinition classDef) {

        FieldVisitor fv = cw.visitField( ACC_PRIVATE,
                                         TraitConstants.TRAITSET_FIELD_NAME,
                                         Type.getDescriptor( Map.class ),
                                         "Ljava/util/Map<Ljava/lang/String;Lorg/drools/core/factmodel/traits/Thing;>;",
                                         null );
        fv.visitEnd();

        MethodVisitor mv;

        mv = cw.visitMethod( ACC_PUBLIC,
                             "_getTraitMap",
                             Type.getMethodDescriptor( Type.getType( Map.class )),
                             "()Ljava/util/Map<Ljava/lang/String;Lorg/drools/factmodel/traits/Thing;>;",
                             null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitFieldInsn( GETFIELD,
                           BuildUtils.getInternalType( classDef.getName() ),
                           TraitConstants.TRAITSET_FIELD_NAME,
                           Type.getDescriptor( Map.class ) );
        mv.visitInsn(ARETURN);
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC, "_setTraitMap", Type.getMethodDescriptor(Type.getType( void.class ), Type.getType(Map.class)), null, null);
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitFieldInsn( PUTFIELD, BuildUtils.getInternalType( classDef.getName() ), TraitConstants.TRAITSET_FIELD_NAME, Type.getDescriptor( Map.class ));
        mv.visitInsn( RETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();


        mv = cw.visitMethod( ACC_PUBLIC, "addTrait",
                             Type.getMethodDescriptor(Type.getType( void.class ), Type.getType(String.class), Type.getType(Thing.class)),
                             "(Ljava/lang/String;Lorg/drools/core/factmodel/traits/Thing;)V", null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKEVIRTUAL,
                            BuildUtils.getInternalType( classDef.getName() ),
                            "_getTraitMap",
                            Type.getMethodDescriptor( Type.getType( Map.class )));
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitVarInsn( ALOAD, 2 );
        mv.visitMethodInsn( INVOKEINTERFACE,
                            Type.getInternalName( Map.class ),
                            "put",
                            Type.getMethodDescriptor(Type.getType( Object.class ), Type.getType(Object.class), Type.getType(Object.class)));
        mv.visitInsn( POP );
        mv.visitInsn( RETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();


        mv = cw.visitMethod( ACC_PUBLIC,
                             "getTrait",
                             Type.getMethodDescriptor(Type.getType( Thing.class ), Type.getType(String.class)),
                             Type.getMethodDescriptor(Type.getType( Thing.class ), Type.getType(String.class)),
                             null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKEVIRTUAL,
                            BuildUtils.getInternalType( classDef.getName() ),
                            "_getTraitMap",
                            Type.getMethodDescriptor( Type.getType( Map.class )));
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitMethodInsn( INVOKEINTERFACE,
                            Type.getInternalName( Map.class ),
                            "get",
                            Type.getMethodDescriptor(Type.getType( Object.class ), Type.getType(Object.class)));
        mv.visitTypeInsn( CHECKCAST, Type.getInternalName( Thing.class ) );
        mv.visitInsn( ARETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();


        mv = cw.visitMethod( ACC_PUBLIC,
                             "hasTrait",
                             Type.getMethodDescriptor(Type.getType( boolean.class ), Type.getType(String.class)),
                             null,
                             null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKEVIRTUAL,
                            BuildUtils.getInternalType( classDef.getName() ),
                            "_getTraitMap",
                            Type.getMethodDescriptor( Type.getType( Map.class )));
        Label l0 = new Label();
        mv.visitJumpInsn( IFNONNULL, l0 );
        mv.visitInsn( ICONST_0 );
        mv.visitInsn( IRETURN );
        mv.visitLabel( l0 );
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKEVIRTUAL,
                            BuildUtils.getInternalType( classDef.getName() ),
                            "_getTraitMap",
                            Type.getMethodDescriptor( Type.getType( Map.class )));
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitMethodInsn( INVOKEINTERFACE,
                            Type.getInternalName( Map.class ),
                            "containsKey",
                            Type.getMethodDescriptor(Type.getType( boolean.class ), Type.getType(Object.class)));
        mv.visitInsn( IRETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();


        mv = cw.visitMethod( ACC_PUBLIC,
                             "hasTraits",
                             Type.getMethodDescriptor( Type.getType( boolean.class )),
                             null,
                             null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitFieldInsn( GETFIELD, BuildUtils.getInternalType( classDef.getName() ),TraitConstants.TRAITSET_FIELD_NAME, Type.getDescriptor( Map.class ) );
        Label l5 = new Label();
        mv.visitJumpInsn( IFNULL, l5 );
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitFieldInsn( GETFIELD, BuildUtils.getInternalType( classDef.getName() ), TraitConstants.TRAITSET_FIELD_NAME, Type.getDescriptor( Map.class ) );
        mv.visitMethodInsn( INVOKEINTERFACE, Type.getInternalName( Map.class ), "isEmpty", Type.getMethodDescriptor( Type.BOOLEAN_TYPE));
        mv.visitJumpInsn( IFNE, l5 );
        mv.visitInsn( ICONST_1 );
        Label l4 = new Label();
        mv.visitJumpInsn( GOTO, l4 );
        mv.visitLabel( l5 );
        mv.visitInsn( ICONST_0 );
        mv.visitLabel( l4 );
        mv.visitInsn( IRETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();


        mv = cw.visitMethod( ACC_PUBLIC, "removeTrait",
                             Type.getMethodDescriptor(Type.getType( Collection.class ), Type.getType(String.class)),
                             Type.getMethodDescriptor(Type.getType( Collection.class ), Type.getType(String.class)),
                             null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKEVIRTUAL, BuildUtils.getInternalType( classDef.getName() ), "_getTraitMap", Type.getMethodDescriptor( Type.getType( Map.class )));
        mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME );
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn( INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "removeCascade",
                            Type.getMethodDescriptor(Type.getType( Collection.class ), Type.getType(String.class)));
        mv.visitInsn( ARETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();

        mv = cw.visitMethod( ACC_PUBLIC, "removeTrait",
                             Type.getMethodDescriptor(Type.getType( Collection.class ), Type.getType(BitSet.class)),
                             Type.getMethodDescriptor(Type.getType( Collection.class ), Type.getType(BitSet.class)),
                             null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKEVIRTUAL, BuildUtils.getInternalType( classDef.getName() ), "_getTraitMap", Type.getMethodDescriptor( Type.getType( Map.class )));
        mv.visitTypeInsn( CHECKCAST, TraitTypeMapConstants.TYPE_NAME );
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitMethodInsn( INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "removeCascade",
                            Type.getMethodDescriptor(Type.getType( Collection.class ), Type.getType(BitSet.class)));
        mv.visitInsn( ARETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();


        mv = cw.visitMethod( ACC_PUBLIC,
                             "getTraits",
                             Type.getMethodDescriptor( Type.getType( Collection.class )),
                             "()Ljava/util/Collection<Ljava/lang/String;>;",
                             null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKEVIRTUAL,
                            BuildUtils.getInternalType( classDef.getName() ),
                            "_getTraitMap",
                            Type.getMethodDescriptor( Type.getType( Map.class )));
        mv.visitMethodInsn( INVOKEINTERFACE,
                            Type.getInternalName( Map.class ),
                            "keySet",
                            Type.getMethodDescriptor( Type.getType( Set.class )));
        mv.visitInsn( ARETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();


        mv = cw.visitMethod( ACC_PUBLIC,
                             "_setBottomTypeCode",
                             Type.getMethodDescriptor(Type.getType( void.class ), Type.getType(BitSet.class)),
                             null, null );
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitFieldInsn( GETFIELD, BuildUtils.getInternalType( classDef.getName() ), TraitConstants.TRAITSET_FIELD_NAME , Type.getDescriptor( Map.class ) );
        mv.visitTypeInsn( CHECKCAST, TraitTypeMapConstants.TYPE_NAME );
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitMethodInsn( INVOKEVIRTUAL,
                            TraitTypeMapConstants.TYPE_NAME,
                            "setBottomCode",
                            Type.getMethodDescriptor(Type.getType( void.class ), Type.getType(BitSet.class)));
        mv.visitInsn( RETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();

        mv = cw.visitMethod( ACC_PUBLIC,
                             "getMostSpecificTraits",
                             Type.getMethodDescriptor( Type.getType( Collection.class )) ,
                             "()Ljava/util/Collection<Lorg/drools/core/factmodel/traits/Thing;>;",
                             null );
        mv.visitCode();

        mv.visitVarInsn( ALOAD, 0 );
        mv.visitFieldInsn( GETFIELD, BuildUtils.getInternalType( classDef.getName() ),
                           TraitConstants.TRAITSET_FIELD_NAME ,
                           Type.getDescriptor( Map.class ) );


        Label l99 = new Label();
        mv.visitJumpInsn( IFNULL, l99 );
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitFieldInsn( GETFIELD, BuildUtils.getInternalType( classDef.getName() ),
                           TraitConstants.TRAITSET_FIELD_NAME ,
                           Type.getDescriptor( Map.class ) );
        mv.visitTypeInsn( CHECKCAST, TraitTypeMapConstants.TYPE_NAME );
        mv.visitMethodInsn( INVOKEVIRTUAL,
                            TraitTypeMapConstants.TYPE_NAME,
                            "getMostSpecificTraits",
                            Type.getMethodDescriptor( Type.getType( Collection.class )));
        mv.visitInsn( ARETURN );
        mv.visitLabel( l99 );
        mv.visitMethodInsn( INVOKESTATIC,
                            Type.getInternalName( Collections.class ),
                            "emptySet",
                            Type.getMethodDescriptor( Type.getType( Set.class )));
        mv.visitMethodInsn( INVOKESTATIC,
                            Type.getInternalName( Collections.class ),
                            "unmodifiableCollection",
                            Type.getMethodDescriptor(Type.getType( Collection.class ), Type.getType(Collection.class)));
        mv.visitInsn( ARETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC, "getCurrentTypeCode", Type.getMethodDescriptor( Type.getType( BitSet.class )) , null, null);
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitFieldInsn( GETFIELD,
                           BuildUtils.getInternalType( classDef.getName() ),
                           TraitConstants.TRAITSET_FIELD_NAME,
                           Type.getDescriptor( Map.class ) );
        Label l3 = new Label();
        mv.visitJumpInsn( IFNONNULL, l3 );
        mv.visitInsn( ACONST_NULL );
        mv.visitInsn( ARETURN );
        mv.visitLabel( l3 );
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitFieldInsn( GETFIELD,
                           BuildUtils.getInternalType( classDef.getName() ),
                           TraitConstants.TRAITSET_FIELD_NAME,
                           Type.getDescriptor( Map.class ) );
        mv.visitTypeInsn( CHECKCAST, TraitTypeMapConstants.TYPE_NAME );
        mv.visitMethodInsn( INVOKEVIRTUAL,
                            TraitTypeMapConstants.TYPE_NAME,
                            "getCurrentTypeCode",
                            Type.getMethodDescriptor( Type.getType( BitSet.class )));
        mv.visitInsn( ARETURN );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();


    }