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;
}
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();
}
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();
}
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();
}
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;
}
Aggregations