Search in sources :

Example 41 with ClassWriter

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

the class ClassFieldAccessorFactory method dumpWriter.

private static byte[] dumpWriter(final Class<?> originalClass, final String className, final Method getterMethod, final Class<?> fieldType) throws Exception {
    final Class<?> superClass = getWriterSuperClassFor(fieldType);
    final ClassWriter cw = buildClassHeader(superClass, className);
    build3ArgConstructor(superClass, className, cw);
    buildSetMethod(originalClass, className, superClass, getterMethod, fieldType, cw);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.mvel.asm.ClassGenerator.createClassWriter)

Example 42 with ClassWriter

use of org.mvel2.asm.ClassWriter 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 43 with ClassWriter

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

the class DefaultBeanClassBuilder method buildFieldTMS.

/**
 * A traitable class is a special class with support for dynamic properties and types.
 *
 * A traitable class in logical mode provides additional control over the values
 * and type(s) of its fields.
 *
 * @param cw
 * @param def
 */
protected void buildFieldTMS(ClassWriter cw, ClassDefinition def) {
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class), null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "_getFieldTMS", Type.getMethodDescriptor(Type.getType(TraitFieldTMS.class), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setFieldTMS", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(TraitFieldTMS.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS) FieldVisitor(org.mvel2.asm.FieldVisitor) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 44 with ClassWriter

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

the class DefaultBeanClassBuilder method buildMetaData.

protected void buildMetaData(ClassWriter cw, ClassDefinition classDef) {
    Object serialVersionUID = classDef.getMetaData("serialVersionUID");
    if (serialVersionUID != null) {
        FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "serialVersionUID", BuildUtils.getTypeDescriptor("long"), null, Long.parseLong(serialVersionUID.toString()));
        fv.visitEnd();
    }
}
Also used : ReactiveObject(org.drools.core.phreak.ReactiveObject) FieldVisitor(org.mvel2.asm.FieldVisitor)

Example 45 with ClassWriter

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

the class DefaultBeanClassBuilder method buildClass.

/**
 * Dynamically builds, defines and loads a class based on the given class definition
 *
 * @param classDef the class definition object structure
 *
 * @return the Class instance for the given class definition
 *
 * @throws IOException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws NoSuchMethodException
 * @throws ClassNotFoundException
 * @throws IllegalArgumentException
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws InstantiationException
 */
public byte[] buildClass(ClassDefinition classDef, ClassLoader classLoader) throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
    ClassWriter cw = this.buildClassHeader(classLoader, classDef);
    buildMetaData(cw, classDef);
    buildFields(cw, classDef);
    if (classDef.isTraitable()) {
        buildDynamicPropertyMap(cw, classDef);
        buildTraitMap(cw, classDef);
        buildFieldTMS(cw, classDef);
    }
    buildConstructors(cw, classDef);
    buildGettersAndSetters(cw, classDef);
    buildEqualityMethods(cw, classDef);
    buildToString(cw, classDef);
    if (classDef.isTraitable()) {
        // must guarantee serialization order when enhancing fields are present
        buildSerializationMethods(cw, classDef);
    }
    if (classDef.isReactive()) {
        implementReactivity(cw, classDef);
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.mvel.asm.ClassGenerator.createClassWriter)

Aggregations

MethodVisitor (org.mvel2.asm.MethodVisitor)58 ClassWriter (org.mvel2.asm.ClassWriter)32 FieldDefinition (org.drools.core.factmodel.FieldDefinition)23 FieldVisitor (org.mvel2.asm.FieldVisitor)23 Map (java.util.Map)20 Label (org.mvel2.asm.Label)20 BitSet (java.util.BitSet)12 ClassGenerator.createClassWriter (org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter)12 ClassGenerator.createClassWriter (org.drools.mvel.asm.ClassGenerator.createClassWriter)12 Serializable (java.io.Serializable)8 Type (org.mvel2.asm.Type)8 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)7 ObjectInput (java.io.ObjectInput)6 ObjectOutput (java.io.ObjectOutput)6 Externalizable (java.io.Externalizable)4 Collection (java.util.Collection)4 Thing (org.drools.core.factmodel.traits.Thing)4 TraitFieldTMS (org.drools.core.factmodel.traits.TraitFieldTMS)3 ReactiveObject (org.drools.core.phreak.ReactiveObject)3