Search in sources :

Example 1 with ClassFieldInspectorImpl

use of org.drools.mvel.asm.ClassFieldInspectorImpl 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 ClassFieldInspectorImpl

use of org.drools.mvel.asm.ClassFieldInspectorImpl in project drools by kiegroup.

the class PackageHeaderLoader method addFields.

private void addFields(Class clazz) throws IOException {
    String className = clazz.getName();
    ClassFieldInspectorImpl inspector = new ClassFieldInspectorImpl(clazz);
    Set<String> fieldNames = inspector.getFieldNames().keySet();
    Map<String, Class<?>> fieldTypes = inspector.getFieldTypes();
    addThisField(className);
    for (String field : fieldNames) {
        if (IGNORED_FIELDS.contains(field)) {
            continue;
        }
        fieldsByClassNames.put(className, field);
        fieldTypesByClassAndFieldNames.put(className + "." + field, fieldTypes.get(field).getName());
    }
}
Also used : ClassFieldInspectorImpl(org.drools.mvel.asm.ClassFieldInspectorImpl)

Example 3 with ClassFieldInspectorImpl

use of org.drools.mvel.asm.ClassFieldInspectorImpl in project drools by kiegroup.

the class TraitBuilderUtil method findMixinInfo.

static MixinInfo findMixinInfo(Class<?> traitClass) {
    if (traitClass == null) {
        return null;
    }
    Map<Class<?>, List<Method>> mixinMethodMap = findMixinMethodImpls(traitClass);
    if (mixinMethodMap.isEmpty()) {
        return null;
    }
    MixinInfo mixinInfo = new MixinInfo(traitClass);
    try {
        mixinInfo.mixinClasses = new ArrayList<>();
        mixinInfo.mixinClasses.addAll(mixinMethodMap.keySet());
        mixinInfo.mixinMethods = new HashMap<>();
        mixinInfo.mixinGetSet = new HashMap<>();
        for (Map.Entry<Class<?>, List<Method>> entry : mixinMethodMap.entrySet()) {
            Class<?> mixinClass = entry.getKey();
            ClassFieldInspectorImpl cfi = new ClassFieldInspectorImpl(mixinClass);
            for (Method m : entry.getValue()) {
                try {
                    traitClass.getMethod(m.getName(), m.getParameterTypes());
                    if (cfi.getGetterMethods().containsValue(m) || cfi.getSetterMethods().containsValue(m)) {
                        Map<String, Method> map = mixinInfo.mixinGetSet.computeIfAbsent(mixinClass, k -> new HashMap<>());
                        map.put(m.getName(), m);
                    } else {
                        Set<Method> set = mixinInfo.mixinMethods.computeIfAbsent(mixinClass, k -> new HashSet<>());
                        set.add(m);
                    }
                } catch (NoSuchMethodException ignored) {
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mixinInfo;
}
Also used : ClassFieldInspectorImpl(org.drools.mvel.asm.ClassFieldInspectorImpl) Method(java.lang.reflect.Method) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ClassFieldInspectorImpl (org.drools.mvel.asm.ClassFieldInspectorImpl)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 ClassDefinition (org.drools.core.factmodel.ClassDefinition)1 FieldDefinition (org.drools.core.factmodel.FieldDefinition)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 ClassFieldAccessor (org.drools.mvel.accessors.ClassFieldAccessor)1 ClassFieldAccessorStore (org.drools.mvel.accessors.ClassFieldAccessorStore)1