Search in sources :

Example 1 with MixinInfo

use of org.drools.core.factmodel.traits.TraitBuilderUtil.MixinInfo 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 2 with MixinInfo

use of org.drools.core.factmodel.traits.TraitBuilderUtil.MixinInfo in project drools by kiegroup.

the class TraitMapProxyClassBuilderImpl 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 descrCore = Type.getDescriptor(core.getDefinedClass());
    String internalCore = Type.getInternalName(core.getDefinedClass());
    String internalTrait = Type.getInternalName(traitClass);
    MixinInfo mixinInfo = findMixinInfo(traitClass);
    ClassWriter cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_SUPER, internalProxy, null, Type.getInternalName(proxyBaseClass), new String[] { internalTrait, Type.getInternalName(Serializable.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_PUBLIC, "map", Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
        fv.visitEnd();
    }
    if (mixinInfo != null) {
        for (Class<?> mixinClass : mixinInfo.mixinClasses) {
            {
                fv = cw.visitField(ACC_PRIVATE, getMixinName(mixinClass), BuildUtils.getTypeDescriptor(mixinClass.getName()), null, null);
                fv.visitEnd();
            }
        }
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + descrCore + Type.getDescriptor(Map.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(boolean.class) + ")V", "(" + descrCore + "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;" + Type.getDescriptor(BitSet.class) + Type.getDescriptor(BitSet.class) + Type.getDescriptor(boolean.class) + ")V", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "<init>", "()V", false);
        mv.visitVarInsn(ALOAD, 2);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNONNULL, l0);
        mv.visitTypeInsn(NEW, Type.getInternalName(ExternalizableLinkedHashMap.class));
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(ExternalizableLinkedHashMap.class), "<init>", "()V", false);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitLabel(l0);
        if (mixinInfo != null) {
            for (Class<?> mixinClass : mixinInfo.mixinClasses) {
                String mixin = getMixinName(mixinClass);
                try {
                    Class actualArg = getPossibleConstructor(mixinClass, trait.getDefinedClass());
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitTypeInsn(NEW, Type.getInternalName(mixinClass));
                    mv.visitInsn(DUP);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(mixinClass), "<init>", "(" + Type.getDescriptor(actualArg) + ")V", false);
                    mv.visitFieldInsn(PUTFIELD, internalProxy, mixin, Type.getDescriptor(mixinClass));
                } catch (NoSuchMethodException nsme) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitTypeInsn(NEW, Type.getInternalName(mixinClass));
                    mv.visitInsn(DUP);
                    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(mixinClass), "<init>", "()V", false);
                    mv.visitFieldInsn(PUTFIELD, internalProxy, mixin, Type.getDescriptor(mixinClass));
                }
            }
        }
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "object", descrCore);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "map", Type.getDescriptor(Map.class));
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalProxy, "setTypeCode", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(BitSet.class)), false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, internalWrapper);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, internalWrapper, "<init>", "(" + descrCore + Type.getDescriptor(Map.class) + ")V", false);
        mv.visitFieldInsn(PUTFIELD, internalProxy, "fields", Type.getDescriptor(Map.class));
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_getDynamicProperties", "()" + Type.getDescriptor(Map.class), false);
        Label l1 = new Label();
        mv.visitJumpInsn(IFNONNULL, l1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_setDynamicProperties", "(" + Type.getDescriptor(Map.class) + ")V", false);
        mv.visitLabel(l1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_getTraitMap", "()" + Type.getDescriptor(Map.class), false);
        Label l2 = new Label();
        mv.visitJumpInsn(IFNONNULL, l2);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(NEW, Type.getInternalName(TraitTypeMap.class));
        mv.visitInsn(DUP);
        mv.visitTypeInsn(NEW, Type.getInternalName(ExternalizableLinkedHashMap.class));
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(ExternalizableLinkedHashMap.class), "<init>", "()V", false);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TraitTypeMap.class), "<init>", "(" + Type.getDescriptor(Map.class) + ")V", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_setTraitMap", "(" + Type.getDescriptor(Map.class) + ")V", false);
        mv.visitLabel(l2);
        // core._setBottomTypeCode()
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 4);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "_setBottomTypeCode", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(BitSet.class)), false);
        // core.addTrait
        mv.visitVarInsn(ALOAD, 1);
        mv.visitLdcInsn(trait.getName().endsWith(TraitFactory.SUFFIX) ? trait.getName().replace(TraitFactory.SUFFIX, "") : trait.getName());
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalCore, "addTrait", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class), Type.getType(Thing.class)), false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ILOAD, 5);
        mv.visitMethodInsn(INVOKESPECIAL, internalProxy, "synchFields", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);
        mv.visitInsn(RETURN);
        // mv.visitMaxs( 5, 3 );
        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, 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, "map", Type.getDescriptor(Map.class));
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), "writeObject", "(" + Type.getDescriptor(Object.class) + ")V", true);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "writeExternal", "(" + Type.getDescriptor(ObjectOutput.class) + ")V", false);
        mv.visitInsn(RETURN);
        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(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(Map.class));
        mv.visitFieldInsn(PUTFIELD, internalProxy, "map", Type.getDescriptor(Map.class));
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(proxyBaseClass), "readExternal", "(" + Type.getDescriptor(ObjectInput.class) + ")V", false);
        mv.visitInsn(RETURN);
        // mv.visitMaxs( 3, 2 );
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    helpBuildClass(core, cw, internalProxy, descrCore, mask);
    buildFields(core, mask, masterName, mixinInfo, cw);
    buildKeys(core, masterName, cw);
    buildMixinMethods(masterName, mixinInfo, cw);
    buildCommonMethods(cw, masterName);
    buildExtendedMethods(cw, trait, core);
    buildShadowMethods(cw, trait, core);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Serializable(java.io.Serializable) ObjectOutput(java.io.ObjectOutput) BitSet(java.util.BitSet) Label(org.mvel2.asm.Label) 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) ObjectInput(java.io.ObjectInput) Map(java.util.Map) ExternalizableLinkedHashMap(org.drools.core.util.ExternalizableLinkedHashMap)

Aggregations

IOException (java.io.IOException)2 ObjectInput (java.io.ObjectInput)2 ObjectOutput (java.io.ObjectOutput)2 BitSet (java.util.BitSet)2 MixinInfo (org.drools.core.factmodel.traits.TraitBuilderUtil.MixinInfo)2 TraitBuilderUtil.findMixinInfo (org.drools.core.factmodel.traits.TraitBuilderUtil.findMixinInfo)2 ClassGenerator.createClassWriter (org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter)2 ClassWriter (org.mvel2.asm.ClassWriter)2 FieldVisitor (org.mvel2.asm.FieldVisitor)2 MethodVisitor (org.mvel2.asm.MethodVisitor)2 Externalizable (java.io.Externalizable)1 Serializable (java.io.Serializable)1 Map (java.util.Map)1 ExternalizableLinkedHashMap (org.drools.core.util.ExternalizableLinkedHashMap)1 TripleStore (org.drools.core.util.TripleStore)1 FactField (org.kie.api.definition.type.FactField)1 Label (org.mvel2.asm.Label)1