Search in sources :

Example 11 with ClassWriter

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

the class TraitMapPropertyWrapperClassBuilderImpl method buildContainsValue.

protected void buildContainsValue(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", null, null);
    mv.visitCode();
    // null check
    mv.visitVarInsn(ALOAD, 1);
    Label l99 = new Label();
    mv.visitJumpInsn(IFNONNULL, l99);
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        if (!BuildUtils.isPrimitive(field.getTypeName())) {
            extractAndTestNotNull(mv, wrapperName, core, field);
        }
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", true);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l99);
    // non-null values check
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        mv.visitVarInsn(ALOAD, 1);
        TraitFactory.invokeExtractor(mv, wrapperName, trait, core, field);
        if (BuildUtils.isPrimitive(field.getTypeName())) {
            TraitFactory.valueOf(mv, field.getTypeName());
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(Object.class), "equals", "(" + Type.getDescriptor(Object.class) + ")Z", false);
        Label l0 = new Label();
        mv.visitJumpInsn(IFEQ, l0);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitLabel(l0);
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", true);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Label(org.mvel2.asm.Label) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 12 with ClassWriter

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

the class TraitTripleProxyClassBuilderImpl 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 = traitRegistry.getFieldMask(getTrait().getName(), core.getDefinedClass().getName());
    String name = TraitFactory.getPropertyWrapperName(getTrait(), core);
    String masterName = TraitFactory.getProxyName(getTrait(), core);
    Class<?> traitClass = getTrait().getDefinedClass();
    String internalWrapper = BuildUtils.getInternalType(name);
    String internalProxy = BuildUtils.getInternalType(masterName);
    String internalCore = Type.getInternalName(core.getDefinedClass());
    String descrCore = Type.getDescriptor(core.getDefinedClass());
    String internalTrait = Type.getInternalName(getTrait().getDefinedClass());
    MixinInfo mixinInfo = findMixinInfo(traitClass);
    ClassWriter cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_SUPER, internalProxy, null, Type.getInternalName(proxyBaseClass), new String[] { internalTrait, Type.getInternalName(Externalizable.class) });
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, TraitType.traitNameField, Type.getDescriptor(String.class), null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PUBLIC, "object", descrCore, null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "store", Type.getDescriptor(TripleStore.class), null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "storeId", Type.getDescriptor(String.class), null, null);
        fv.visitEnd();
    }
    if (mixinInfo != null) {
        for (Class<?> mixinClass : mixinInfo.mixinClasses) {
            {
                fv = cw.visitField(ACC_PRIVATE, getMixinName(mixinClass), Type.getDescriptor(mixinClass), null, null);
                fv.visitEnd();
            }
        }
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + descrCore + Type.getDescriptor(TripleStore.class) + Type.getDescriptor(TripleFactory.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(boolean.class) + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleStore.class), "getId", "()" + Type.getDescriptor(String.class), false);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "storeId", Type.getDescriptor(String.class));
        buildConstructorCore(mv, internalProxy, internalWrapper, internalCore, descrCore, mixinInfo);
        initFields(mv, internalProxy);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "setObject", "(" + Type.getDescriptor(Object.class) + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, internalCore);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "object", descrCore);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "writeExternal", "(" + Type.getDescriptor(ObjectOutput.class) + ")V", null, new String[] { Type.getInternalName(IOException.class) });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "writeExternal", "(" + Type.getDescriptor(ObjectOutput.class) + ")V", false);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalProxy, "getObject", "()" + Type.getDescriptor(TraitableBean.class), false);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, internalProxy, "storeId", Type.getDescriptor(String.class));
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, internalProxy, "store", Type.getDescriptor(TripleStore.class));
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
        mv.visitInsn(RETURN);
        // mv.visitMaxs( 2, 2 );
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "readExternal", "(" + Type.getDescriptor(ObjectInput.class) + ")V", null, new String[] { Type.getInternalName(IOException.class), Type.getInternalName(ClassNotFoundException.class) });
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "readExternal", "(" + Type.getDescriptor(ObjectInput.class) + ")V", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), "readObject", "()" + Type.getDescriptor(Object.class), true);
        mv.visitTypeInsn(CHECKCAST, internalCore);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "object", descrCore);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), "readObject", "()" + Type.getDescriptor(Object.class), true);
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(String.class));
        mv.visitFieldInsn(PUTFIELD, internalProxy, "storeId", Type.getDescriptor(String.class));
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), "readObject", "()" + Type.getDescriptor(Object.class), true);
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TripleStore.class));
        mv.visitFieldInsn(PUTFIELD, internalProxy, "store", Type.getDescriptor(TripleStore.class));
        mv.visitInsn(RETURN);
        // mv.visitMaxs( 3, 2 );
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    helpBuildClass(core, cw, internalProxy, descrCore, mask);
    buildProxyAccessors(mask, cw, masterName, core, mixinInfo);
    boolean hasKeys = false;
    for (FactField ff : getTrait().getFields()) {
        if (ff.isKey()) {
            hasKeys = true;
            break;
        }
    }
    if (!hasKeys) {
        buildEqualityMethods(cw, masterName, core.getClassName());
    } else {
        buildKeyedEqualityMethods(cw, getTrait(), masterName);
    }
    buildMixinMethods(masterName, mixinInfo, cw);
    buildCommonMethods(cw, masterName);
    buildExtendedMethods(cw, getTrait(), core);
    buildShadowMethods(cw, trait, core);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : FactField(org.kie.api.definition.type.FactField) ObjectOutput(java.io.ObjectOutput) BitSet(java.util.BitSet) IOException(java.io.IOException) FieldVisitor(org.mvel2.asm.FieldVisitor) ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter) MethodVisitor(org.mvel2.asm.MethodVisitor) MixinInfo(org.drools.core.factmodel.traits.TraitBuilderUtil.MixinInfo) TraitBuilderUtil.findMixinInfo(org.drools.core.factmodel.traits.TraitBuilderUtil.findMixinInfo) TripleStore(org.drools.core.util.TripleStore) Externalizable(java.io.Externalizable) ObjectInput(java.io.ObjectInput)

Example 13 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 = TraitRegistry.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 14 with ClassWriter

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildContainsValue.

protected void buildContainsValue(ClassWriter cw, String wrapperName, ClassDefinition core) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", null, null);
    mv.visitCode();
    boolean hasNillable = false;
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        if (!BuildUtils.isPrimitive(field.getTypeName())) {
            hasNillable = true;
        }
    }
    Label l99 = null;
    if (hasNillable) {
        mv.visitVarInsn(ALOAD, 1);
        l99 = new Label();
        mv.visitJumpInsn(IFNONNULL, l99);
    }
    core.getFieldsDefinitions().size();
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        if (!BuildUtils.isPrimitive(field.getTypeName())) {
            extractAndTestNotNull(mv, wrapperName, core, field);
        }
    }
    if (hasNillable) {
        mv.visitLabel(l99);
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "containsValue", "(" + Type.getDescriptor(Object.class) + ")Z", false);
    mv.visitInsn(IRETURN);
    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 15 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 = traitRegistry.getFieldMask(trait.getName(), core.getDefinedClass().getName());
    String name = TraitFactory.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) TripleFactory(org.drools.core.util.TripleFactory) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter) 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