Search in sources :

Example 11 with FieldDefinition

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

the class TraitMapProxyClassBuilderImpl method buildKeyedHashCode.

protected void buildKeyedHashCode(ClassVisitor cw, ClassDefinition classDef, String proxyType) {
    MethodVisitor mv;
    {
        mv = cw.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
        mv.visitCode();
        mv.visitIntInsn(BIPUSH, 31);
        mv.visitVarInsn(ISTORE, 1);
        int x = 2;
        int y = 2;
        for (FieldDefinition field : classDef.getFieldsDefinitions()) {
            if (field.isKey()) {
                if (!BuildUtils.isPrimitive(field.getTypeName())) {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                    Label l8 = new Label();
                    mv.visitJumpInsn(IFNULL, l8);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(field.getTypeName()), "hashCode", "()I", false);
                    Label l9 = new Label();
                    mv.visitJumpInsn(GOTO, l9);
                    mv.visitLabel(l8);
                    mv.visitInsn(ICONST_0);
                    mv.visitLabel(l9);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                } else if ("double".equals(field.getTypeName())) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                    mv.visitInsn(DCONST_0);
                    mv.visitInsn(DCMPL);
                    Label l2 = new Label();
                    mv.visitJumpInsn(IFEQ, l2);
                    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), "doubleToLongBits", "(D)J", false);
                    Label l3 = new Label();
                    mv.visitJumpInsn(GOTO, l3);
                    mv.visitLabel(l2);
                    mv.visitInsn(LCONST_0);
                    mv.visitLabel(l3);
                    mv.visitVarInsn(LSTORE, 2);
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(LLOAD, 2);
                    mv.visitVarInsn(LLOAD, 2);
                    mv.visitIntInsn(BIPUSH, 32);
                    mv.visitInsn(LUSHR);
                    mv.visitInsn(LXOR);
                    mv.visitInsn(L2I);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                    x = Math.max(6, x);
                    y = Math.max(4, y);
                } else if ("boolean".equals(field.getTypeName())) {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                    Label l4 = new Label();
                    mv.visitJumpInsn(IFEQ, l4);
                    mv.visitInsn(ICONST_1);
                    Label l5 = new Label();
                    mv.visitJumpInsn(GOTO, l5);
                    mv.visitLabel(l4);
                    mv.visitInsn(ICONST_0);
                    mv.visitLabel(l5);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                } else if ("float".equals(field.getTypeName())) {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                    mv.visitInsn(FCONST_0);
                    mv.visitInsn(FCMPL);
                    Label l6 = new Label();
                    mv.visitJumpInsn(IFEQ, l6);
                    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), "floatToIntBits", "(F)I", false);
                    Label l7 = new Label();
                    mv.visitJumpInsn(GOTO, l7);
                    mv.visitLabel(l6);
                    mv.visitInsn(ICONST_0);
                    mv.visitLabel(l7);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                    x = Math.max(3, x);
                } else if ("long".equals(field.getTypeName())) {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    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.visitIntInsn(BIPUSH, 32);
                    mv.visitInsn(LUSHR);
                    mv.visitInsn(LXOR);
                    mv.visitInsn(L2I);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                    x = Math.max(6, x);
                } else {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + Type.getDescriptor(field.getType()), false);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                }
            }
        }
        mv.visitVarInsn(ILOAD, 1);
        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 12 with FieldDefinition

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

the class TraitTripleProxyClassBuilderImpl method buildKeyedHashCode.

protected void buildKeyedHashCode(ClassVisitor cw, ClassDefinition classDef, String proxyType) {
    MethodVisitor mv;
    {
        mv = cw.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
        mv.visitCode();
        mv.visitIntInsn(BIPUSH, 31);
        mv.visitVarInsn(ISTORE, 1);
        int x = 2;
        int y = 2;
        for (FieldDefinition field : classDef.getFieldsDefinitions()) {
            if (field.isKey()) {
                if (!BuildUtils.isPrimitive(field.getTypeName())) {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    Label l8 = new Label();
                    mv.visitJumpInsn(IFNULL, l8);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(field.getTypeName()), "hashCode", "()I", false);
                    Label l9 = new Label();
                    mv.visitJumpInsn(GOTO, l9);
                    mv.visitLabel(l8);
                    mv.visitInsn(ICONST_0);
                    mv.visitLabel(l9);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                } else if ("double".equals(field.getTypeName())) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    mv.visitInsn(DCONST_0);
                    mv.visitInsn(DCMPL);
                    Label l2 = new Label();
                    mv.visitJumpInsn(IFEQ, l2);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Double.class), "doubleToLongBits", "(D)J", false);
                    Label l3 = new Label();
                    mv.visitJumpInsn(GOTO, l3);
                    mv.visitLabel(l2);
                    mv.visitInsn(LCONST_0);
                    mv.visitLabel(l3);
                    mv.visitVarInsn(LSTORE, 2);
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(LLOAD, 2);
                    mv.visitVarInsn(LLOAD, 2);
                    mv.visitIntInsn(BIPUSH, 32);
                    mv.visitInsn(LUSHR);
                    mv.visitInsn(LXOR);
                    mv.visitInsn(L2I);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                    x = Math.max(6, x);
                    y = Math.max(4, y);
                } else if ("boolean".equals(field.getTypeName())) {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    Label l4 = new Label();
                    mv.visitJumpInsn(IFEQ, l4);
                    mv.visitInsn(ICONST_1);
                    Label l5 = new Label();
                    mv.visitJumpInsn(GOTO, l5);
                    mv.visitLabel(l4);
                    mv.visitInsn(ICONST_0);
                    mv.visitLabel(l5);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                } else if ("float".equals(field.getTypeName())) {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    mv.visitInsn(FCONST_0);
                    mv.visitInsn(FCMPL);
                    Label l6 = new Label();
                    mv.visitJumpInsn(IFEQ, l6);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Float.class), "floatToIntBits", "(F)I", false);
                    Label l7 = new Label();
                    mv.visitJumpInsn(GOTO, l7);
                    mv.visitLabel(l6);
                    mv.visitInsn(ICONST_0);
                    mv.visitLabel(l7);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                    x = Math.max(3, x);
                } else if ("long".equals(field.getTypeName())) {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    mv.visitIntInsn(BIPUSH, 32);
                    mv.visitInsn(LUSHR);
                    mv.visitInsn(LXOR);
                    mv.visitInsn(L2I);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                    x = Math.max(6, x);
                } else {
                    mv.visitIntInsn(BIPUSH, 31);
                    mv.visitVarInsn(ILOAD, 1);
                    mv.visitInsn(IMUL);
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, proxyType, BuildUtils.getterName(field.getName(), field.getTypeName()), "()" + BuildUtils.getTypeDescriptor(field.getTypeName()), false);
                    mv.visitInsn(IADD);
                    mv.visitVarInsn(ISTORE, 1);
                }
            }
        }
        mv.visitVarInsn(ILOAD, 1);
        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 13 with FieldDefinition

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildClearSoftFields.

protected void buildClearSoftFields(ClassWriter cw, String wrapperName, ClassDefinition trait, BitSet mask) {
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE, "clearSoftFields", "()V", null, null);
    mv.visitCode();
    int j = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
        boolean isSoftField = TraitRegistry.isSoftField(field, j++, mask);
        if (isSoftField) {
            clearSoftField(mv, wrapperName, field);
        }
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 14 with FieldDefinition

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildPut.

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

Example 15 with FieldDefinition

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

the class TraitTriplePropertyWrapperClassBuilderImpl method buildKeyset.

protected void buildKeyset(ClassVisitor cw, ClassDefinition core) {
    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()) {
        collectFieldName(mv, field);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TripleBasedStruct.class), "keySet", "()" + 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)

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