Search in sources :

Example 6 with FieldDefinition

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

the class TraitMapPropertyWrapperClassBuilderImpl method buildGet.

protected void buildGet(ClassVisitor cw, String wrapperName, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), null, null);
    mv.visitCode();
    if (core.getFieldsDefinitions().size() > 0) {
        for (FieldDefinition field : core.getFieldsDefinitions()) {
            invokeGet(mv, wrapperName, core, field.getName(), field);
        }
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", "(" + 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 7 with FieldDefinition

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

the class TraitMapPropertyWrapperClassBuilderImpl method buildClear.

protected void buildClear(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core, BitSet mask) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null);
    mv.visitCode();
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        if (field.isKey())
            continue;
        TraitFactory.invokeInjector(mv, wrapperName, core, field, true, 1);
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "clear", "()V", true);
    initSoftFields(mv, trait, core, internalWrapper, mask, 0);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 8 with FieldDefinition

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

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

the class TraitMapPropertyWrapperClassBuilderImpl method buildValues.

protected void buildValues(ClassVisitor cw, String wrapperName, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    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.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "values", "()" + Type.getDescriptor(Collection.class), true);
    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) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 10 with FieldDefinition

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

the class TraitMapProxyClassBuilderImpl method buildKeyedEquals.

protected void buildKeyedEquals(ClassVisitor cw, ClassDefinition classDef, String proxyType) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "equals", "(" + Type.getDescriptor(Object.class) + ")Z", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    Label l0 = new Label();
    mv.visitJumpInsn(IF_ACMPNE, l0);
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 1);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNULL, l1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(Object.class), "getClass", "()" + Type.getDescriptor(Class.class), false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(Object.class), "getClass", "()" + Type.getDescriptor(Class.class), false);
    Label l2 = new Label();
    mv.visitJumpInsn(IF_ACMPEQ, l2);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l2);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, proxyType);
    mv.visitVarInsn(ASTORE, 2);
    int x = 2;
    for (FieldDefinition field : classDef.getFieldsDefinitions()) {
        if (field.isKey()) {
            if (!BuildUtils.isPrimitive(field.getTypeName())) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                Label l11 = new Label();
                mv.visitJumpInsn(IFNULL, l11);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(field.getTypeName()), "equals", "(" + Type.getDescriptor(Object.class) + ")Z", false);
                Label l12 = new Label();
                mv.visitJumpInsn(IFNE, l12);
                Label l13 = new Label();
                mv.visitJumpInsn(GOTO, l13);
                mv.visitLabel(l11);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitJumpInsn(IFNULL, l12);
                mv.visitLabel(l13);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(l12);
            } else if ("double".equals(field.getTypeName())) {
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Double.class), "compare", "(DD)I", false);
                Label l5 = new Label();
                mv.visitJumpInsn(IFEQ, l5);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(l5);
                x = Math.max(x, 4);
            } else if ("float".equals(field.getTypeName())) {
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Float.class), "compare", "(FF)I", false);
                Label l6 = new Label();
                mv.visitJumpInsn(IFEQ, l6);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(l6);
            } else if ("long".equals(field.getTypeName())) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitInsn(LCMP);
                Label l8 = new Label();
                mv.visitJumpInsn(IFEQ, l8);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(l8);
                x = Math.max(x, 4);
            } else {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                Label l4 = new Label();
                mv.visitJumpInsn(IF_ICMPEQ, l4);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(l4);
            }
        }
    }
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    // mv.visitMaxs( x, 3 );
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

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