Search in sources :

Example 66 with ClassWriter

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();
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter)

Example 67 with ClassWriter

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

the class ClassFieldAccessorFactory method dumpReader.

private static byte[] dumpReader(final Class<?> originalClass, final String className, final Method getterMethod, final Class<?> fieldType) throws Exception {
    final Class<?> superClass = getReaderSuperClassFor(fieldType);
    final ClassWriter cw = buildClassHeader(superClass, className);
    build3ArgConstructor(superClass, className, cw);
    buildGetMethod(originalClass, className, superClass, getterMethod, cw);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter)

Example 68 with ClassWriter

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

the class ClassFieldAccessorFactory method buildGetMethod.

/**
 * Creates the proxy reader method for the given method
 */
protected static void buildGetMethod(final Class<?> originalClass, final String className, final Class<?> superClass, final Method getterMethod, final ClassWriter cw) {
    final Class<?> fieldType = getterMethod.getReturnType();
    Method overridingMethod;
    try {
        overridingMethod = superClass.getMethod(getOverridingGetMethodName(fieldType), InternalWorkingMemory.class, Object.class);
    } catch (final Exception e) {
        throw new RuntimeException("This is a bug. Please report back to JBoss Rules team.", e);
    }
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, overridingMethod.getName(), Type.getMethodDescriptor(overridingMethod), null, null);
    mv.visitCode();
    final Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(originalClass));
    if (originalClass.isInterface()) {
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(originalClass), getterMethod.getName(), Type.getMethodDescriptor(getterMethod));
    } else {
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(originalClass), getterMethod.getName(), Type.getMethodDescriptor(getterMethod));
    }
    mv.visitInsn(Type.getType(fieldType).getOpcode(Opcodes.IRETURN));
    final Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
    mv.visitLocalVariable("workingMemory", Type.getDescriptor(InternalWorkingMemory.class), null, l0, l1, 1);
    mv.visitLocalVariable("object", Type.getDescriptor(Object.class), null, l0, l1, 2);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Label(org.mvel2.asm.Label) Method(java.lang.reflect.Method) IOException(java.io.IOException) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 69 with ClassWriter

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

the class ClassFieldAccessorFactory method dumpReader.

private static byte[] dumpReader(final Class<?> originalClass, final String className, final Method getterMethod, final Class<?> fieldType) throws Exception {
    final Class<?> superClass = getReaderSuperClassFor(fieldType);
    final ClassWriter cw = buildClassHeader(superClass, className);
    build3ArgConstructor(superClass, className, cw);
    buildGetMethod(originalClass, className, superClass, getterMethod, cw);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.mvel.asm.ClassGenerator.createClassWriter)

Example 70 with ClassWriter

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

the class ClassFieldAccessorFactory method buildClassHeader.

/**
 * Builds the class header
 */
protected static ClassWriter buildClassHeader(Class<?> superClass, String className) {
    ClassWriter cw = createClassWriter(superClass.getClassLoader(), Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className, null, Type.getInternalName(superClass), null);
    cw.visitSource(null, null);
    return cw;
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.mvel.asm.ClassGenerator.createClassWriter)

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