Search in sources :

Example 26 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);
        TraitFactoryImpl.invokeExtractor(mv, wrapperName, core, field);
        if (BuildUtils.isPrimitive(field.getTypeName())) {
            TraitFactoryImpl.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 27 with ClassWriter

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

the class TraitMapPropertyWrapperClassBuilderImpl method buildContainsKey.

protected void buildContainsKey(ClassWriter cw, String name, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(name);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "containsKey", "(" + Type.getDescriptor(Object.class) + ")Z", null, null);
    mv.visitCode();
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        invokeContainsKey(mv, field.getName());
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsKey", "(" + Type.getDescriptor(Object.class) + ")Z", true);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 28 with ClassWriter

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

the class TraitMapPropertyWrapperClassBuilderImpl 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();
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        invokeRemove(mv, wrapperName, core, field.getName(), field);
    }
    int j = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
        boolean isSoftField = TraitRegistryImpl.isSoftField(field, j++, mask);
        if (isSoftField) {
            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, "map", Type.getDescriptor(Map.class));
            mv.visitLdcInsn(field.getName());
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
            mv.visitVarInsn(ASTORE, 2);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
            mv.visitLdcInsn(field.getName());
            mv.visitInsn(AsmUtil.zero(field.getTypeName()));
            if (BuildUtils.isPrimitive(field.getTypeName())) {
                TraitFactoryImpl.valueOf(mv, field.getTypeName());
            }
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "put", "(" + Type.getDescriptor(Object.class) + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
            mv.visitInsn(POP);
            mv.visitVarInsn(ALOAD, 2);
            mv.visitInsn(ARETURN);
            mv.visitLabel(l2);
        }
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "remove", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
    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) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 29 with ClassWriter

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

the class TraitMapPropertyWrapperClassBuilderImpl 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());
    String internalCore = Type.getInternalName(core.getDefinedClass());
    ClassWriter cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_SUPER, internalWrapper, Type.getDescriptor(Object.class) + Type.getDescriptor(Map.class) + Type.getDescriptor(MapWrapper.class), // "Ljava/lang/Object;Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;Lorg/drools/factmodel/traits/MapWrapper;",
    Type.getInternalName(Object.class), new String[] { Type.getInternalName(Map.class), Type.getInternalName(MapWrapper.class), 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();
    }
    {
        fv = cw.visitField(0, "map", Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + descrCore + Type.getDescriptor(Map.class) + ")V", "(" + descrCore + "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)V", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.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, "map", Type.getDescriptor(Map.class));
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_setDynamicProperties", "(" + Type.getDescriptor(Map.class) + ")V", false);
        initSoftFields(mv, trait, core, internalWrapper, mask, 2);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    buildSize(cw, name, core);
    buildIsEmpty(cw, name, core);
    buildGet(cw, name, core);
    buildPut(cw, name, core);
    buildClear(cw, name, trait, core, mask);
    buildRemove(cw, name, trait, core, mask);
    buildContainsKey(cw, name, core);
    buildContainsValue(cw, name, trait, core);
    buildKeyset(cw, name, core);
    buildValues(cw, name, core);
    buildEntryset(cw, name, core);
    buildCommonMethods(cw, name);
    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 30 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 = traitRegistryImpl.getFieldMask(getTrait().getName(), core.getDefinedClass().getName());
    String name = TraitFactoryImpl.getPropertyWrapperName(getTrait(), core);
    String proxyName = TraitFactoryImpl.getProxyName(getTrait(), core);
    Class<?> traitClass = getTrait().getDefinedClass();
    String internalWrapper = BuildUtils.getInternalType(name);
    String internalProxy = BuildUtils.getInternalType(proxyName);
    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, proxyName, core, mixinInfo);
    boolean hasKeys = false;
    for (FactField ff : getTrait().getFields()) {
        if (ff.isKey()) {
            hasKeys = true;
            break;
        }
    }
    if (!hasKeys) {
        buildEqualityMethods(cw, proxyName, core.getClassName());
    } else {
        buildKeyedEqualityMethods(cw, getTrait(), proxyName);
    }
    buildMixinMethods(proxyName, mixinInfo, cw);
    buildCommonMethods(cw, proxyName);
    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) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) IOException(java.io.IOException) FieldVisitor(org.mvel2.asm.FieldVisitor) ClassWriter(org.mvel2.asm.ClassWriter) ClassGenerator.createClassWriter(org.drools.mvel.asm.ClassGenerator.createClassWriter) MethodVisitor(org.mvel2.asm.MethodVisitor) TraitBuilderUtil.findMixinInfo(org.drools.traits.core.factmodel.TraitBuilderUtil.findMixinInfo) MixinInfo(org.drools.traits.core.factmodel.TraitBuilderUtil.MixinInfo) Externalizable(java.io.Externalizable) ObjectInput(java.io.ObjectInput)

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