Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReflectionUtils |
|
| 2.6666666666666665;2.667 |
1 | package org.jtheque.utils.bean; | |
2 | ||
3 | import org.jtheque.utils.StringUtils; | |
4 | import org.slf4j.LoggerFactory; | |
5 | ||
6 | import java.beans.BeanInfo; | |
7 | import java.beans.IntrospectionException; | |
8 | import java.beans.Introspector; | |
9 | import java.beans.PropertyDescriptor; | |
10 | import java.lang.reflect.InvocationTargetException; | |
11 | import java.lang.reflect.Method; | |
12 | ||
13 | /* | |
14 | * This file is part of JTheque. | |
15 | * | |
16 | * JTheque is free software: you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation, either version 3 of the License. | |
19 | * | |
20 | * JTheque is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | * GNU General Public License for more details. | |
24 | * | |
25 | * You should have received a copy of the GNU General Public License | |
26 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
27 | */ | |
28 | ||
29 | /** | |
30 | * An utility class for Reflection. | |
31 | * | |
32 | * @author Baptiste Wicht | |
33 | */ | |
34 | public final class ReflectionUtils { | |
35 | 2 | private static final PropertyDescriptor[] EMPTY_PROPERTY_DESCRIPTOR_ARRAY = {}; |
36 | ||
37 | /** | |
38 | * Utility class, not instanciable. | |
39 | */ | |
40 | private ReflectionUtils() { | |
41 | 0 | super(); |
42 | 0 | } |
43 | ||
44 | /** | |
45 | * Return the name of the getter for the specified property name. | |
46 | * | |
47 | * @param property The property name. | |
48 | * @return The name of the getter for the specified property. | |
49 | */ | |
50 | public static String getGetter(String property) { | |
51 | 12 | return "get" + StringUtils.setFirstLetterUpper(property); |
52 | } | |
53 | ||
54 | /** | |
55 | * Return the getter method for the property of the specified bean. | |
56 | * | |
57 | * @param bean The bean to get the property from. | |
58 | * @param property The property name. | |
59 | * @return The getter Method object or null if there is no getter for this property. | |
60 | */ | |
61 | public static Method getGetterMethod(Object bean, String property) { | |
62 | try { | |
63 | 8 | String getter = getGetter(property); |
64 | ||
65 | 8 | return bean.getClass().getMethod(getter); |
66 | 0 | } catch (NoSuchMethodException e) { |
67 | 0 | LoggerFactory.getLogger(ReflectionUtils.class).error("The getter doesn't exists", e); |
68 | } | |
69 | ||
70 | 0 | return null; |
71 | } | |
72 | ||
73 | /** | |
74 | * Return the name of the setter for the specified property name. | |
75 | * | |
76 | * @param property The property name. | |
77 | * | |
78 | * @return The name of the setter for the specified property. | |
79 | */ | |
80 | public static String getSetter(String property) { | |
81 | 0 | return "set" + StringUtils.setFirstLetterUpper(property); |
82 | } | |
83 | ||
84 | /** | |
85 | * Return the setter method for the property of the specified bean. | |
86 | * | |
87 | * @param bean The bean to set the property to. | |
88 | * @param property The property name. | |
89 | * | |
90 | * @return The setter Method object or null if there is no setter for this property. | |
91 | */ | |
92 | public static Method getSetterMethod(Object bean, String property) { | |
93 | 0 | String setter = getSetter(property); |
94 | ||
95 | 0 | for(Method m : bean.getClass().getMethods()){ |
96 | 0 | if(m.getName().equals(setter)){ |
97 | 0 | return m; |
98 | } | |
99 | } | |
100 | ||
101 | 0 | return null; |
102 | } | |
103 | ||
104 | /** | |
105 | * Return the value of the property of the specified bean. | |
106 | * | |
107 | * @param bean The bean to get the value from. | |
108 | * @param property The property name. | |
109 | * @return The value of the property or null if there is no getter method. | |
110 | */ | |
111 | public static Object getPropertyValue(Object bean, String property) { | |
112 | try { | |
113 | 6 | Method m = getGetterMethod(bean, property); |
114 | ||
115 | 6 | return m.invoke(bean); |
116 | 0 | } catch (IllegalAccessException e) { |
117 | 0 | LoggerFactory.getLogger(ReflectionUtils.class).error("Unable to access the property getter method", e); |
118 | 0 | } catch (InvocationTargetException e) { |
119 | 0 | LoggerFactory.getLogger(ReflectionUtils.class).error("Problem during getter invocation", e); |
120 | 0 | } |
121 | ||
122 | 0 | return null; |
123 | } | |
124 | ||
125 | /** | |
126 | * Return the value of a property. | |
127 | * <p/> | |
128 | * Note : The properties of the Object class are not retrieved. | |
129 | * | |
130 | * @param bean The bean to get the property value from. | |
131 | * @param property The property. | |
132 | * @return the value of the property. | |
133 | */ | |
134 | public static Object getProperty(Object bean, PropertyDescriptor property) { | |
135 | 14 | Method m = property.getReadMethod(); |
136 | ||
137 | 14 | Object o = null; |
138 | ||
139 | try { | |
140 | 14 | o = m.invoke(bean); |
141 | 0 | } catch (IllegalAccessException e) { |
142 | 0 | LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e); |
143 | 0 | } catch (InvocationTargetException e) { |
144 | 0 | LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e); |
145 | 14 | } |
146 | ||
147 | 14 | return o; |
148 | } | |
149 | ||
150 | /** | |
151 | * Return all the properties of a bean. The results are cached using the Introspector class. | |
152 | * <p/> | |
153 | * Note : The properties of the Object class are not retrieved. | |
154 | * | |
155 | * @param bean The bean to extract the properties from. | |
156 | * @return An array containing all the properties of the bean or an empty array if there was an error during the | |
157 | * property retrieving process. | |
158 | */ | |
159 | public static PropertyDescriptor[] getProperties(Object bean) { | |
160 | try { | |
161 | 10 | BeanInfo info = Introspector.getBeanInfo(bean.getClass(), Object.class); |
162 | ||
163 | 10 | return info.getPropertyDescriptors(); |
164 | 0 | } catch (IntrospectionException e) { |
165 | 0 | LoggerFactory.getLogger(ReflectionUtils.class).error("Unable to get the properties", e); |
166 | ||
167 | 0 | return EMPTY_PROPERTY_DESCRIPTOR_ARRAY; |
168 | } | |
169 | } | |
170 | ||
171 | /** | |
172 | * Return the value of a property. | |
173 | * <p/> | |
174 | * Note : The properties of the Object class are not retrieved. | |
175 | * | |
176 | * @param bean The bean to get the property value from. | |
177 | * @param property The property. | |
178 | * @return the value of the property. | |
179 | */ | |
180 | public static Object getProperty(Object bean, String property) { | |
181 | 2 | for (PropertyDescriptor p : getProperties(bean)) { |
182 | 2 | if (p.getName().equals(property)) { |
183 | 2 | return getProperty(bean, p); |
184 | } | |
185 | } | |
186 | ||
187 | 0 | return null; |
188 | } | |
189 | } |