Search in sources :

Example 1 with AutoMetric

use of com.datatorrent.api.AutoMetric in project apex-core by apache.

the class AppDataPushAgent method extractFields.

private JSONObject extractFields(Object o) {
    List<Field> fields;
    Map<String, Method> methods;
    if (cacheFields.containsKey(o.getClass())) {
        fields = cacheFields.get(o.getClass());
    } else {
        fields = new ArrayList<>();
        for (Class<?> c = o.getClass(); c != Object.class; c = c.getSuperclass()) {
            Field[] declaredFields = c.getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                AutoMetric rfa = field.getAnnotation(AutoMetric.class);
                if (rfa != null) {
                    field.setAccessible(true);
                    try {
                        fields.add(field);
                    } catch (Exception ex) {
                        LOG.debug("Error extracting fields for app data: {}. Ignoring.", ex.getMessage());
                    }
                }
            }
        }
        cacheFields.put(o.getClass(), fields);
    }
    JSONObject result = new JSONObject();
    for (Field field : fields) {
        try {
            result.put(field.getName(), field.get(o));
        } catch (Exception ex) {
        // ignore
        }
    }
    if (cacheGetMethods.containsKey(o.getClass())) {
        methods = cacheGetMethods.get(o.getClass());
    } else {
        methods = new HashMap<>();
        try {
            BeanInfo info = Introspector.getBeanInfo(o.getClass());
            for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
                Method method = pd.getReadMethod();
                if (pd.getReadMethod() != null) {
                    AutoMetric rfa = method.getAnnotation(AutoMetric.class);
                    if (rfa != null) {
                        methods.put(pd.getName(), method);
                    }
                }
            }
        } catch (IntrospectionException ex) {
        // ignore
        }
        cacheGetMethods.put(o.getClass(), methods);
    }
    for (Map.Entry<String, Method> entry : methods.entrySet()) {
        try {
            result.put(entry.getKey(), entry.getValue().invoke(o));
        } catch (Exception ex) {
        // ignore
        }
    }
    return result;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) AutoMetric(com.datatorrent.api.AutoMetric) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) JSONException(org.codehaus.jettison.json.JSONException) Field(java.lang.reflect.Field) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AutoMetric (com.datatorrent.api.AutoMetric)1 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JSONException (org.codehaus.jettison.json.JSONException)1 JSONObject (org.codehaus.jettison.json.JSONObject)1