1 | /* |
2 | * Copyright 2005-2006 The RbUtils Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | */ |
17 | |
18 | // $Id: ReflectUtils.java,v 1.1 2006/09/16 14:36:10 redfish Exp $ |
19 | |
20 | package org.ktc.rbutils.api.reflect; |
21 | |
22 | import java.lang.reflect.Constructor; |
23 | import java.lang.reflect.Field; |
24 | import java.lang.reflect.Method; |
25 | import org.apache.commons.lang.Validate; |
26 | import org.apache.commons.lang.exception.NestableRuntimeException; |
27 | |
28 | /** |
29 | * Provides reflection utilities. This could be used to get field value or to invoke method which |
30 | * have private or package private accessor. |
31 | * @since RbUtils 0.7.3 |
32 | * @version $Revision: 1.1 $ |
33 | * @author ktcguru |
34 | */ |
35 | public class ReflectUtils { |
36 | /** |
37 | * Does Nothing. |
38 | */ |
39 | protected ReflectUtils() { |
40 | // Do Nothing |
41 | } |
42 | |
43 | /** |
44 | * Invokes a constructor with given arguments. |
45 | * @param targetObjectClass the name of the class of the object to be constructed. |
46 | * @param argumentsType the list of class argument as they appear in the constructor. |
47 | * @param argumentsValue the list of value argument as they appear in the constructor. |
48 | * @return the instance of the object. |
49 | * @throws RuntimeException if an error occurs on invokation. |
50 | */ |
51 | public static Object invokeConstructor(final Class targetObjectClass, |
52 | final Class[] argumentsType, |
53 | final Object[] argumentsValue) |
54 | { |
55 | // Validate arguments |
56 | Validate.notNull(targetObjectClass); |
57 | |
58 | Object returnedValue = null; |
59 | try { |
60 | final Constructor constructor = targetObjectClass.getDeclaredConstructor(argumentsType); |
61 | constructor.setAccessible(true); |
62 | returnedValue = constructor.newInstance(argumentsValue); |
63 | constructor.setAccessible(false); |
64 | } |
65 | catch (final Exception exception) { |
66 | // BUGS_2 fix |
67 | // throw new RuntimeException(exception); |
68 | throw new NestableRuntimeException(exception); |
69 | } |
70 | return returnedValue; |
71 | } |
72 | |
73 | /** |
74 | * Invokes a method with given arguments on an instance of an object. |
75 | * @param targetObject the instance of the object on which the method will be invoked. |
76 | * @param methodName the name of the method to be invoked. |
77 | * @param argumentsType the list of class argument as they appear in the constructor. |
78 | * @param argumentsValue the list of value argument as they appear in the constructor. |
79 | * @return the returned value of the invoked method. |
80 | * @throws org.apache.commons.lang.NullArgumentException if <code>targetObject</code> is |
81 | * <code>null</code> |
82 | * @throws RuntimeException if an error occurs on invokation. |
83 | */ |
84 | public static Object invokeMethod(final Object targetObject, final String methodName, |
85 | final Class[] argumentsType, final Object[] argumentsValue) |
86 | { |
87 | // Validate arguments |
88 | Validate.notNull(targetObject); |
89 | Object returnedValue = null; |
90 | try { |
91 | returnedValue = invokeMethodImpl(targetObject.getClass(), targetObject, methodName, |
92 | argumentsType, argumentsValue); |
93 | } |
94 | catch (final Exception exception) { |
95 | // BUGS_2 fix |
96 | // throw new RuntimeException(exception); |
97 | throw new NestableRuntimeException(exception); |
98 | } |
99 | return returnedValue; |
100 | } |
101 | |
102 | /** |
103 | * Invokes a static method of a given class with given arguments. |
104 | * @param targetClass the name of the class on which the method will be invoked. |
105 | * @param methodName the name of the method to be invoked. |
106 | * @param argumentsType the list of class argument as they appear in the constructor. |
107 | * @param argumentsValue the list of value argument as they appear in the constructor. |
108 | * @return the returned value of the invoked method. |
109 | * @throws org.apache.commons.lang.NullArgumentException if <code>targetObject</code> or |
110 | * <code>methodName</code> is <code>null</code> |
111 | * @throws RuntimeException if an error occurs on invokation. |
112 | */ |
113 | public static Object invokeMethod(final Class targetClass, final String methodName, |
114 | final Class[] argumentsType, final Object[] argumentsValue) |
115 | { |
116 | Object returnedValue = null; |
117 | try { |
118 | returnedValue = invokeMethodImpl(targetClass, null, methodName, argumentsType, |
119 | argumentsValue); |
120 | } |
121 | catch (final Exception exception) { |
122 | // BUGS_2 fix |
123 | // throw new RuntimeException(exception); |
124 | throw new NestableRuntimeException(exception); |
125 | } |
126 | return returnedValue; |
127 | } |
128 | |
129 | /** |
130 | * Invokes a method with given arguments. |
131 | * <p> |
132 | * <code>targetObject</code> can be <code>null</code> if you want to invoke a static method. |
133 | * Otherwise, an exception will occur. |
134 | * @param targetClass the name of the class on which the method will be invoked. |
135 | * @param targetObject the instance of the object on which the method will be invoked. |
136 | * @param methodName the name of the method to be invoked. |
137 | * @param argumentsType the list of class argument as they appear in the constructor. |
138 | * @param argumentsValue the list of value argument as they appear in the constructor. |
139 | * @return the returned value of the invoked method. |
140 | * @throws org.apache.commons.lang.NullArgumentException if <code>targetClass</code> or |
141 | * <code>methodName</code> is <code>null</code> |
142 | * @throws Exception if an error occurs on invokation. |
143 | */ |
144 | private static Object invokeMethodImpl(final Class targetClass, final Object targetObject, |
145 | final String methodName, final Class[] argumentsType, |
146 | final Object[] argumentsValue) throws Exception |
147 | { |
148 | // Validate arguments |
149 | Validate.notNull(targetClass); |
150 | Validate.notNull(methodName); |
151 | |
152 | // Invoke Method |
153 | final Method method = targetClass.getDeclaredMethod(methodName, argumentsType); |
154 | method.setAccessible(true); |
155 | final Object returnedValue = method.invoke(targetObject, argumentsValue); |
156 | method.setAccessible(false); |
157 | |
158 | return returnedValue; |
159 | } |
160 | |
161 | /** |
162 | * Get the value of a field. |
163 | * @param targetObject the instance of the object the field belongs. |
164 | * @param fieldName the name of the field. |
165 | * @return the value of the field. |
166 | * @throws org.apache.commons.lang.NullArgumentException if <code>targetObject</code> is |
167 | * <code>null</code> |
168 | * @throws RuntimeException if an error occurs on invokation. |
169 | */ |
170 | public static Object getFieldValue(final Object targetObject, final String fieldName) { |
171 | // Validate arguments |
172 | Validate.notNull(targetObject); |
173 | |
174 | Object returnedValue = null; |
175 | try { |
176 | final Field field = targetObject.getClass().getDeclaredField(fieldName); |
177 | field.setAccessible(true); |
178 | returnedValue = field.get(targetObject); |
179 | field.setAccessible(false); |
180 | } |
181 | catch (final Exception exception) { |
182 | // BUGS_2 fix |
183 | // throw new RuntimeException(exception); |
184 | throw new NestableRuntimeException(exception); |
185 | } |
186 | return returnedValue; |
187 | } |
188 | |
189 | // TODO Code - faire une méthode pour les champs static (idem méthode) |
190 | |
191 | //Code - factoriser avec getObject |
192 | //Code - cette méthode ne doit faire qu'un cast - est-elle utile? |
193 | // public static boolean getBooleanValue(final Object targetObject, final String fieldName) { |
194 | // // Validate arguments |
195 | // Validate.notNull(targetObject); |
196 | // |
197 | // boolean returnedValue = false; |
198 | // try { |
199 | // final Field field = targetObject.getClass().getDeclaredField(fieldName); |
200 | // field.setAccessible(true); |
201 | // returnedValue = field.getBoolean(targetObject); |
202 | // field.setAccessible(false); |
203 | // } |
204 | // catch (final Exception exception) { |
205 | // throw new RuntimeException(exception); |
206 | // } |
207 | // return returnedValue; |
208 | // } |
209 | |
210 | // public static int getIntValueForField(final Object targetObject, final String attributeName) |
211 | // { |
212 | // // Validate arguments |
213 | // Validate.notNull(targetObject); |
214 | // Validate.notNull(attributeName); |
215 | // |
216 | // int returnedValue = 0; |
217 | // try { |
218 | // final Field lifeCycleManagedType = targetObject.getClass() |
219 | // .getDeclaredField(attributeName); |
220 | // lifeCycleManagedType.setAccessible(true); |
221 | // returnedValue = lifeCycleManagedType.getInt(targetObject); |
222 | // lifeCycleManagedType.setAccessible(false); |
223 | // } |
224 | // catch (final Exception exception) { |
225 | // throw new RuntimeException(exception); |
226 | // } |
227 | // return returnedValue; |
228 | // } |
229 | |
230 | // public static void setFieldValue(final Object targetObject, final String attributeName, |
231 | // final Object value) |
232 | // { |
233 | // // Validate arguments |
234 | // Validate.notNull(targetObject); |
235 | // Validate.notNull(attributeName); |
236 | // |
237 | // Field field = null; |
238 | // try { |
239 | // field = targetObject.getClass().getDeclaredField(attributeName); |
240 | // } |
241 | // catch (final NoSuchFieldException e) { |
242 | // try { |
243 | // field = targetObject.getClass().getSuperclass().getDeclaredField(attributeName); |
244 | // } |
245 | // catch (final Exception exception) { |
246 | // throw new RuntimeException(exception); |
247 | // } |
248 | // } |
249 | // if (null != field) { |
250 | // try { |
251 | // field.setAccessible(true); |
252 | // field.set(targetObject, value); |
253 | // field.setAccessible(false); |
254 | // } |
255 | // catch (final Exception exception) { |
256 | // throw new RuntimeException(exception); |
257 | // } |
258 | // } |
259 | // } |
260 | |
261 | // public static void setIntForField(final Object targetObject, final String attributeName, |
262 | // final int value) |
263 | // { |
264 | // // Validate arguments |
265 | // Validate.notNull(targetObject); |
266 | // Validate.notNull(attributeName); |
267 | // |
268 | // Field field = null; |
269 | // try { |
270 | // field = targetObject.getClass().getDeclaredField(attributeName); |
271 | // } |
272 | // catch (NoSuchFieldException e) { |
273 | // try { |
274 | // field = targetObject.getClass().getSuperclass().getDeclaredField(attributeName); |
275 | // } |
276 | // catch (SecurityException e1) { |
277 | // } |
278 | // catch (NoSuchFieldException e1) { |
279 | // e1.printStackTrace(); |
280 | // } |
281 | // } |
282 | // if (null != field) { |
283 | // try { |
284 | // field.setAccessible(true); |
285 | // field.setInt(targetObject, value); |
286 | // field.setAccessible(false); |
287 | // } |
288 | // catch (SecurityException e2) { |
289 | // } |
290 | // catch (IllegalArgumentException e2) { |
291 | // } |
292 | // catch (IllegalAccessException e2) { |
293 | // } |
294 | // } |
295 | // } |
296 | |
297 | } |