2
* @(#)Methods.java 1.3.1 2006-09-18
4
* Copyright (c) 2005-2006 Werner Randelshofer
5
* Staldenmattweg 2, Immensee, CH-6405, Switzerland.
8
* This software is the confidential and proprietary information of
9
* Werner Randelshofer. ("Confidential Information"). You shall not
10
* disclose such Confidential Information and shall use it only in
11
* accordance with the terms of the license agreement you entered into
12
* with Werner Randelshofer.
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.util;
17
import java.lang.reflect.*;
19
* Methods contains convenience methods for method invocations using
22
* @author Werner Randelshofer
23
* @version 1.3.1 2006-09-18 Fixed javadoc warnings.
24
* <br>1.2 2006-08-20 Additional invokeIfExists method added.
25
* <br>1.2 2006-05-07 Added invokeNew method.
26
* <br>1.1 2006-02-18 Added more convenience methods.
27
* <br>1.0 September 24, 2005 Created.
29
public class Methods {
31
* Prevent instance creation.
37
* Invokes the specified accessible parameterless method if it exists.
39
* @param obj The object on which to invoke the method.
40
* @param methodName The name of the method.
41
* @return The return value of the method.
42
* @return NoSuchMethodException if the method does not exist or is not
45
public static Object invoke(Object obj, String methodName)
46
throws NoSuchMethodException {
48
Method method = obj.getClass().getMethod(methodName, new Class[0]);
49
Object result = method.invoke(obj, new Object[0]);
51
} catch (IllegalAccessException e) {
52
throw new NoSuchMethodException(methodName+" is not accessible");
53
} catch (InvocationTargetException e) {
54
// The method is not supposed to throw exceptions
55
throw new InternalError(e.getMessage());
59
* Invokes the specified accessible method with a string parameter if it exists.
61
* @param obj The object on which to invoke the method.
62
* @param methodName The name of the method.
63
* @param stringParameter The String parameter
64
* @return The return value of the method or METHOD_NOT_FOUND.
65
* @return NoSuchMethodException if the method does not exist or is not accessible.
67
public static Object invoke(Object obj, String methodName, String stringParameter)
68
throws NoSuchMethodException {
70
Method method = obj.getClass().getMethod(methodName, new Class[] { String.class });
71
Object result = method.invoke(obj, new Object[] { stringParameter });
73
} catch (IllegalAccessException e) {
74
throw new NoSuchMethodException(methodName+" is not accessible");
75
} catch (InvocationTargetException e) {
76
// The method is not supposed to throw exceptions
77
throw new InternalError(e.getMessage());
82
* Invokes the specified method if it exists.
84
* @param obj The object on which to invoke the method.
85
* @param methodName The name of the method.
86
* @param types The parameter types.
87
* @param values The parameter values.
88
* @return The return value of the method.
89
* @return NoSuchMethodException if the method does not exist or is not accessible.
91
public static Object invoke(Object obj, String methodName, Class[] types, Object[] values)
92
throws NoSuchMethodException {
94
Method method = obj.getClass().getMethod(methodName, types);
95
Object result = method.invoke(obj, values);
97
} catch (IllegalAccessException e) {
98
throw new NoSuchMethodException(methodName+" is not accessible");
99
} catch (InvocationTargetException e) {
100
// The method is not supposed to throw exceptions
101
throw new InternalError(e.getMessage());
105
* Invokes the specified accessible parameterless method if it exists.
107
* @param clazz The class on which to invoke the method.
108
* @param methodName The name of the method.
109
* @return The return value of the method or METHOD_NOT_FOUND.
110
* @return NoSuchMethodException if the method does not exist or is not accessible.
112
public static Object invokeStatic(Class clazz, String methodName)
113
throws NoSuchMethodException {
115
Method method = clazz.getMethod(methodName, new Class[0]);
116
Object result = method.invoke(null, new Object[0]);
118
} catch (IllegalAccessException e) {
119
throw new NoSuchMethodException(methodName+" is not accessible");
120
} catch (InvocationTargetException e) {
121
// The method is not supposed to throw exceptions
122
throw new InternalError(e.getMessage());
126
* Invokes the specified static parameterless method if it exists.
128
* @param clazz The class on which to invoke the method.
129
* @param methodName The name of the method.
130
* @return The return value of the method.
131
* @return NoSuchMethodException if the method does not exist or is not accessible.
133
public static Object invokeStatic(String clazz, String methodName)
134
throws NoSuchMethodException {
136
return invokeStatic(Class.forName(clazz), methodName);
137
} catch (ClassNotFoundException e) {
138
throw new NoSuchMethodException("class "+clazz+" not found");
142
* Invokes the specified static method if it exists.
144
* @param clazz The class on which to invoke the method.
145
* @param methodName The name of the method.
146
* @param types The parameter types.
147
* @param values The parameter values.
148
* @return The return value of the method.
149
* @return NoSuchMethodException if the method does not exist or is not accessible.
151
public static Object invokeStatic(Class clazz, String methodName,
152
Class[] types, Object[] values)
153
throws NoSuchMethodException {
155
Method method = clazz.getMethod(methodName, types);
156
Object result = method.invoke(null, values);
158
} catch (IllegalAccessException e) {
159
throw new NoSuchMethodException(methodName+" is not accessible");
160
} catch (InvocationTargetException e) {
161
// The method is not supposed to throw exceptions
162
throw new InternalError(e.getMessage());
166
* Invokes the specified static method if it exists.
168
* @param clazz The class on which to invoke the method.
169
* @param methodName The name of the method.
170
* @param types The parameter types.
171
* @param values The parameter values.
172
* @return The return value of the method.
173
* @return NoSuchMethodException if the method does not exist or is not accessible.
175
public static Object invokeStatic(String clazz, String methodName,
176
Class[] types, Object[] values)
177
throws NoSuchMethodException {
179
return invokeStatic(Class.forName(clazz), methodName, types, values);
180
} catch (ClassNotFoundException e) {
181
throw new NoSuchMethodException("class "+clazz+" not found");
185
* Invokes the specified static method if it exists.
187
* @param clazz The class on which to invoke the method.
188
* @param methodName The name of the method.
189
* @param type The parameter types.
190
* @param value The parameter values.
191
* @return The return value of the method.
192
* @return NoSuchMethodException if the method does not exist or is not accessible.
194
public static Object invokeStatic(String clazz, String methodName,
195
Class type, Object value)
196
throws NoSuchMethodException {
198
return invokeStatic(Class.forName(clazz), methodName, new Class[] {type}, new Object[] {value});
199
} catch (ClassNotFoundException e) {
200
throw new NoSuchMethodException("class "+clazz+" not found");
204
* Invokes the specified static method if it exists.
206
* @param clazz The class on which to invoke the method.
207
* @param methodName The name of the method.
208
* @param types The parameter types.
209
* @param values The parameter values.
210
* @param defaultValue The default value.
211
* @return The return value of the method or the default value if the method
212
* does not exist or is not accessible.
214
public static Object invokeStatic(String clazz, String methodName,
215
Class[] types, Object[] values, Object defaultValue) {
217
return invokeStatic(Class.forName(clazz), methodName, types, values);
218
} catch (ClassNotFoundException e) {
220
} catch (NoSuchMethodException e) {
225
* Invokes the specified static method if it exists.
227
* @param clazz The class on which to invoke the method.
228
* @param methodName The name of the method.
229
* @param type The parameter type.
230
* @param value The parameter value.
231
* @return The return value of the method or the default value if the method
232
* does not exist or is not accessible.
234
public static Object invokeStatic(Class clazz, String methodName,
235
Class type, Object value) throws NoSuchMethodException {
236
return invokeStatic(clazz, methodName, new Class[] {type}, new Object[] {value});
240
* Invokes the specified getter method if it exists.
242
* @param obj The object on which to invoke the method.
243
* @param methodName The name of the method.
244
* @param defaultValue This value is returned, if the method does not exist.
245
* @return The value returned by the getter method or the default value.
247
public static int invokeGetter(Object obj, String methodName, int defaultValue) {
249
Method method = obj.getClass().getMethod(methodName, new Class[0]);
250
Object result = method.invoke(obj, new Object[0]);
251
return (Integer) result;
252
} catch (NoSuchMethodException e) {
254
} catch (IllegalAccessException e) {
256
} catch (InvocationTargetException e) {
261
* Invokes the specified getter method if it exists.
263
* @param obj The object on which to invoke the method.
264
* @param methodName The name of the method.
265
* @param defaultValue This value is returned, if the method does not exist.
266
* @return The value returned by the getter method or the default value.
268
public static long invokeGetter(Object obj, String methodName, long defaultValue) {
270
Method method = obj.getClass().getMethod(methodName, new Class[0]);
271
Object result = method.invoke(obj, new Object[0]);
272
return (Long) result;
273
} catch (NoSuchMethodException e) {
275
} catch (IllegalAccessException e) {
277
} catch (InvocationTargetException e) {
282
* Invokes the specified getter method if it exists.
284
* @param obj The object on which to invoke the method.
285
* @param methodName The name of the method.
286
* @param defaultValue This value is returned, if the method does not exist.
287
* @return The value returned by the getter method or the default value.
289
public static boolean invokeGetter(Object obj, String methodName, boolean defaultValue) {
291
Method method = obj.getClass().getMethod(methodName, new Class[0]);
292
Object result = method.invoke(obj, new Object[0]);
293
return (Boolean) result;
294
} catch (NoSuchMethodException e) {
296
} catch (IllegalAccessException e) {
298
} catch (InvocationTargetException e) {
303
* Invokes the specified getter method if it exists.
305
* @param obj The object on which to invoke the method.
306
* @param methodName The name of the method.
307
* @param defaultValue This value is returned, if the method does not exist.
308
* @return The value returned by the getter method or the default value.
310
public static Object invokeGetter(Object obj, String methodName, Object defaultValue) {
312
Method method = obj.getClass().getMethod(methodName, new Class[0]);
313
Object result = method.invoke(obj, new Object[0]);
315
} catch (NoSuchMethodException e) {
317
} catch (IllegalAccessException e) {
319
} catch (InvocationTargetException e) {
324
* Invokes the specified getter method if it exists.
326
* @param clazz The class on which to invoke the method.
327
* @param methodName The name of the method.
328
* @param defaultValue This value is returned, if the method does not exist.
329
* @return The value returned by the getter method or the default value.
331
public static boolean invokeStaticGetter(Class clazz, String methodName, boolean defaultValue) {
333
Method method = clazz.getMethod(methodName, new Class[0]);
334
Object result = method.invoke(null, new Object[0]);
335
return (Boolean) result;
336
} catch (NoSuchMethodException e) {
338
} catch (IllegalAccessException e) {
340
} catch (InvocationTargetException e) {
345
* Invokes the specified setter method if it exists.
347
* @param obj The object on which to invoke the method.
348
* @param methodName The name of the method.
350
public static Object invoke(Object obj, String methodName, boolean newValue)
351
throws NoSuchMethodException {
353
Method method = obj.getClass().getMethod(methodName, new Class[] { Boolean.TYPE} );
354
return method.invoke(obj, new Object[] {newValue});
355
} catch (IllegalAccessException e) {
356
throw new NoSuchMethodException(methodName+" is not accessible");
357
} catch (InvocationTargetException e) {
358
// The method is not supposed to throw exceptions
359
throw new InternalError(e.getMessage());
363
* Invokes the specified method if it exists.
365
* @param obj The object on which to invoke the method.
366
* @param methodName The name of the method.
368
public static Object invoke(Object obj, String methodName, int newValue)
369
throws NoSuchMethodException {
371
Method method = obj.getClass().getMethod(methodName, new Class[] { Integer.TYPE} );
372
return method.invoke(obj, new Object[] {newValue});
373
} catch (IllegalAccessException e) {
374
throw new NoSuchMethodException(methodName+" is not accessible");
375
} catch (InvocationTargetException e) {
376
// The method is not supposed to throw exceptions
377
throw new InternalError(e.getMessage());
381
* Invokes the specified setter method if it exists.
383
* @param obj The object on which to invoke the method.
384
* @param methodName The name of the method.
386
public static Object invoke(Object obj, String methodName, float newValue)
387
throws NoSuchMethodException {
389
Method method = obj.getClass().getMethod(methodName, new Class[] { Float.TYPE} );
390
return method.invoke(obj, new Object[] {newValue});
391
} catch (IllegalAccessException e) {
392
throw new NoSuchMethodException(methodName+" is not accessible");
393
} catch (InvocationTargetException e) {
394
// The method is not supposed to throw exceptions
395
throw new InternalError(e.getMessage());
399
* Invokes the specified setter method if it exists.
401
* @param obj The object on which to invoke the method.
402
* @param methodName The name of the method.
404
public static Object invoke(Object obj, String methodName, Class clazz, Object newValue)
405
throws NoSuchMethodException {
407
Method method = obj.getClass().getMethod(methodName, new Class[] { clazz } );
408
return method.invoke(obj, new Object[] { newValue});
409
} catch (IllegalAccessException e) {
410
throw new NoSuchMethodException(methodName+" is not accessible");
411
} catch (InvocationTargetException e) {
412
// The method is not supposed to throw exceptions
413
throw new InternalError(e.getMessage());
417
* Invokes the specified setter method if it exists.
419
* @param obj The object on which to invoke the method.
420
* @param methodName The name of the method.
422
public static void invokeIfExists(Object obj, String methodName) {
424
invoke(obj, methodName);
425
} catch (NoSuchMethodException e) {
430
* Invokes the specified setter method if it exists.
432
* @param obj The object on which to invoke the method.
433
* @param methodName The name of the method.
435
public static void invokeIfExists(Object obj, String methodName, int newValue) {
437
invoke(obj, methodName, newValue);
438
} catch (NoSuchMethodException e) {
443
* Invokes the specified setter method if it exists.
445
* @param obj The object on which to invoke the method.
446
* @param methodName The name of the method.
448
public static void invokeIfExists(Object obj, String methodName, float newValue) {
450
invoke(obj, methodName, newValue);
451
} catch (NoSuchMethodException e) {
456
* Invokes the specified method if it exists.
458
* @param obj The object on which to invoke the method.
459
* @param methodName The name of the method.
461
public static void invokeIfExists(Object obj, String methodName, boolean newValue) {
463
invoke(obj, methodName, newValue);
464
} catch (NoSuchMethodException e) {
469
* Invokes the specified setter method if it exists.
471
* @param obj The object on which to invoke the method.
472
* @param methodName The name of the method.
474
public static void invokeIfExists(Object obj, String methodName, Class parameterClass, Object newValue) {
476
invoke(obj, methodName, parameterClass, newValue);
477
} catch (NoSuchMethodException e) {
482
* Invokes the specified setter method if it exists.
484
* @param obj The object on which to invoke the method.
485
* @param methodName The name of the method.
487
public static void invokeIfExistsWithEnum(Object obj, String methodName, String enumClassName, String enumValueName) {
489
Class enumClass = Class.forName(enumClassName);
490
Object enumValue = invokeStatic("java.lang.Enum", "valueOf", new Class[] {Class.class, String.class},
491
new Object[] {enumClass, enumValueName}
493
invoke(obj, methodName, enumClass, enumValue);
494
} catch (ClassNotFoundException e) {
497
} catch (NoSuchMethodException e) {
504
* Invokes the specified constructor if it exists.
506
* @param clazz The Class on which to invoke the constructor.
507
* @param types The parameter types of the constructor.
508
* @param values The parameter values of the constructor.
510
public static Object newInstance(Class clazz, Class[] types, Object[] values)
511
throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
512
return clazz.getConstructor(types).newInstance(values);
b'\\ No newline at end of file'