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;
}
Aggregations