2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.util;
19
20 import java.beans.BeanInfo;
21 import java.beans.IntrospectionException;
22 import java.beans.Introspector;
23 import java.beans.PropertyDescriptor;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import javax.faces.el.EvaluationException;
27 import javax.faces.el.PropertyNotFoundException;
28
29 /***
30 * <p>Helper class to provide access to the properties of JavaBeans. This
31 * implementation is stateless and maintains no cached information, so
32 * instances may be freely created and destroyed, with no side effects
33 * (unless there are side effects from caching inside the JDK itself).</p>
34 *
35 * $Id: PropertyHelper.java 464373 2006-10-16 04:21:54Z rahul $
36 *
37 * @since 1.0.1
38 */
39 public class PropertyHelper {
40
41
42
43
44
45 /***
46 * <p>Return the value of the specified property from the specified bean.</p>
47 *
48 * @param bean Bean whose property is to be retrieved
49 * @param name Name of the property to get
50 *
51 * @exception PropertyNotFoundException if the bean class does not have
52 * a property with the specified name, or if the property is write only
53 * @exception EvaluationException if an exception occurs while
54 * retrieving the property value
55 */
56 public Object getValue(Object bean, String name) {
57
58 PropertyDescriptor descriptor = descriptor(bean, name);
59 Method method = descriptor.getReadMethod();
60 if (method == null) {
61 throw new PropertyNotFoundException(name);
62 }
63
64 try {
65 return method.invoke(bean, new Object[0]);
66 } catch (InvocationTargetException e) {
67 throw new EvaluationException(e.getCause());
68 } catch (Exception e) {
69 throw new EvaluationException(e);
70 }
71
72 }
73
74
75 /***
76 * <p>Return <code>true</code> if the specified property is read only on
77 * the underlying bean class.</p>
78 *
79 * @param bean Bean whose property is to be checked
80 * @param name Name of the property to check
81 *
82 * @exception PropertyNotFoundException if the bean class does not have
83 * a property with the specified name, or if the property is write only
84 * @exception EvaluationException if an exception occurs while
85 * checking the read only status
86 */
87 public boolean isReadOnly(Object bean, String name) {
88
89 PropertyDescriptor descriptor = descriptor(bean, name);
90 if (descriptor.getReadMethod() != null) {
91 return descriptor.getWriteMethod() == null;
92 }
93 throw new PropertyNotFoundException(name);
94
95 }
96
97
98 /***
99 * <p>Return the type of the specified property on the underlying bean
100 * class.</p>
101 *
102 * @param bean Bean whose property is to be analyzed
103 * @param name Name of the property to return the type of
104 *
105 * @exception PropertyNotFoundException if the bean class does not have
106 * a property with the specified name
107 * @exception EvaluationException if an exception occurs while
108 * retrieving the property type
109 */
110 public Class getType(Object bean, String name) {
111
112 PropertyDescriptor descriptor = descriptor(bean, name);
113 return descriptor.getPropertyType();
114
115 }
116
117
118 /***
119 * <p>Set the specified value on the specified property of the specified
120 * bean.</p>
121 *
122 * @param bean Bean whose property is to be set
123 * @param name Name of the property to be set
124 * @param value New value for this property
125 *
126 * @exception PropertyNotFoundException if the bean class does not have
127 * a property with the specified name, or if the property is read only
128 * @exception EvaluationException if an exception occurs while
129 * setting the property value
130 */
131 public void setValue(Object bean, String name, Object value) {
132
133 PropertyDescriptor descriptor = descriptor(bean, name);
134 Method method = descriptor.getWriteMethod();
135 if (method == null) {
136 throw new PropertyNotFoundException(name);
137 }
138
139 try {
140 method.invoke(bean, new Object[] { value });
141 } catch (InvocationTargetException e) {
142 throw new EvaluationException(e.getCause());
143 } catch (Exception e) {
144 throw new EvaluationException(e);
145 }
146
147 }
148
149
150
151
152
153 /***
154 * <p>Return the <code>PropertyDescriptor</code> for the specified
155 * property of the specified class.</p>
156 *
157 * @param bean Bean whose property descriptor is to be returned
158 * @param name Name of the property to retrieve a descrpitor for
159 *
160 * @exception PropertyNotFoundException if the bean class does not have
161 * a property with the specified name
162 * @exception EvaluationException if an exception occurs introspecting
163 * the underlying bean class
164 */
165 private PropertyDescriptor descriptor(Object bean, String name) {
166
167 BeanInfo beanInfo = null;
168 try {
169 beanInfo = Introspector.getBeanInfo(bean.getClass());
170 } catch (IntrospectionException e) {
171 throw new EvaluationException(e);
172 }
173
174 PropertyDescriptor descriptors[] = beanInfo.getPropertyDescriptors();
175 if (descriptors == null) {
176 descriptors = new PropertyDescriptor[0];
177 }
178 for (int i = 0; i < descriptors.length; i++) {
179 if (name.equals(descriptors[i].getName())) {
180 return descriptors[i];
181 }
182 }
183
184 throw new PropertyNotFoundException(name);
185
186 }
187
188
189 }