01: package org.conform.format;
02:
03: import java.lang.reflect.Method;
04:
05: public class CallGetterFormat extends AbstractFormat {
06: private String[] getterNames;
07:
08: public CallGetterFormat(String... getterNames) {
09: this .getterNames = getterNames;
10: }
11:
12: public String format(Object value) {
13: return value != null ? callGetters(value) : "";
14: }
15:
16: private String callGetters(Object object) {
17: try {
18: for (int i = 0; i < getterNames.length; i++) {
19: String getterName = getterNames[i];
20: Method getter = object.getClass().getMethod(getterName);
21: Object value = getter.invoke(object);
22: if (value != null)
23: return value.toString();
24: }
25: return null;
26: } catch (Exception e) {
27: return null;
28: }
29: }
30: }
|