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