Search in sources :

Example 1 with FieldDefinition

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

the class AbstractTraitFactory method invokeExtractor.

public static void invokeExtractor(MethodVisitor mv, String masterName, ClassDefinition trait, ClassDefinition core, FieldDefinition field) {
    FieldDefinition tgtField = core.getFieldByAlias(field.resolveAlias());
    String fieldType = tgtField.getTypeName();
    String returnType = BuildUtils.getTypeDescriptor(fieldType);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(masterName), "object", BuildUtils.getTypeDescriptor(core.getClassName()));
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(core.getDefinedClass()), tgtField.getReadMethod(), Type.getMethodDescriptor(Type.getType(returnType)), false);
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition)

Example 2 with FieldDefinition

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

the class AbstractTraitFactory method invokeInjector.

public static void invokeInjector(MethodVisitor mv, String masterName, ClassDefinition core, FieldDefinition field, boolean toNull, int pointer) {
    FieldDefinition tgtField = core.getFieldByAlias(field.resolveAlias());
    String fieldType = tgtField.getTypeName();
    String returnType = BuildUtils.getTypeDescriptor(fieldType);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(masterName), "object", BuildUtils.getTypeDescriptor(core.getName()));
    if (toNull) {
        mv.visitInsn(BuildUtils.zero(field.getTypeName()));
    } else {
        mv.visitVarInsn(BuildUtils.varType(fieldType), pointer);
    }
    if (!BuildUtils.isPrimitive(fieldType)) {
        mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(fieldType));
    }
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(core.getDefinedClass()), tgtField.getWriteMethod(), Type.getMethodDescriptor(Type.getType(void.class), Type.getType(returnType)), false);
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition)

Example 3 with FieldDefinition

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

the class TraitMapPropertyWrapperClassBuilderImpl method buildRemove.

protected void buildRemove(ClassWriter cw, String wrapperName, ClassDefinition trait, ClassDefinition core, BitSet mask) {
    String internalWrapper = BuildUtils.getInternalType(wrapperName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "remove", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), null, null);
    mv.visitCode();
    for (FieldDefinition field : core.getFieldsDefinitions()) {
        invokeRemove(mv, wrapperName, core, field.getName(), field);
    }
    int j = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
        boolean isSoftField = TraitRegistry.isSoftField(field, j++, mask);
        if (isSoftField) {
            mv.visitLdcInsn(field.getName());
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(String.class), "equals", "(" + Type.getDescriptor(Object.class) + ")Z", false);
            Label l2 = new Label();
            mv.visitJumpInsn(IFEQ, l2);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
            mv.visitLdcInsn(field.getName());
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
            mv.visitVarInsn(ASTORE, 2);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
            mv.visitLdcInsn(field.getName());
            mv.visitInsn(BuildUtils.zero(field.getTypeName()));
            if (BuildUtils.isPrimitive(field.getTypeName())) {
                TraitFactory.valueOf(mv, field.getTypeName());
            }
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "put", "(" + Type.getDescriptor(Object.class) + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
            mv.visitInsn(POP);
            mv.visitVarInsn(ALOAD, 2);
            mv.visitInsn(ARETURN);
            mv.visitLabel(l2);
        }
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "remove", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitInsn(ARETURN);
    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 4 with FieldDefinition

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

the class TraitMapPropertyWrapperClassBuilderImpl method buildContainsKey.

protected void buildContainsKey(ClassWriter cw, String name, ClassDefinition core) {
    String internalWrapper = BuildUtils.getInternalType(name);
    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.visitFieldInsn(GETFIELD, internalWrapper, "map", Type.getDescriptor(Map.class));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsKey", "(" + Type.getDescriptor(Object.class) + ")Z", true);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Map(java.util.Map) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 5 with FieldDefinition

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

the class TraitMapPropertyWrapperClassBuilderImpl method initSoftFields.

protected void initSoftFields(MethodVisitor mv, ClassDefinition trait, ClassDefinition core, String internalWrapper, BitSet mask, int varNum) {
    int j = 0;
    for (FieldDefinition field : trait.getFieldsDefinitions()) {
        boolean isSoftField = TraitRegistry.isSoftField(field, j++, mask);
        if (isSoftField) {
            mv.visitVarInsn(ALOAD, varNum);
            mv.visitLdcInsn(field.resolveAlias());
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsKey", Type.getMethodDescriptor(Type.getType(boolean.class), Type.getType(Object.class)), true);
            Label l0 = new Label();
            mv.visitJumpInsn(IFNE, l0);
            mv.visitVarInsn(ALOAD, varNum);
            mv.visitLdcInsn(field.resolveAlias());
            mv.visitInsn(BuildUtils.zero(field.getTypeName()));
            if (BuildUtils.isPrimitive(field.getTypeName())) {
                TraitFactory.valueOf(mv, field.getTypeName());
            }
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "put", Type.getMethodDescriptor(Type.getType(Object.class), Type.getType(Object.class), Type.getType(Object.class)), true);
            mv.visitInsn(POP);
            if (core.isFullTraiting()) {
                registerLogicalField(mv, internalWrapper, field, core);
            }
            mv.visitLabel(l0);
        }
    }
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) Label(org.mvel2.asm.Label) Map(java.util.Map)

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