Search in sources :

Example 31 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class TraitMapPropertyWrapperClassBuilderImpl method buildEntryset.

protected void buildEntryset(ClassVisitor cw, String wrapperName, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "entrySet", "()" + Type.getDescriptor(Set.class), "()Ljava/util/Set<Ljava/util/Map$Entry<Ljava/lang/String;Ljava/lang/Object;>;>;", null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, Type.getInternalName(HashSet.class));
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(HashSet.class), "<init>", "()V", false);
    mv.visitVarInsn(ASTORE, 1);
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        buildEntry(mv, field, wrapperName, core);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "entrySet", "()" + Type.getDescriptor(Set.class), true);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Set.class), "addAll", "(" + Type.getDescriptor(Collection.class) + ")Z", true);
    mv.visitInsn(POP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) FieldDefinition(org.drools.core.factmodel.FieldDefinition) Collection(java.util.Collection) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor) HashSet(java.util.HashSet)

Example 32 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class TraitMapPropertyWrapperClassBuilderImpl method buildKeyset.

protected void buildKeyset(ClassVisitor cw, String wrapperName, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "keySet", "()" + Type.getDescriptor(Set.class), "()Ljava/util/Set<Ljava/lang/String;>;", null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, Type.getInternalName(HashSet.class));
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(HashSet.class), "<init>", "()V", false);
    mv.visitVarInsn(ASTORE, 1);
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        mv.visitVarInsn(ALOAD, 1);
        mv.visitLdcInsn(field.getName());
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Set.class), "add", "(" + Type.getDescriptor(Object.class) + ")Z", true);
        mv.visitInsn(POP);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "keySet", "()" + Type.getDescriptor(Set.class), true);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Set.class), "addAll", "(" + Type.getDescriptor(Collection.class) + ")Z", true);
    mv.visitInsn(POP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) FieldDefinition(org.drools.core.factmodel.FieldDefinition) Collection(java.util.Collection) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor) HashSet(java.util.HashSet)

Example 33 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class TraitMapPropertyWrapperClassBuilderImpl method buildPut.

protected void buildPut(ClassVisitor cw, String wrapperName, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "put", "(" + Type.getDescriptor(String.class) + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), null, null);
    mv.visitCode();
    if (core.getFieldsDefinitions().size() > 0) {
        for (FieldDefinition field : core.getFieldsDefinitions()) {
            invokePut(mv, wrapperName, core, field.getName(), field);
        }
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "put", "(" + Type.getDescriptor(Object.class) + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 34 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class TraitMapProxyClassBuilderImpl method buildFields.

protected void buildFields(ClassDefinition core, BitSet mask, String masterName, MixinInfo mixinInfo, ClassWriter cw) {
    int j = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
        boolean hardField = !TraitRegistry.isSoftField(field, j++, mask);
        if (core.isFullTraiting()) {
            buildLogicalGetter(cw, field, masterName, core);
            if (hardField) {
                buildHardSetter(cw, field, masterName, trait, core);
            } else {
                buildSoftSetter(cw, field, masterName, core);
            }
        } else {
            if (!hardField) {
                if (mixinInfo == null || !mixinInfo.isMixinGetter(field)) {
                    buildSoftGetter(cw, field, masterName);
                    buildSoftSetter(cw, field, masterName, core);
                }
            } else {
                buildHardGetter(cw, field, masterName, trait, core);
                buildHardSetter(cw, field, masterName, trait, core);
            }
        }
    }
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition)

Example 35 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class TraitRegistry method bind.

private BitSet bind(String trait, String traitable) throws UnsupportedOperationException {
    ClassDefinition traitDef = getTrait(trait);
    if (traitDef == null) {
        throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a trait ");
    }
    ClassDefinition traitableDef = getTraitable(traitable);
    if (traitableDef == null) {
        throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a traitable ");
    }
    int j = 0;
    BitSet bitmask = new BitSet(traitDef.getFields().size());
    for (FactField field : traitDef.getFields()) {
        String alias = ((FieldDefinition) field).resolveAlias();
        FieldDefinition concreteField = traitableDef.getFieldByAlias(alias);
        if (concreteField != null) {
            if (!traitableDef.isFullTraiting() && !concreteField.getType().isAssignableFrom(field.getType())) {
                throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " :" + " trait field " + field.getName() + ":" + ((FieldDefinition) field).getTypeName() + " is incompatible with" + " concrete hard field " + concreteField.getName() + ":" + concreteField.getTypeName() + ". Consider enabling logical traiting" + " mode using @Traitable( logical = true )");
            }
            bitmask.set(j);
        }
        j++;
    }
    return bitmask;
}
Also used : FactField(org.kie.api.definition.type.FactField) FieldDefinition(org.drools.core.factmodel.FieldDefinition) BitSet(java.util.BitSet) ClassDefinition(org.drools.core.factmodel.ClassDefinition)

Aggregations

FieldDefinition (org.drools.core.factmodel.FieldDefinition)44 MethodVisitor (org.mvel2.asm.MethodVisitor)23 Map (java.util.Map)11 Label (org.mvel2.asm.Label)10 BitSet (java.util.BitSet)6 Collection (java.util.Collection)6 ClassDefinition (org.drools.core.factmodel.ClassDefinition)6 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)4 Field (java.lang.reflect.Field)3 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 FactField (org.kie.api.definition.type.FactField)3 Position (org.kie.api.definition.type.Position)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)2 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)2