Search in sources :

Example 11 with Label

use of org.mvel2.asm.Label in project drools by kiegroup.

the class TraitTriplePropertyWrapperClassBuilderImpl method buildContainsValue.

protected void buildContainsValue(ClassWriter cw, String wrapperName, ClassDefinition core) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", null, null);
    mv.visitCode();
    boolean hasNillable = false;
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        if (!BuildUtils.isPrimitive(field.getTypeName())) {
            hasNillable = true;
        }
    }
    Label l99 = null;
    if (hasNillable) {
        mv.visitVarInsn(ALOAD, 1);
        l99 = new Label();
        mv.visitJumpInsn(IFNONNULL, l99);
    }
    core.getFieldsDefinitions().size();
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        if (!BuildUtils.isPrimitive(field.getTypeName())) {
            extractAndTestNotNull(mv, wrapperName, core, field);
        }
    }
    if (hasNillable) {
        mv.visitLabel(l99);
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", false);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 12 with Label

use of org.mvel2.asm.Label in project drools by kiegroup.

the class DefaultBeanClassBuilder method implementReactivity.

private void implementReactivity(ClassWriter cw, ClassDefinition classDef) {
    final String LEFT_TUPLES_FIELD_NAME = "_lts";
    final String TYPE_NAME = BuildUtils.getInternalType(classDef.getClassName());
    FieldVisitor fv;
    /*
            private Collection<Tuple> _lts;
         */
    {
        fv = cw.visitField(ACC_PRIVATE, LEFT_TUPLES_FIELD_NAME, "Ljava/util/Collection;", "Ljava/util/Collection<Lorg/drools/core/spi/Tuple;>;", null);
        fv.visitEnd();
    }
    MethodVisitor mv;
    /*
            public void addLeftTuple(Tuple leftTuple) {
                if (_lts == null) {
                    _lts = new HashSet<Tuple>();
                }
                _lts.add(leftTuple);
            }
         */
    {
        mv = cw.visitMethod(ACC_PUBLIC, "addLeftTuple", "(Lorg/drools/core/spi/Tuple;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(30, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, TYPE_NAME, LEFT_TUPLES_FIELD_NAME, "Ljava/util/Collection;");
        Label l1 = new Label();
        mv.visitJumpInsn(IFNONNULL, l1);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLineNumber(31, l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/HashSet");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V", false);
        mv.visitFieldInsn(PUTFIELD, TYPE_NAME, LEFT_TUPLES_FIELD_NAME, "Ljava/util/Collection;");
        mv.visitLabel(l1);
        mv.visitLineNumber(33, l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, TYPE_NAME, LEFT_TUPLES_FIELD_NAME, "Ljava/util/Collection;");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Collection", "add", "(Ljava/lang/Object;)Z", true);
        mv.visitInsn(POP);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(34, l3);
        mv.visitInsn(RETURN);
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + TYPE_NAME + ";", null, l0, l4, 0);
        mv.visitLocalVariable("leftTuple", "Lorg/drools/core/spi/Tuple;", null, l0, l4, 1);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    /*
            public Collection<Tuple> getLeftTuples() {             
                return _lts != null ? _lts : Collections.emptyList();
            }                                                      
         */
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getLeftTuples", "()Ljava/util/Collection;", "()Ljava/util/Collection<Lorg/drools/core/spi/Tuple;>;", null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(37, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, TYPE_NAME, LEFT_TUPLES_FIELD_NAME, "Ljava/util/Collection;");
        Label l1 = new Label();
        mv.visitJumpInsn(IFNULL, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, TYPE_NAME, LEFT_TUPLES_FIELD_NAME, "Ljava/util/Collection;");
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitMethodInsn(INVOKESTATIC, "java/util/Collections", "emptyList", "()Ljava/util/List;", false);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/util/Collection" });
        mv.visitInsn(ARETURN);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLocalVariable("this", "L" + TYPE_NAME + ";", null, l0, l3, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    /*
            protected void notifyModification() {           
                ReactiveObjectUtil.notifyModification(this);
            }                                               
         */
    {
        mv = cw.visitMethod(ACC_PROTECTED, "notifyModification", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(41, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, "org/drools/core/phreak/ReactiveObjectUtil", "notifyModification", "(Lorg/drools/core/phreak/ReactiveObject;)V", false);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(42, l1);
        mv.visitInsn(RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + TYPE_NAME + ";", null, l0, l2, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    /*
            public void removeLeftTuple(Tuple leftTuple) {
                _lts.remove(leftTuple);
            }
         */
    {
        mv = cw.visitMethod(ACC_PUBLIC, "removeLeftTuple", "(Lorg/drools/core/spi/Tuple;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(46, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, TYPE_NAME, LEFT_TUPLES_FIELD_NAME, "Ljava/util/Collection;");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Collection", "remove", "(Ljava/lang/Object;)Z", true);
        mv.visitInsn(POP);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(47, l1);
        mv.visitInsn(RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "Lorg/drools/core/phreak/AbstractReactiveObject;", null, l0, l2, 0);
        mv.visitLocalVariable("leftTuple", "Lorg/drools/core/spi/Tuple;", null, l0, l2, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
}
Also used : Label(org.mvel2.asm.Label) ReactiveObject(org.drools.core.phreak.ReactiveObject) FieldVisitor(org.mvel2.asm.FieldVisitor) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 13 with Label

use of org.mvel2.asm.Label in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildHashCode.

protected void buildHashCode(ClassVisitor cw, ClassDefinition classDef) {
    MethodVisitor mv;
    // Building hashCode() method
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "hashCode", "()I", null, null);
        mv.visitCode();
        Label l0 = null;
        if (this.debug) {
            l0 = new Label();
            mv.visitLabel(l0);
        }
        // int result = 1;
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitVarInsn(Opcodes.ISTORE, 1);
        // for each key field
        for (FieldDefinition field : classDef.getFieldsDefinitions()) {
            if (field.isKey()) {
                // result = result * 31 + <attr_hash>
                mv.visitVarInsn(Opcodes.ILOAD, 1);
                mv.visitIntInsn(Opcodes.BIPUSH, 31);
                mv.visitVarInsn(Opcodes.ILOAD, 1);
                mv.visitInsn(Opcodes.IMUL);
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                visitFieldOrGetter(mv, classDef, field);
                if ("boolean".equals(field.getTypeName())) {
                    // attr_hash ::== <boolean_attr> ? 1231 : 1237;
                    Label blabel1 = new Label();
                    mv.visitJumpInsn(Opcodes.IFEQ, blabel1);
                    mv.visitIntInsn(Opcodes.SIPUSH, 1231);
                    Label blabel2 = new Label();
                    mv.visitJumpInsn(Opcodes.GOTO, blabel2);
                    mv.visitLabel(blabel1);
                    mv.visitIntInsn(Opcodes.SIPUSH, 1237);
                    mv.visitLabel(blabel2);
                } else if ("long".equals(field.getTypeName())) {
                    // attr_hash ::== (int) (longAttr ^ (longAttr >>> 32))
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    visitFieldOrGetter(mv, classDef, field);
                    mv.visitIntInsn(Opcodes.BIPUSH, 32);
                    mv.visitInsn(Opcodes.LUSHR);
                    mv.visitInsn(Opcodes.LXOR);
                    mv.visitInsn(Opcodes.L2I);
                } else if ("float".equals(field.getTypeName())) {
                    // attr_hash ::== Float.floatToIntBits( floatAttr );
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Float.class), "floatToIntBits", "(F)I");
                } else if ("double".equals(field.getTypeName())) {
                    // attr_hash ::== (int) (Double.doubleToLongBits( doubleAttr ) ^ (Double.doubleToLongBits( doubleAttr ) >>> 32));
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Double.class), "doubleToLongBits", "(D)J");
                    mv.visitInsn(Opcodes.DUP2);
                    mv.visitIntInsn(Opcodes.BIPUSH, 32);
                    mv.visitInsn(Opcodes.LUSHR);
                    mv.visitInsn(Opcodes.LXOR);
                    mv.visitInsn(Opcodes.L2I);
                } else if (!BuildUtils.isPrimitive(field.getTypeName())) {
                    // attr_hash ::== ((objAttr == null) ? 0 : objAttr.hashCode());
                    Label olabel1 = new Label();
                    mv.visitJumpInsn(Opcodes.IFNONNULL, olabel1);
                    mv.visitInsn(Opcodes.ICONST_0);
                    Label olabel2 = new Label();
                    mv.visitJumpInsn(Opcodes.GOTO, olabel2);
                    mv.visitLabel(olabel1);
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    visitFieldOrGetter(mv, classDef, field);
                    if (!BuildUtils.isArray(field.getTypeName())) {
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "hashCode", "()I");
                    } else {
                        mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "hashCode", "(" + BuildUtils.arrayType(field.getTypeName()) + ")I");
                    }
                    mv.visitLabel(olabel2);
                }
                mv.visitInsn(Opcodes.IADD);
                mv.visitVarInsn(Opcodes.ISTORE, 1);
            }
        }
        mv.visitVarInsn(Opcodes.ILOAD, 1);
        mv.visitInsn(Opcodes.IRETURN);
        Label lastLabel = null;
        if (this.debug) {
            lastLabel = new Label();
            mv.visitLabel(lastLabel);
            mv.visitLocalVariable("this", BuildUtils.getTypeDescriptor(classDef.getClassName()), null, l0, lastLabel, 0);
            mv.visitLocalVariable("hash", Type.getDescriptor(int.class), null, l0, lastLabel, 1);
        }
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}
Also used : Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 14 with Label

use of org.mvel2.asm.Label in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildTraitMap.

/**
 * A traitable class is a special class with support for dynamic properties and types.
 *
 * This method builds the trait map, containing the references to the proxies
 * for each trait carried by an object at a given time.
 *
 * @param cw
 * @param classDef
 */
protected void buildTraitMap(ClassWriter cw, ClassDefinition classDef) {
    FieldVisitor fv = cw.visitField(ACC_PRIVATE, TraitableBean.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), new Type[] {}), "()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()), TraitableBean.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), new Type[] { Type.getType(Map.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.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), new Type[] { 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), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    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);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getTrait", Type.getMethodDescriptor(Type.getType(Thing.class), new Type[] { Type.getType(String.class) }), Type.getMethodDescriptor(Type.getType(Thing.class), new Type[] { 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), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { 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), new Type[] { 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), new Type[] {}));
    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), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsKey", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] { 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), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.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()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "isEmpty", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new 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), new Type[] { Type.getType(String.class) }), Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { 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), new Type[] {}));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "removeCascade", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { 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), new Type[] { Type.getType(BitSet.class) }), Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { 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), new Type[] {}));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "removeCascade", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { 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), new Type[] {}), "()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), new Type[] {}));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "keySet", Type.getMethodDescriptor(Type.getType(Set.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setBottomTypeCode", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(BitSet.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "setBottomCode", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { 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), new Type[] {}), "()Ljava/util/Collection<Lorg/drools/core/factmodel/traits/Thing;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.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()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "getMostSpecificTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitLabel(l99);
    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Collections.class), "emptySet", Type.getMethodDescriptor(Type.getType(Set.class), new Type[] {}));
    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Collections.class), "unmodifiableCollection", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { 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), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.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()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitTypeMap.class));
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitTypeMap.class), "getCurrentTypeCode", Type.getMethodDescriptor(Type.getType(BitSet.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) Label(org.mvel2.asm.Label) BitSet(java.util.BitSet) Collection(java.util.Collection) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) Collections(java.util.Collections) FieldVisitor(org.mvel2.asm.FieldVisitor) Map(java.util.Map) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) Thing(org.drools.core.factmodel.traits.Thing) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 15 with Label

use of org.mvel2.asm.Label in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildEquals.

protected void buildEquals(ClassVisitor cw, ClassDefinition classDef) {
    MethodVisitor mv;
    // Building equals method
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null);
        mv.visitCode();
        Label l0 = null;
        if (this.debug) {
            l0 = new Label();
            mv.visitLabel(l0);
        }
        // if ( this == obj ) return true;
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        Label l1 = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, l1);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IRETURN);
        // if ( obj == null ) return false;
        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        Label l2 = new Label();
        mv.visitJumpInsn(Opcodes.IFNONNULL, l2);
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitInsn(Opcodes.IRETURN);
        // if ( getClass() != obj.getClass() ) return false;
        mv.visitLabel(l2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Object.class), "getClass", Type.getMethodDescriptor(Type.getType(Class.class), new Type[] {}));
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Object.class), "getClass", Type.getMethodDescriptor(Type.getType(Class.class), new Type[] {}));
        Label l3 = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPEQ, l3);
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitInsn(Opcodes.IRETURN);
        // final <classname> other = (<classname>) obj;
        mv.visitLabel(l3);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitTypeInsn(Opcodes.CHECKCAST, BuildUtils.getInternalType(classDef.getClassName()));
        mv.visitVarInsn(Opcodes.ASTORE, 2);
        // for each key field
        int count = 0;
        for (FieldDefinition field : classDef.getFieldsDefinitions()) {
            if (field.isKey()) {
                count++;
                Label goNext = new Label();
                if (BuildUtils.isPrimitive(field.getTypeName())) {
                    // if attr is primitive
                    // if ( this.<attr> != other.<booleanAttr> ) return false;
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    visitFieldOrGetter(mv, classDef, field);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    visitFieldOrGetter(mv, classDef, field);
                    if (field.getTypeName().equals("long")) {
                        mv.visitInsn(Opcodes.LCMP);
                        mv.visitJumpInsn(Opcodes.IFEQ, goNext);
                    } else if (field.getTypeName().equals("double")) {
                        mv.visitInsn(Opcodes.DCMPL);
                        mv.visitJumpInsn(Opcodes.IFEQ, goNext);
                    } else if (field.getTypeName().equals("float")) {
                        mv.visitInsn(Opcodes.FCMPL);
                        mv.visitJumpInsn(Opcodes.IFEQ, goNext);
                    } else {
                        // boolean, byte, char, short, int
                        mv.visitJumpInsn(Opcodes.IF_ICMPEQ, goNext);
                    }
                    mv.visitInsn(Opcodes.ICONST_0);
                    mv.visitInsn(Opcodes.IRETURN);
                } else {
                    // if attr is not a primitive
                    // if ( this.<attr> == null && other.<attr> != null ||
                    // this.<attr> != null && ! this.<attr>.equals( other.<attr> ) ) return false;
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    visitFieldOrGetter(mv, classDef, field);
                    Label secondIfPart = new Label();
                    mv.visitJumpInsn(Opcodes.IFNONNULL, secondIfPart);
                    // if ( other.objAttr != null ) return false;
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    visitFieldOrGetter(mv, classDef, field);
                    Label returnFalse = new Label();
                    mv.visitJumpInsn(Opcodes.IFNONNULL, returnFalse);
                    mv.visitLabel(secondIfPart);
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    visitFieldOrGetter(mv, classDef, field);
                    mv.visitJumpInsn(Opcodes.IFNULL, goNext);
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    visitFieldOrGetter(mv, classDef, field);
                    mv.visitVarInsn(Opcodes.ALOAD, 2);
                    visitFieldOrGetter(mv, classDef, field);
                    if (!BuildUtils.isArray(field.getTypeName())) {
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z");
                    } else {
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/Arrays", "equals", "(" + BuildUtils.arrayType(field.getTypeName()) + BuildUtils.arrayType(field.getTypeName()) + ")Z");
                    }
                    mv.visitJumpInsn(Opcodes.IFNE, goNext);
                    mv.visitLabel(returnFalse);
                    mv.visitInsn(Opcodes.ICONST_0);
                    mv.visitInsn(Opcodes.IRETURN);
                }
                mv.visitLabel(goNext);
            }
        }
        if (count > 0) {
            mv.visitInsn(Opcodes.ICONST_1);
        } else {
            mv.visitInsn(Opcodes.ICONST_0);
        }
        mv.visitInsn(Opcodes.IRETURN);
        Label lastLabel = null;
        if (this.debug) {
            lastLabel = new Label();
            mv.visitLabel(lastLabel);
            mv.visitLocalVariable("this", BuildUtils.getTypeDescriptor(classDef.getClassName()), null, l0, lastLabel, 0);
            mv.visitLocalVariable("obj", Type.getDescriptor(Object.class), null, l0, lastLabel, 1);
            mv.visitLocalVariable("other", BuildUtils.getTypeDescriptor(classDef.getClassName()), null, l0, lastLabel, 2);
        }
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}
Also used : Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Aggregations

Label (org.mvel2.asm.Label)97 MethodVisitor (org.mvel2.asm.MethodVisitor)49 FieldDefinition (org.drools.core.factmodel.FieldDefinition)24 Map (java.util.Map)18 Type (org.mvel2.asm.Type)12 IOException (java.io.IOException)10 BitSet (java.util.BitSet)8 FieldVisitor (org.mvel2.asm.FieldVisitor)8 Method (java.lang.reflect.Method)7 CompiledInvoker (org.drools.core.spi.CompiledInvoker)7 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)5 ObjectInput (java.io.ObjectInput)4 ObjectOutput (java.io.ObjectOutput)4 Collection (java.util.Collection)4 Thing (org.drools.core.factmodel.traits.Thing)4 Declaration (org.drools.core.rule.Declaration)4 ClassGenerator (org.drools.core.rule.builder.dialect.asm.ClassGenerator)4 ClassWriter (org.mvel2.asm.ClassWriter)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3