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