| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
package net.sf.mindoro.commons.util; |
| 8 |
|
|
| 9 |
|
import java.lang.reflect.InvocationTargetException; |
| 10 |
|
import java.lang.reflect.Method; |
| 11 |
|
import java.lang.reflect.Modifier; |
| 12 |
|
|
| 13 |
|
import org.apache.commons.logging.Log; |
| 14 |
|
import org.apache.commons.logging.LogFactory; |
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
public final class BeanUtils { |
| 23 |
|
|
| 24 |
6 |
private static final Log log = LogFactory.getLog(BeanUtils.class); |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
0 |
private BeanUtils() { |
| 30 |
|
|
| 31 |
0 |
} |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
public static String beanToString(final Object obj) { |
| 43 |
2 |
final StringBuffer sb = new StringBuffer(); |
| 44 |
|
|
| 45 |
2 |
sb.append('('); |
| 46 |
2 |
if (null == obj) { |
| 47 |
0 |
sb.append("null"); |
| 48 |
|
} else { |
| 49 |
2 |
final Class objClass = obj.getClass(); |
| 50 |
2 |
sb.append(objClass.getName()).append(' '); |
| 51 |
|
|
| 52 |
2 |
final Method[] methods = objClass.getDeclaredMethods(); |
| 53 |
26 |
for (int i = 0; i < methods.length; ++i) { |
| 54 |
24 |
final Method method = methods[i]; |
| 55 |
24 |
if (Modclass="keyword">ifier.isPublic(method.getModclass="keyword">ifiers()) && method.getName().startsWith("get") |
| 56 |
|
&& method.getParameterTypes().length == 0) { |
| 57 |
|
|
| 58 |
12 |
extractPropertyValue(sb, obj, method); |
| 59 |
|
} |
| 60 |
|
} |
| 61 |
|
} |
| 62 |
2 |
sb.append(')'); |
| 63 |
2 |
return sb.toString(); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
private static void extractPropertyValue(final StringBuffer sb, class="keyword">final Object obj, class="keyword">final Method method) { |
| 75 |
|
try { |
| 76 |
12 |
final Object value = method.invoke(obj, new Object[0]); |
| 77 |
12 |
sb.append('(').append(getFriendlyName(method.getName())).append(' '); |
| 78 |
|
|
| 79 |
12 |
if (null == value) { |
| 80 |
6 |
sb.append("null"); |
| 81 |
|
} else { |
| 82 |
6 |
final String valueToString = value.toString(); |
| 83 |
6 |
if (value.getClass() == java.lang.String.class) { |
| 84 |
6 |
sb.append('"').append(valueToString).append('"'); |
| 85 |
|
} else { |
| 86 |
0 |
sb.append(valueToString); |
| 87 |
|
} |
| 88 |
|
} |
| 89 |
12 |
sb.append(')'); |
| 90 |
0 |
} catch (final IllegalArgumentException e) { |
| 91 |
0 |
log.error("IllegalArgumentException caught invoking \"" + method.getName() + "\" on " |
| 92 |
|
+ obj.toString(), e); |
| 93 |
0 |
} catch (final IllegalAccessException e) { |
| 94 |
0 |
log.error("IllegalAccessException caught invoking \"" + method.getName() + "\" on " |
| 95 |
|
+ obj.toString(), e); |
| 96 |
0 |
} catch (final InvocationTargetException e) { |
| 97 |
0 |
log.error("InvocationTargetException caught invoking \"" + method.getName() + "\" on " |
| 98 |
|
+ obj.toString(), e); |
| 99 |
12 |
} |
| 100 |
12 |
} |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
private static String getFriendlyName(final String name) { |
| 108 |
|
final String result; |
| 109 |
|
|
| 110 |
12 |
if (name.startsWith("get")) { |
| 111 |
12 |
result = name.substring(3, 4).toLowerCase() + name.substring(4, name.length()); |
| 112 |
|
} else { |
| 113 |
0 |
result = name; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
12 |
return result; |
| 117 |
|
} |
| 118 |
|
} |