Search in sources :

Example 61 with ClassWriter

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

the class DefaultBeanClassBuilder method buildClassHeader.

/**
 * Defines the class header for the given class definition
 */
protected ClassWriter buildClassHeader(ClassLoader classLoader, ClassDefinition classDef) {
    boolean reactive = classDef.isReactive();
    String[] original = classDef.getInterfaces();
    int interfacesNr = original.length + (reactive ? 2 : 1);
    String[] interfaces = new String[interfacesNr];
    for (int i = 0; i < original.length; i++) {
        interfaces[i] = BuildUtils.getInternalType(original[i]);
    }
    interfaces[original.length] = BuildUtils.getInternalType(GeneratedFact.class.getName());
    if (reactive) {
        interfaces[original.length + 1] = BuildUtils.getInternalType(ReactiveObject.class.getName());
    }
    int classModifiers = Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER;
    if (classDef.isAbstrakt()) {
        classModifiers += Opcodes.ACC_ABSTRACT;
    }
    ClassWriter cw = createClassWriter(classLoader, classModifiers, BuildUtils.getInternalType(classDef.getClassName()), null, BuildUtils.getInternalType(classDef.getSuperClass()), interfaces);
    buildClassAnnotations(classDef, cw);
    cw.visitSource(classDef.getClassName() + ".java", null);
    return cw;
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter)

Example 62 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 63 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);
    this.buildFields(cw, classDef);
    if (classDef.isTraitable()) {
        this.buildDynamicPropertyMap(cw, classDef);
        this.buildTraitMap(cw, classDef);
        this.buildFieldTMS(cw, classDef);
    }
    this.buildConstructors(cw, classDef);
    this.buildGettersAndSetters(cw, classDef);
    this.buildEqualityMethods(cw, classDef);
    this.buildToString(cw, classDef);
    if (classDef.isTraitable()) {
        // must guarantee serialization order when enhancing fields are present
        this.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.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter)

Example 64 with ClassWriter

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

the class DefaultBeanClassBuilder method buildDynamicPropertyMap.

/**
 * A traitable class is a special class with support for dynamic properties and types.
 *
 * This method builds the property map, containing the key/values pairs to implement
 * any property defined in a trait interface but not supported by the traited class
 * fields.
 *
 * @param cw
 * @param def
 */
protected void buildDynamicPropertyMap(ClassWriter cw, ClassDefinition def) {
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "_getDynamicProperties", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}), "()Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setDynamicProperties", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(Map.class) }), "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)V", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) FieldVisitor(org.mvel2.asm.FieldVisitor) Map(java.util.Map) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 65 with ClassWriter

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

the class ClassGenerator method generateBytecode.

public byte[] generateBytecode() {
    if (bytecode == null) {
        ClassWriter cw = createClassWriter(classLoader, access, getClassDescriptor(), signature, getSuperClassDescriptor(), toInteralNames(interfaces));
        for (int i = 0; i < classParts.size(); i++) {
            // don't use iterator to allow method visits to add more class fields and methods
            classParts.get(i).write(this, cw);
        }
        if (staticInitializer != null) {
            staticInitializer.write(this, cw);
        }
        cw.visitEnd();
        bytecode = cw.toByteArray();
        if (DUMP_GENERATED_CLASSES) {
            dumpGeneratedClass(bytecode);
        }
    }
    return bytecode;
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter)

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