Search in sources :

Example 1 with ClassFieldAccessor

use of org.drools.mvel.accessors.ClassFieldAccessor in project drools by kiegroup.

the class AbstractTraitFactory method buildClassDefinition.

public ClassDefinition buildClassDefinition(Class<?> klazz, Class<?> wrapperClass) throws IOException {
    ClassFieldInspectorImpl inspector = new ClassFieldInspectorImpl(klazz);
    ClassFieldAccessorStore store = getClassFieldAccessorStore();
    ClassDefinition def;
    if (!klazz.isInterface()) {
        String className = wrapperClass.getName();
        String superClass = wrapperClass != klazz ? klazz.getName() : klazz.getSuperclass().getName();
        String[] interfaces = new String[klazz.getInterfaces().length + 1];
        for (int j = 0; j < klazz.getInterfaces().length; j++) {
            interfaces[j] = klazz.getInterfaces()[j].getName();
        }
        interfaces[interfaces.length - 1] = CoreWrapper.class.getName();
        def = new ClassDefinition(className, superClass, interfaces);
        def.setDefinedClass(wrapperClass);
        Traitable tbl = wrapperClass.getAnnotation(Traitable.class);
        def.setTraitable(true, tbl != null && tbl.logical());
        Map<String, Field> fields = inspector.getFieldTypesField();
        for (Field f : fields.values()) {
            if (f != null) {
                FieldDefinition fld = new FieldDefinition();
                fld.setName(f.getName());
                fld.setTypeName(f.getType().getName());
                fld.setInherited(true);
                ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
                fld.setReadWriteAccessor(accessor);
                if (inspector.getGetterMethods().containsKey(f.getName())) {
                    fld.setGetterName(inspector.getGetterMethods().get(f.getName()).getName());
                }
                if (inspector.getSetterMethods().containsKey(f.getName())) {
                    fld.setSetterName(inspector.getSetterMethods().get(f.getName()).getName());
                }
                def.addField(fld);
            }
        }
    } else {
        String className = klazz.getName();
        String superClass = Object.class.getName();
        String[] interfaces = new String[klazz.getInterfaces().length];
        for (int j = 0; j < klazz.getInterfaces().length; j++) {
            interfaces[j] = klazz.getInterfaces()[j].getName();
        }
        def = new ClassDefinition(className, superClass, interfaces);
        def.setDefinedClass(klazz);
        Map<String, Method> properties = inspector.getGetterMethods();
        for (Map.Entry<String, Method> propEntry : properties.entrySet()) {
            Method m = propEntry.getValue();
            if (m != null && m.getDeclaringClass() != TraitType.class && m.getDeclaringClass() != Thing.class && inspector.getSetterMethods().containsKey(propEntry.getKey())) {
                FieldDefinition fld = new FieldDefinition();
                fld.setName(getterToFieldName(m.getName()));
                fld.setTypeName(m.getReturnType().getName());
                fld.setInherited(true);
                ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
                fld.setReadWriteAccessor(accessor);
                fld.setGetterName(m.getName());
                fld.setSetterName(inspector.getSetterMethods().get(propEntry.getKey()).getName());
                def.addField(fld);
            }
        }
    }
    return def;
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) TraitType(org.drools.core.factmodel.traits.TraitType) ClassFieldInspectorImpl(org.drools.mvel.asm.ClassFieldInspectorImpl) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) Method(java.lang.reflect.Method) ClassDefinition(org.drools.core.factmodel.ClassDefinition) Field(java.lang.reflect.Field) ClassFieldAccessor(org.drools.mvel.accessors.ClassFieldAccessor) Traitable(org.drools.core.factmodel.traits.Traitable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ClassFieldAccessor

use of org.drools.mvel.accessors.ClassFieldAccessor in project drools by kiegroup.

the class MVELKnowledgePackageImpl method buildFieldAccessors.

@Override
public void buildFieldAccessors(TypeDeclaration type) {
    ClassDefinition cd = type.getTypeClassDef();
    for (FieldDefinition attrDef : cd.getFieldsDefinitions()) {
        ClassFieldAccessor accessor = classFieldAccessorStore.getAccessor(cd.getDefinedClass().getName(), attrDef.getName());
        attrDef.setReadWriteAccessor(accessor);
    }
}
Also used : ClassFieldAccessor(org.drools.mvel.accessors.ClassFieldAccessor) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition)

Example 3 with ClassFieldAccessor

use of org.drools.mvel.accessors.ClassFieldAccessor in project drools by kiegroup.

the class ClassFieldAccessorTest method testBuildFieldAccessor.

@Test
public void testBuildFieldAccessor() {
    try {
        ClassFieldAccessor intAccessor = store.getAccessor(TestClass.class, "intAttr");
        ClassFieldAccessor strAccessor = store.getAccessor(TestClass.class, "strAttr");
        String testString1 = "TestAttr1";
        String testString2 = "TestAttr2";
        TestClass instance = new TestClass();
        instance.setIntAttr(10);
        instance.setStrAttr(testString1);
        assertEquals("Error reading int attr", 10, ((Integer) intAccessor.getValue(instance)).intValue());
        assertEquals("Error reading String attr", testString1, strAccessor.getValue(instance));
        intAccessor.setValue(instance, new Integer(50));
        strAccessor.setValue(instance, testString2);
        assertEquals("Error setting int attr", 50, instance.getIntAttr());
        assertEquals("Error setting String attr", testString2, instance.getStrAttr());
        assertEquals("Error reading String attr", testString2, strAccessor.getValue(instance));
    } catch (Exception e) {
        e.printStackTrace();
        fail("No exception is supposed to be generated when creating field accessor: " + e);
    }
}
Also used : ClassFieldAccessor(org.drools.mvel.accessors.ClassFieldAccessor) Test(org.junit.Test)

Example 4 with ClassFieldAccessor

use of org.drools.mvel.accessors.ClassFieldAccessor in project drools by kiegroup.

the class ClassFieldAccessorTest method testNullOnPrimitives.

@Test
public void testNullOnPrimitives() {
    try {
        ClassFieldAccessor intAccessor = store.getAccessor(TestClass.class, "intAttr");
        ClassFieldAccessor strAccessor = store.getAccessor(TestClass.class, "strAttr");
        ClassFieldAccessor byteAccessor = store.getAccessor(TestClass.class, "byteAttr");
        ClassFieldAccessor booleanAccessor = store.getAccessor(TestClass.class, "booleanAttr");
        ClassFieldAccessor charAccessor = store.getAccessor(TestClass.class, "charAttr");
        ClassFieldAccessor doubleAccessor = store.getAccessor(TestClass.class, "doubleAttr");
        ClassFieldAccessor floatAccessor = store.getAccessor(TestClass.class, "floatAttr");
        ClassFieldAccessor longAccessor = store.getAccessor(TestClass.class, "longAttr");
        ClassFieldAccessor shortAccessor = store.getAccessor(TestClass.class, "shortAttr");
        String testString1 = "TestAttr1";
        TestClass instance = new TestClass();
        instance.setIntAttr(25);
        instance.setStrAttr(testString1);
        instance.setByteAttr((byte) 10);
        instance.setBooleanAttr(true);
        instance.setCharAttr('x');
        instance.setDoubleAttr(10.5d);
        instance.setFloatAttr(40.3f);
        instance.setLongAttr(43l);
        instance.setShortAttr((short) 20);
        intAccessor.setValue(instance, null);
        strAccessor.setValue(instance, null);
        byteAccessor.setValue(instance, null);
        booleanAccessor.setValue(instance, null);
        charAccessor.setValue(instance, null);
        doubleAccessor.setValue(instance, null);
        floatAccessor.setValue(instance, null);
        longAccessor.setValue(instance, null);
        shortAccessor.setValue(instance, null);
        assertEquals("Error setting attr", 0, instance.getIntAttr());
        assertNull("Error setting attr", instance.getStrAttr());
        assertEquals("Error setting attr", 0, instance.getByteAttr());
        assertEquals("Error setting attr", false, instance.isBooleanAttr());
        assertEquals("Error setting attr", '\0', instance.getCharAttr());
        assertEquals("Error setting attr", 0.0d, instance.getDoubleAttr(), 0.1d);
        assertEquals("Error setting attr", 0.0f, instance.getFloatAttr(), 0.1f);
        assertEquals("Error setting attr", 0l, instance.getLongAttr());
        assertEquals("Error setting attr", (short) 0, instance.getShortAttr());
        assertEquals("Error reading int attr", 0, ((Integer) intAccessor.getValue(instance)).intValue());
        assertNull("Error reading String attr", strAccessor.getValue(instance));
        assertEquals("Error reading attr", 0, ((Byte) byteAccessor.getValue(instance)).byteValue());
        assertEquals("Error reading attr", false, ((Boolean) booleanAccessor.getValue(instance)).booleanValue());
        assertEquals("Error reading attr", '\0', ((Character) charAccessor.getValue(instance)).charValue());
        assertEquals("Error reading attr", 0.0d, ((Double) doubleAccessor.getValue(instance)).doubleValue(), 0.1d);
        assertEquals("Error reading attr", 0.0f, ((Float) floatAccessor.getValue(instance)).floatValue(), 0.1f);
        assertEquals("Error reading attr", 0l, ((Long) longAccessor.getValue(instance)).longValue());
        assertEquals("Error reading attr", (short) 0, ((Short) shortAccessor.getValue(instance)).shortValue());
    } catch (Exception e) {
        e.printStackTrace();
        fail("No exception is supposed to be generated when creating field accessor: " + e);
    }
}
Also used : ClassFieldAccessor(org.drools.mvel.accessors.ClassFieldAccessor) Test(org.junit.Test)

Aggregations

ClassFieldAccessor (org.drools.mvel.accessors.ClassFieldAccessor)4 ClassDefinition (org.drools.core.factmodel.ClassDefinition)2 FieldDefinition (org.drools.core.factmodel.FieldDefinition)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CoreWrapper (org.drools.core.factmodel.traits.CoreWrapper)1 TraitType (org.drools.core.factmodel.traits.TraitType)1 Traitable (org.drools.core.factmodel.traits.Traitable)1 ClassFieldAccessorStore (org.drools.mvel.accessors.ClassFieldAccessorStore)1 ClassFieldInspectorImpl (org.drools.mvel.asm.ClassFieldInspectorImpl)1