My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 187 attachment: NullableColumnFilter.patch (5.2 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
Index: src/org/jmesa/util/BeanUtils.java
===================================================================
--- src/org/jmesa/util/BeanUtils.java (revision 0)
+++ src/org/jmesa/util/BeanUtils.java (revision 0)
@@ -0,0 +1,124 @@
+package org.jmesa.util;
+
+import java.lang.reflect.Method;
+
+import org.apache.log4j.Logger;
+
+/**
+ * Utilities for java bean.
+ *
+ * @author wliu
+ */
+public class BeanUtils {
+ /**
+ * Logger instance.
+ */
+ private static final Logger logger =
+ Logger.getLogger(BeanUtils.class);
+
+ /**
+ * the possible prefixes for read method
+ */
+ private static final String[] getterPrefixes = { "get", "is"};
+
+ /**
+ * Gets the type of a property of an object.
+ *
+ * @param object
+ * The object that the property belongs to, cannot be null.
+ * @param property
+ * The property to get type, can be nested. for example,
+ * 'foo.bar.baz'.
+ * @return The type of the property. If the property doesn't exists in the
+ * object, returns null.
+ */
+ public static Class getPropertyType(Object object, String property) {
+ if (object == null)
+ throw new IllegalArgumentException("Object cannot be null");
+
+ return getPropertyType(object.getClass(), property);
+ }
+
+ /**
+ * Gets the type of a property of a class.
+ *
+ * @param clazz
+ * The class that the property belongs to, cannot be null.
+ * @param property
+ * The property to get type, can be nested. for example,
+ * 'foo.bar.baz'.
+ * @return The type of the property. If the property doesn't exists in the
+ * clazz, returns null.
+ */
+ public static Class getPropertyType(Class clazz, String property) {
+ if (clazz == null)
+ throw new IllegalArgumentException("clazz cannot be null");
+
+ if (property == null)
+ throw new IllegalArgumentException("property cannot be null");
+
+ int dotIndex = property.lastIndexOf('.');
+
+ if (dotIndex == -1) {
+ Method method = getReadMethod(clazz, property);
+ return method == null ? null : method.getReturnType();
+ }
+
+ String deepestProperty = property.substring(dotIndex + 1);
+ String parentProperty = property.substring(0, dotIndex);
+ return getPropertyType(getPropertyType(clazz, parentProperty),
+ deepestProperty);
+ }
+
+ /**
+ * Gets the read method for a property in a class.
+ *
+ * for example: <code>
+ * class Foo {
+ * public String getBar() { return "bar"; }
+ * public Boolean isBaz() { return false; }
+ * }
+ *
+ * BeanUtils.getReadMethod(Foo.class, "bar"); // return Foo#getBar()
+ * BeanUtils.getReadMethod(Foo.class, "baz"); // return Foo#isBaz()
+ * BeanUtils.getReadMethod(Foo.class, "baa"); // return null
+ * </code>
+ *
+ * @param clazz
+ * The class to get read method.
+ * @param property
+ * The property to get read method for, can NOT be nested.
+ * @return The read method (getter) for the property, if there is no read
+ * method for the property, returns null.
+ */
+ public static Method getReadMethod(Class clazz, String property) {
+ // Capitalize the property
+ StringBuffer buf = new StringBuffer();
+ buf.append(property.substring(0, 1).toUpperCase());
+ if (property.length() > 1) {
+ buf.append(property.substring(1));
+ }
+
+ Method method = null;
+ for (String prefix : getterPrefixes) {
+ String methodName = prefix + buf.toString();
+ try {
+ method = clazz.getMethod(methodName);
+
+ // Once get method successfully, jump out the loop.
+ break;
+ } catch (NoSuchMethodException e) {
+ // do nothing but logging
+ logger.debug("No such read method '" + methodName
+ + "()' in class '" + clazz.getName() + "'.", e);
+ } catch (SecurityException e) {
+ // do nothing but logging
+ logger.debug("Error occurs while getting read method '"
+ + methodName + "()' in class '" + clazz.getName()
+ + "'.", e);
+ }
+ }
+
+ return method;
+ }
+}
Index: src/org/jmesa/util/ItemUtils.java
===================================================================
--- src/org/jmesa/util/ItemUtils.java (revision 2310)
+++ src/org/jmesa/util/ItemUtils.java (working copy)
@@ -99,6 +99,13 @@
}
}

- return PropertyUtils.getPropertyType(item, property);
+ Class<?> type = null;
+ try {
+ type = PropertyUtils.getPropertyType(item, property);
+ } catch (Exception e) {
+ logger.debug("Had problems getting property type by object, trying reflection...", e);
+ type = BeanUtils.getPropertyType(item, property);
+ }
+ return type;
}
}
Powered by Google Project Hosting