1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.talika.commons.util;
24
25 import java.util.Map;
26 import java.util.Iterator;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.InvocationTargetException;
29
30 /**
31 * Utility class that allows access to beans setters and getters using reflexion.
32 *
33 * @author Jose M. Palomar
34 * @version $Revision: 113 $
35 */
36 public final class BeanHelper {
37
38
39
40
41
42 /**
43 * Class of bean we want access.
44 */
45 private Class beanClass;
46
47 /**
48 * Methods of bean we want access.
49 */
50 private Method[] beanMethods;
51
52
53 /**
54 * Constructs a new <code>BeanHelper</code> for the specified class name.
55 *
56 * @param className String Name of class of bean we want access.
57 * @throws ClassNotFoundException if the class cannot be located.
58 */
59 public BeanHelper(String className) throws ClassNotFoundException {
60 this.beanClass = Class.forName(className);
61 this.beanMethods = beanClass.getMethods();
62 }
63
64
65 /**
66 * Creates a new instance of bean.
67 *
68 * @return Object new instance of bean.
69 * @throws IllegalAccessException if the class or its nullary constructor is not
70 * accessible.
71 * @throws InstantiationException if this <code>Class</code> represents an
72 * abstract class, an interface, an array class, a primitive type, or void; or
73 * if the class has no nullary constructor; or if the instantiation fails for
74 * some other reason.
75 */
76 public Object getInstance() throws IllegalAccessException, InstantiationException {
77 return beanClass.newInstance();
78 }
79
80 /**
81 * Call set method of one specified property of bean.
82 *
83 * @param instance Object instance of bean.
84 * @param name String name of property.
85 * @param value Object value of property.
86 * @throws NoSuchMethodException if a matching method is not found.
87 * @throws IllegalAccessException if the underlying method enforces Java language
88 * access control and the underlying method is inaccessible.
89 * @throws InvocationTargetException if the underlying method throws an exception.
90 */
91 public void setProperty(Object instance, String name, Object value)
92 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
93
94 String methodName = "set" + name.substring(0, 1).toUpperCase() +
95 name.substring(1);
96
97 Method method = findMethod(methodName);
98 Object[] params = {value};
99 method.invoke(instance, params);
100
101 }
102
103 /**
104 * Call set method for a set of properties of bean.
105 *
106 * @param instance Object instance of bean.
107 * @param props Map map of properties in which keys are properties names and
108 * values are properties values.
109 * @throws NoSuchMethodException if a matching method is not found.
110 * @throws IllegalAccessException if the underlying method enforces Java language
111 * access control and the underlying method is inaccessible.
112 * @throws InvocationTargetException if the underlying method throws an exception.
113 */
114 public void setProperties(Object instance, Map props)
115 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
116
117 Iterator propNames = props.keySet().iterator();
118 while (propNames.hasNext()) {
119 String propName = (String) propNames.next();
120 setProperty(instance, propName, props.get(propName));
121 }
122
123 }
124
125 /**
126 * Call get method for a property of bean.
127 *
128 * @param instance Object instance of bean.
129 * @param name String name of property.
130 * @return Object value of property.
131 * @throws NoSuchMethodException if a matching method is not found.
132 * @throws IllegalAccessException if the underlying method enforces Java language
133 * access control and the underlying method is inaccessible.
134 * @throws InvocationTargetException if the underlying method throws an exception.
135 */
136 public Object getProperty(Object instance, String name)
137 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
138
139 String methodName = "get" + name.substring(0, 1).toUpperCase() +
140 name.substring(1);
141
142 Method method = findMethod(methodName);
143 return method.invoke(instance, null);
144
145 }
146
147 /**
148 * Searchs for a method of bean.
149 *
150 * @param name String name of method.
151 * @return Method method found.
152 * @throws NoSuchMethodException if a matching method is not found.
153 */
154 private Method findMethod(String name) throws NoSuchMethodException {
155
156 for (int i = 0; i < beanMethods.length; i++) {
157 if (beanMethods[i].getName().equals(name) &&
158 beanMethods[i].getParameterTypes().length == 1) {
159 return beanMethods[i];
160 }
161 }
162
163 throw new NoSuchMethodException("Property method " + name + " not found");
164
165 }
166
167 }