1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.tools.database;
19
20 import java.util.*;
21 import java.lang.reflect.*;
22
23 import org.apache.commons.beanutils.PropertyUtils;
24
25
26 /***
27 * Helper class used to reflectively call setters and getters of beans
28 * and determine the return type.
29 *
30 * @author P.S.D.Reitsma, Finalist IT Group
31 * @version 1.0
32 */
33 public class MethodInvoker {
34
35 /***
36 * Returns the method of a bean using taking the property name and a prefix.
37 * It is used to find getters or setters (prefix="get" or "set") for a certain
38 * property. This method is <b>not</b> case sensitive.
39 * In case the method is not found in the current class, it recursively looks
40 * in superclasses until java.lang.Object is met.
41 * @param beanClass class of the bean object that owns the method
42 * @param property that is get or set by the method
43 * @param prefix String that is concatenated to the property
44 * @return the desired method
45 * @exception NoSuchMethodException thrown when the method is not found
46 */
47 public static Method getBeanMethod(Class beanClass, String property, String prefix) throws NoSuchMethodException {
48
49 Class descriptor = beanClass;
50 Method methlist[] = descriptor.getDeclaredMethods();
51 for (int i = 0; i < methlist.length; i++) {
52 if (methlist[i].getName().equalsIgnoreCase(prefix + property) ||
53 methlist[i].getName().equalsIgnoreCase(prefix + Helper.createAttNameInitCap(property))
54 ) {
55 return methlist[i];
56 }
57 }
58 if (!descriptor.getSuperclass().getName().equals("java.lang.Object")) {
59
60 return getBeanMethod(descriptor.getSuperclass(), property, prefix);
61 }
62 throw new NoSuchMethodException();
63 }
64
65
66 /***
67 * Returns the names of all the getters of the tested bean
68 *
69 * @return ArrayList containing the names
70 */
71 public static ArrayList getGetterNames(Object bean) {
72 Class descriptor = bean.getClass();
73 ArrayList res = new ArrayList();
74 Method methlist[] = descriptor.getDeclaredMethods();
75 for (int i = 0; i < methlist.length; i++) {
76 if (methlist[i].getName().startsWith("get")) {
77 res.add(methlist[i].getName().substring(3));
78 }
79 }
80 return res;
81 }
82
83
84 /***
85 * Executes the getter method of a property of a bean.
86 * @param bean the bean of which the method should be called
87 * @param property the property that should be derived
88 * @return the value of the property
89 * @exception IllegalAccessException
90 * @exception IllegalArgumentException
91 * @exception InvocationTargetException
92 * @exception NoSuchMethodException
93 */
94 public static Object getProperty(Object bean, String property)
95 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
96
97 try {
98 return PropertyUtils.getProperty(bean, property);
99 }
100 catch (Exception e) {
101 Method m = getBeanMethod(bean.getClass(), property, "get");
102 Object retObject = m.invoke(bean, null);
103 return retObject;
104 }
105 }
106
107
108 /***
109 * Executes the setter method of a property of a bean.
110 * @param bean the bean of which the method should be called
111 * @param property the property that is set
112 * @param bean the value that is set
113 * @exception IllegalAccessException
114 * @exception IllegalArgumentException
115 * @exception InvocationTargetException
116 * @exception NoSuchMethodException
117 */
118 public static void setProperty(Object bean, String property, Object value)
119 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
120
121 Method m = getBeanMethod(bean.getClass(), property, "set");
122 if (value != null) {
123 m.invoke(bean, new Object[]{value});
124 }
125 else {
126 m.invoke(bean, new Object[]{null});
127 }
128 }
129
130
131 /***
132 * Returns the object classname of a property of a bean.
133 *
134 * @param bean the bean to be examined
135 * @param property the property of which the return type is returned
136 * @exception IllegalArgumentException
137 * @exception NoSuchMethodException
138 */
139 public static String getReturnType(Object bean, String property)
140 throws IllegalArgumentException, NoSuchMethodException {
141
142 try {
143 String prop = Helper.createAttName(property);
144 Class cl = PropertyUtils.getPropertyType(bean, prop);
145
146 return cl.getName();
147
148 }
149 catch (Exception e) {
150 Method m = getBeanMethod(bean.getClass(), property, "get");
151 String retType = m.getReturnType().getName();
152 return retType;
153 }
154 }
155
156
157 /***
158 * Test a bean whether a certain getter exists for a property.
159 *
160 * @return boolean
161 * @param bean the bean that should be tested
162 * @param property the property of which the return type is returned
163 *
164 */
165 public static boolean getterExists(Object bean, String property) {
166 try {
167 getReturnType(bean, property);
168 return true;
169
170 }
171 catch (Exception E) {
172 return false;
173 }
174 }
175
176 }