Search in sources :

Example 46 with Label

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

the class AbstractProxyClassBuilderImpl method helpSet.

public void helpSet(ClassDefinition core, FieldDefinition field, MethodVisitor mv, String proxyName) {
    // The trait field update will be done by the core setter. However, types may mismatch here
    FieldDefinition hardField = core.getFieldByAlias(field.resolveAlias());
    boolean isHardField = field.getTypeName().equals(hardField.getTypeName());
    if (!field.getType().isPrimitive() && !isHardField) {
        boolean isCoreTrait = hardField.getType().getAnnotation(Trait.class) != null;
        boolean isTraitTrait = field.getType().getAnnotation(Trait.class) != null;
        Label l0 = new Label();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitJumpInsn(IFNULL, l0);
        if (isCoreTrait && !isTraitTrait) {
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitableBean.class));
            mv.visitLdcInsn(hardField.getTypeName());
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitableBean.class), "getTrait", Type.getMethodDescriptor(Type.getType(Thing.class), Type.getType(String.class)), true);
            mv.visitVarInsn(ASTORE, 1);
        } else if (!isCoreTrait && isTraitTrait) {
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitProxyImpl.class));
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitProxyImpl.class), "getObject", Type.getMethodDescriptor(Type.getType(TraitableBean.class)), false);
            mv.visitVarInsn(ASTORE, 1);
        } else if (isCoreTrait) {
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitProxyImpl.class));
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TraitProxyImpl.class), "getObject", Type.getMethodDescriptor(Type.getType(TraitableBean.class)), false);
            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitableBean.class));
            mv.visitLdcInsn(hardField.getTypeName());
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitableBean.class), "getTrait", Type.getMethodDescriptor(Type.getType(Thing.class), Type.getType(String.class)), true);
            mv.visitVarInsn(ASTORE, 1);
        } else {
            // handled by normal inheritance, exceptions should have been thrown
            if (!hardField.getType().isAssignableFrom(field.getType())) {
                mv.visitInsn(RETURN);
            }
        }
        Label l1 = new Label();
        mv.visitJumpInsn(GOTO, l1);
        mv.visitLabel(l0);
        mv.visitInsn(ACONST_NULL);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitLabel(l1);
    } else if (field.getType().isPrimitive()) {
        if (!hardField.getType().equals(field.getType())) {
            mv.visitInsn(RETURN);
        }
    }
    if (isHardField && CoreWrapper.class.isAssignableFrom(core.getDefinedClass())) {
        logicalSetter(mv, field, proxyName, core);
    }
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) FieldDefinition(org.drools.core.factmodel.FieldDefinition) Label(org.mvel2.asm.Label) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) Trait(org.drools.core.factmodel.traits.Trait)

Example 47 with Label

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

the class AbstractProxyClassBuilderImpl method buildLogicalGetter.

protected void buildLogicalGetter(ClassVisitor cw, FieldDefinition field, String proxy, ClassDefinition core) {
    String fieldName = field.getName();
    String fieldType = field.getTypeName();
    String getter = BuildUtils.getterName(fieldName, fieldType);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getter, "()" + BuildUtils.getTypeDescriptor(fieldType), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(proxy), "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)), true);
    mv.visitLdcInsn(field.resolveAlias());
    if (BuildUtils.isPrimitive(fieldType)) {
        // mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType( BuildUtils.box( fieldType ) ), "TYPE", Type.getDescriptor( Class.class ) );
        mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(field.getTypeName()))));
    } else {
        mv.visitLdcInsn(Type.getType(Type.getDescriptor(field.getType())));
    }
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitFieldTMS.class), "get", Type.getMethodDescriptor(Type.getType(Object.class), Type.getType(String.class), Type.getType(Class.class)), true);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    if (BuildUtils.isPrimitive(fieldType)) {
        Label l0 = new Label();
        mv.visitJumpInsn(IFNULL, l0);
        mv.visitVarInsn(ALOAD, 1);
        Label l1 = new Label();
        mv.visitJumpInsn(GOTO, l1);
        mv.visitLabel(l0);
        mv.visitInsn(AsmUtil.zero(fieldType));
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, BuildUtils.getInternalType(BuildUtils.box(fieldType)), "valueOf", Type.getMethodDescriptor(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(fieldType))), Type.getType(BuildUtils.getTypeDescriptor(fieldType))), false);
        mv.visitLabel(l1);
        mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(BuildUtils.box(fieldType)));
        mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(BuildUtils.box(fieldType)), BuildUtils.numericMorph(BuildUtils.box(fieldType)), Type.getMethodDescriptor(Type.getType(field.getType())), false);
        mv.visitInsn(AsmUtil.returnType(fieldType));
    } else {
        mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(fieldType));
        mv.visitInsn(ARETURN);
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS) Label(org.mvel2.asm.Label) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 48 with Label

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

the class ClassFieldAccessorFactory method build3ArgConstructor.

/**
 * Creates a constructor for the field extractor receiving
 * the index, field type and value type
 */
private static void build3ArgConstructor(final Class<?> superClazz, final String className, final ClassWriter cw) {
    MethodVisitor mv;
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(int.class), Type.getType(Class.class), Type.getType(ValueType.class)), null, null);
    mv.visitCode();
    final Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(superClazz), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(int.class), Type.getType(Class.class), Type.getType(ValueType.class)));
    final Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitInsn(Opcodes.RETURN);
    final Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0);
    mv.visitLocalVariable("index", Type.getDescriptor(int.class), null, l0, l2, 1);
    mv.visitLocalVariable("fieldType", Type.getDescriptor(Class.class), null, l0, l2, 2);
    mv.visitLocalVariable("valueType", Type.getDescriptor(ValueType.class), null, l0, l2, 3);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : ValueType(org.drools.core.base.ValueType) Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 49 with Label

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

the class ClassFieldAccessorFactory method buildGetMethod.

/**
 * Creates the proxy reader method for the given method
 */
protected static void buildGetMethod(final Class<?> originalClass, final String className, final Class<?> superClass, final Method getterMethod, final ClassWriter cw) {
    final Class<?> fieldType = getterMethod.getReturnType();
    Method overridingMethod;
    try {
        overridingMethod = superClass.getMethod(getOverridingGetMethodName(fieldType), ReteEvaluator.class, Object.class);
    } catch (final Exception e) {
        throw new RuntimeException("This is a bug. Please report back to JBoss Rules team.", e);
    }
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, overridingMethod.getName(), Type.getMethodDescriptor(overridingMethod), null, null);
    mv.visitCode();
    final Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(originalClass));
    if (originalClass.isInterface()) {
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(originalClass), getterMethod.getName(), Type.getMethodDescriptor(getterMethod), true);
    } else {
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(originalClass), getterMethod.getName(), Type.getMethodDescriptor(getterMethod), false);
    }
    mv.visitInsn(Type.getType(fieldType).getOpcode(Opcodes.IRETURN));
    final Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
    mv.visitLocalVariable("workingMemory", Type.getDescriptor(InternalWorkingMemory.class), null, l0, l1, 1);
    mv.visitLocalVariable("object", Type.getDescriptor(Object.class), null, l0, l1, 2);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Label(org.mvel2.asm.Label) Method(java.lang.reflect.Method) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 50 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)

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