Search in sources :

Example 31 with ClassWriter

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildInitSoftFields.

protected void buildInitSoftFields(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core, BitSet mask) {
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE, "initSoftFields", "()V", null, null);
    mv.visitCode();
    initSoftFields(mv, wrapperName, trait, core, mask);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 32 with ClassWriter

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildClearSoftFields.

protected void buildClearSoftFields(ClassWriter cw, String wrapperName, ClassDefinition trait, BitSet mask) {
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE, "clearSoftFields", "()V", null, null);
    mv.visitCode();
    int j = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
        boolean isSoftField = TraitRegistryImpl.isSoftField(field, j++, mask);
        if (isSoftField) {
            clearSoftField(mv, wrapperName, field);
        }
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 33 with ClassWriter

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildClass.

public byte[] buildClass(ClassDefinition core, ClassLoader classLoader) throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
    FieldVisitor fv;
    MethodVisitor mv;
    // get the method bitmask
    BitSet mask = traitRegistryImpl.getFieldMask(trait.getName(), core.getDefinedClass().getName());
    String name = TraitFactoryImpl.getPropertyWrapperName(trait, core);
    String internalWrapper = BuildUtils.getInternalType(name);
    String descrCore = Type.getDescriptor(core.getDefinedClass());
    ClassWriter cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_SUPER, internalWrapper, null, Type.getInternalName(TripleBasedStruct.class), new String[] { Type.getInternalName(Serializable.class) });
    cw.visitInnerClass(Type.getInternalName(Map.Entry.class), Type.getInternalName(Map.class), "Entry", ACC_PUBLIC + ACC_STATIC + ACC_ABSTRACT + ACC_INTERFACE);
    {
        fv = cw.visitField(0, "object", descrCore, null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + descrCore + Type.getDescriptor(TripleStore.class) + Type.getDescriptor(TripleFactory.class) + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "<init>", "()V", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(PUTFIELD, internalWrapper, "object", descrCore);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitFieldInsn(PUTFIELD, internalWrapper, "store", Type.getDescriptor(TripleStore.class));
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleBasedStruct.class), "setTripleFactory", "(" + Type.getDescriptor(TripleFactory.class) + ")V", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleStore.class), "getId", "()" + Type.getDescriptor(String.class), false);
        mv.visitFieldInsn(PUTFIELD, internalWrapper, "storeId", Type.getDescriptor(String.class));
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, internalWrapper, "initSoftFields", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    buildInitSoftFields(cw, internalWrapper, trait, core, mask);
    buildClearSoftFields(cw, internalWrapper, trait, mask);
    buildSize(cw, core);
    buildIsEmpty(cw, core);
    buildGet(cw, name, core);
    buildPut(cw, name, core);
    buildClear(cw, name, trait, core);
    buildRemove(cw, name, trait, core, mask);
    buildContainsKey(cw, core);
    buildContainsValue(cw, name, core);
    buildKeyset(cw, core);
    buildValues(cw, name, core);
    buildEntryset(cw, name, core);
    buildCommonMethods(cw, name);
    buildSpecificMethods(cw, name, core);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Serializable(java.io.Serializable) BitSet(java.util.BitSet) FieldVisitor(org.mvel2.asm.FieldVisitor) Map(java.util.Map) ClassGenerator.createClassWriter(org.drools.mvel.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 34 with ClassWriter

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildRemove.

protected void buildRemove(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core, BitSet mask) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "remove", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), null, null);
    mv.visitCode();
    int stack = 0;
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        stack = Math.max(stack, BuildUtils.sizeOf(field.getTypeName()));
        invokeRemove(mv, wrapperName, core, field.getName(), field);
    }
    int j = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
        boolean isSoftField = TraitRegistryImpl.isSoftField(field, j++, mask);
        if (isSoftField) {
            stack = Math.max(stack, BuildUtils.sizeOf(field.getTypeName()));
            mv.visitLdcInsn(field.getName());
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(String.class), "equals", "(" + Type.getDescriptor(Object.class) + ")Z", false);
            Label l2 = new Label();
            mv.visitJumpInsn(IFEQ, l2);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, internalWrapper, "store", Type.getDescriptor(TripleStore.class));
            mv.visitVarInsn(ALOAD, 0);
            mv.visitLdcInsn(field.getName());
            mv.visitMethodInsn(INVOKEVIRTUAL, internalWrapper, "propertyKey", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Triple.class), false);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleStore.class), "get", "(" + Type.getDescriptor(Triple.class) + ")" + Type.getDescriptor(Triple.class), false);
            mv.visitVarInsn(ASTORE, 2);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitLdcInsn(field.getName());
            mv.visitInsn(AsmUtil.zero(field.getTypeName()));
            if (BuildUtils.isPrimitive(field.getTypeName())) {
                TraitFactoryImpl.valueOf(mv, field.getTypeName());
            }
            mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "put", "(" + Type.getDescriptor(String.class) + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), false);
            mv.visitInsn(POP);
            mv.visitVarInsn(ALOAD, 2);
            mv.visitInsn(ARETURN);
            mv.visitLabel(l2);
        }
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "remove", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), false);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 35 with ClassWriter

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildClear.

protected void buildClear(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null);
    mv.visitCode();
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        if (field.isKey())
            continue;
        TraitFactoryImpl.invokeInjector(mv, wrapperName, core, field, true, 1);
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "clear", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, internalWrapper, "clearSoftFields", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) MethodVisitor(org.mvel2.asm.MethodVisitor)

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