Search in sources :

Example 16 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition 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 17 with FieldDefinition

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildEntryset.

protected void buildEntryset(ClassVisitor cw, String wrapperName, ClassDefinition core) {
    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.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "entrySet", "()" + Type.getDescriptor(Set.class), false);
    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) MethodVisitor(org.mvel2.asm.MethodVisitor) HashSet(java.util.HashSet)

Example 18 with FieldDefinition

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildValues.

protected void buildValues(ClassVisitor cw, String wrapperName, ClassDefinition core) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "values", "()" + Type.getDescriptor(Collection.class), "()Ljava/util/Collection<Ljava/lang/Object;>;", null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, Type.getInternalName(ArrayList.class));
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(ArrayList.class), "<init>", "()V", false);
    mv.visitVarInsn(ASTORE, 1);
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        extractAndCollect(mv, wrapperName, field, core);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "values", "()" + Type.getDescriptor(Collection.class), false);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Collection.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 : FieldDefinition(org.drools.core.factmodel.FieldDefinition) ArrayList(java.util.ArrayList) Collection(java.util.Collection) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 19 with FieldDefinition

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildContainsKey.

protected void buildContainsKey(ClassWriter cw, ClassDefinition core) {
    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.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "containsKey", "(" + Type.getDescriptor(Object.class) + ")Z", false);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 20 with FieldDefinition

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

the class ClassDefinitionFactory method wireFields.

protected boolean wireFields(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, PackageRegistry pkgRegistry, List<TypeDefinition> unresolvedTypeDefinitions) {
    // Fields are SORTED in the process
    if (!typeDescr.getFields().isEmpty()) {
        if (unresolvedTypeDefinitions != null && !unresolvedTypeDefinitions.isEmpty()) {
            for (TypeFieldDescr fld : typeDescr.getFields().values()) {
                if (unresolvedTypeDefinitions != null) {
                    for (TypeDefinition typeDef : unresolvedTypeDefinitions) {
                        if (fld.getPattern().getObjectType().equals(typeDef.getTypeClassName())) {
                            return false;
                        }
                    }
                }
            }
        }
        List<FieldDefinition> fieldDefs = sortFields(typeDescr.getFields(), pkgRegistry.getTypeResolver(), kbuilder);
        int i = 0;
        for (FieldDefinition fieldDef : fieldDefs) {
            fieldDef.setIndex(i++);
            def.addField(fieldDef);
        }
    }
    return true;
}
Also used : TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) FieldDefinition(org.drools.core.factmodel.FieldDefinition)

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