Search in sources :

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

Example 47 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.mvel.asm.ClassGenerator.createClassWriter)

Example 48 with ClassWriter

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

the class DefaultEnumClassBuilder 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 java.io.IOException
 * @throws ClassNotFoundException
 * @throws IllegalArgumentException
 * @throws SecurityException
 */
public byte[] buildClass(ClassDefinition classDef, ClassLoader classLoader) throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException {
    if (!(classDef instanceof EnumClassDefinition)) {
        throw new RuntimeException("FATAL : Trying to create an enum out of a bean class definition  " + classDef);
    }
    EnumClassDefinition edef = (EnumClassDefinition) classDef;
    ClassWriter cw = this.buildClassHeader(classLoader, edef);
    this.buildLiterals(cw, edef);
    this.buildFields(cw, edef);
    this.buildConstructors(cw, edef);
    this.buildGettersAndSetters(cw, edef);
    this.buildEqualityMethods(cw, edef);
    this.buildToString(cw, edef);
    cw.visitEnd();
    byte[] serializedClass = cw.toByteArray();
    return serializedClass;
}
Also used : EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) ClassGenerator.createClassWriter(org.drools.mvel.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter)

Example 49 with ClassWriter

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

the class DefaultEnumClassBuilder method buildFields.

protected void buildFields(ClassWriter cw, EnumClassDefinition classDef) {
    FieldVisitor fv;
    for (FieldDefinition fld : classDef.getFieldsDefinitions()) {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, fld.getName(), BuildUtils.getTypeDescriptor(fld.getTypeName()), null, null);
        fv.visitEnd();
    }
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) FieldVisitor(org.mvel2.asm.FieldVisitor)

Example 50 with ClassWriter

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

the class DefaultEnumClassBuilder method buildLiterals.

protected void buildLiterals(ClassWriter cw, EnumClassDefinition classDef) {
    FieldVisitor fv;
    for (EnumLiteralDefinition lit : classDef.getEnumLiterals()) {
        fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM, lit.getName(), BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC + ACC_SYNTHETIC, "$VALUES", "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null);
        fv.visitEnd();
    }
}
Also used : EnumLiteralDefinition(org.drools.core.factmodel.EnumLiteralDefinition) FieldVisitor(org.mvel2.asm.FieldVisitor)

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