~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/contrib/randelshofer/quaqua/util/Methods.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)Methods.java  1.3.1  2006-09-18
 
3
 *
 
4
 * Copyright (c) 2005-2006 Werner Randelshofer
 
5
 * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
 
6
 * All rights reserved.
 
7
 *
 
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.
 
13
 */
 
14
 
 
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.util;
 
16
 
 
17
import java.lang.reflect.*;
 
18
/**
 
19
 * Methods contains convenience methods for method invocations using
 
20
 * java.lang.reflect.
 
21
 *
 
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.
 
28
 */
 
29
public class Methods {
 
30
    /**
 
31
     * Prevent instance creation.
 
32
     */
 
33
    private Methods() {
 
34
    }
 
35
    
 
36
    /**
 
37
     * Invokes the specified accessible parameterless method if it exists.
 
38
     *
 
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
 
43
     * accessible.
 
44
     */
 
45
    public static Object invoke(Object obj, String methodName)
 
46
    throws NoSuchMethodException {
 
47
        try {
 
48
            Method method =  obj.getClass().getMethod(methodName,  new Class[0]);
 
49
            Object result = method.invoke(obj, new Object[0]);
 
50
            return result;
 
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());
 
56
        }
 
57
    }
 
58
    /**
 
59
     * Invokes the specified accessible method with a string parameter if it exists.
 
60
     *
 
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.
 
66
     */
 
67
    public static Object invoke(Object obj, String methodName, String stringParameter)
 
68
    throws NoSuchMethodException {
 
69
        try {
 
70
            Method method =  obj.getClass().getMethod(methodName,  new Class[] { String.class });
 
71
            Object result = method.invoke(obj, new Object[] { stringParameter });
 
72
            return result;
 
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());
 
78
        }
 
79
    }
 
80
    
 
81
    /**
 
82
     * Invokes the specified method if it exists.
 
83
     *
 
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.
 
90
     */
 
91
    public static Object invoke(Object obj, String methodName, Class[] types, Object[] values)
 
92
    throws NoSuchMethodException {
 
93
        try {
 
94
            Method method =  obj.getClass().getMethod(methodName,  types);
 
95
            Object result = method.invoke(obj, values);
 
96
            return result;
 
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());
 
102
        }
 
103
    }
 
104
    /**
 
105
     * Invokes the specified accessible parameterless method if it exists.
 
106
     *
 
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.
 
111
     */
 
112
    public static Object invokeStatic(Class clazz, String methodName)
 
113
    throws NoSuchMethodException {
 
114
        try {
 
115
            Method method =  clazz.getMethod(methodName,  new Class[0]);
 
116
            Object result = method.invoke(null, new Object[0]);
 
117
            return result;
 
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());
 
123
        }
 
124
    }
 
125
    /**
 
126
     * Invokes the specified static parameterless method if it exists.
 
127
     *
 
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.
 
132
     */
 
133
    public static Object invokeStatic(String clazz, String methodName)
 
134
    throws NoSuchMethodException {
 
135
        try {
 
136
            return invokeStatic(Class.forName(clazz), methodName);
 
137
        } catch (ClassNotFoundException e) {
 
138
            throw new NoSuchMethodException("class "+clazz+" not found");
 
139
        }
 
140
    }
 
141
    /**
 
142
     * Invokes the specified static method if it exists.
 
143
     *
 
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.
 
150
     */
 
151
    public static Object invokeStatic(Class clazz, String methodName,
 
152
            Class[] types, Object[] values)
 
153
            throws NoSuchMethodException {
 
154
        try {
 
155
            Method method =  clazz.getMethod(methodName,  types);
 
156
            Object result = method.invoke(null, values);
 
157
            return result;
 
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());
 
163
        }
 
164
    }
 
165
    /**
 
166
     * Invokes the specified static method if it exists.
 
167
     *
 
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.
 
174
     */
 
175
    public static Object invokeStatic(String clazz, String methodName,
 
176
            Class[] types, Object[] values)
 
177
            throws NoSuchMethodException {
 
178
        try {
 
179
            return invokeStatic(Class.forName(clazz), methodName, types, values);
 
180
        } catch (ClassNotFoundException e) {
 
181
            throw new NoSuchMethodException("class "+clazz+" not found");
 
182
        }
 
183
    }
 
184
    /**
 
185
     * Invokes the specified static method if it exists.
 
186
     *
 
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.
 
193
     */
 
194
    public static Object invokeStatic(String clazz, String methodName,
 
195
            Class type, Object value)
 
196
            throws NoSuchMethodException {
 
197
        try {
 
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");
 
201
        }
 
202
    }
 
203
    /**
 
204
     * Invokes the specified static method if it exists.
 
205
     *
 
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.
 
213
     */
 
214
    public static Object invokeStatic(String clazz, String methodName,
 
215
            Class[] types, Object[] values, Object defaultValue) {
 
216
        try {
 
217
            return invokeStatic(Class.forName(clazz), methodName, types, values);
 
218
        } catch (ClassNotFoundException e) {
 
219
            return defaultValue;
 
220
        } catch (NoSuchMethodException e) {
 
221
            return defaultValue;
 
222
        }
 
223
    }
 
224
    /**
 
225
     * Invokes the specified static method if it exists.
 
226
     *
 
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.
 
233
     */
 
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});
 
237
    }
 
238
    
 
239
    /**
 
240
     * Invokes the specified getter method if it exists.
 
241
     *
 
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.
 
246
     */
 
247
    public static int invokeGetter(Object obj, String methodName, int defaultValue) {
 
248
        try {
 
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) {
 
253
            return defaultValue;
 
254
        } catch (IllegalAccessException e) {
 
255
            return defaultValue;
 
256
        } catch (InvocationTargetException e) {
 
257
            return defaultValue;
 
258
        }
 
259
    }
 
260
    /**
 
261
     * Invokes the specified getter method if it exists.
 
262
     *
 
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.
 
267
     */
 
268
    public static long invokeGetter(Object obj, String methodName, long defaultValue) {
 
269
        try {
 
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) {
 
274
            return defaultValue;
 
275
        } catch (IllegalAccessException e) {
 
276
            return defaultValue;
 
277
        } catch (InvocationTargetException e) {
 
278
            return defaultValue;
 
279
        }
 
280
    }
 
281
    /**
 
282
     * Invokes the specified getter method if it exists.
 
283
     *
 
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.
 
288
     */
 
289
    public static boolean invokeGetter(Object obj, String methodName, boolean defaultValue) {
 
290
        try {
 
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) {
 
295
            return defaultValue;
 
296
        } catch (IllegalAccessException e) {
 
297
            return defaultValue;
 
298
        } catch (InvocationTargetException e) {
 
299
            return defaultValue;
 
300
        }
 
301
    }
 
302
    /**
 
303
     * Invokes the specified getter method if it exists.
 
304
     *
 
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.
 
309
     */
 
310
    public static Object invokeGetter(Object obj, String methodName, Object defaultValue) {
 
311
        try {
 
312
            Method method =  obj.getClass().getMethod(methodName,  new Class[0]);
 
313
            Object result = method.invoke(obj, new Object[0]);
 
314
            return result;
 
315
        } catch (NoSuchMethodException e) {
 
316
            return defaultValue;
 
317
        } catch (IllegalAccessException e) {
 
318
            return defaultValue;
 
319
        } catch (InvocationTargetException e) {
 
320
            return defaultValue;
 
321
        }
 
322
    }
 
323
    /**
 
324
     * Invokes the specified getter method if it exists.
 
325
     *
 
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.
 
330
     */
 
331
    public static boolean invokeStaticGetter(Class clazz, String methodName, boolean defaultValue) {
 
332
        try {
 
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) {
 
337
            return defaultValue;
 
338
        } catch (IllegalAccessException e) {
 
339
            return defaultValue;
 
340
        } catch (InvocationTargetException e) {
 
341
            return defaultValue;
 
342
        }
 
343
    }
 
344
    /**
 
345
     * Invokes the specified setter method if it exists.
 
346
     *
 
347
     * @param obj The object on which to invoke the method.
 
348
     * @param methodName The name of the method.
 
349
     */
 
350
    public static Object invoke(Object obj, String methodName, boolean newValue)
 
351
    throws NoSuchMethodException {
 
352
        try {
 
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());
 
360
        }
 
361
    }
 
362
    /**
 
363
     * Invokes the specified method if it exists.
 
364
     *
 
365
     * @param obj The object on which to invoke the method.
 
366
     * @param methodName The name of the method.
 
367
     */
 
368
    public static Object invoke(Object obj, String methodName, int newValue)
 
369
    throws NoSuchMethodException {
 
370
        try {
 
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());
 
378
        }
 
379
    }
 
380
    /**
 
381
     * Invokes the specified setter method if it exists.
 
382
     *
 
383
     * @param obj The object on which to invoke the method.
 
384
     * @param methodName The name of the method.
 
385
     */
 
386
    public static Object invoke(Object obj, String methodName, float newValue)
 
387
    throws NoSuchMethodException {
 
388
        try {
 
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());
 
396
        }
 
397
    }
 
398
    /**
 
399
     * Invokes the specified setter method if it exists.
 
400
     *
 
401
     * @param obj The object on which to invoke the method.
 
402
     * @param methodName The name of the method.
 
403
     */
 
404
    public static Object invoke(Object obj, String methodName, Class clazz, Object newValue)
 
405
    throws NoSuchMethodException {
 
406
        try {
 
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());
 
414
        }
 
415
    }
 
416
    /**
 
417
     * Invokes the specified setter method if it exists.
 
418
     *
 
419
     * @param obj The object on which to invoke the method.
 
420
     * @param methodName The name of the method.
 
421
     */
 
422
    public static void invokeIfExists(Object obj, String methodName) {
 
423
        try {
 
424
            invoke(obj, methodName);
 
425
        } catch (NoSuchMethodException e) {
 
426
            // ignore
 
427
        }
 
428
    }
 
429
    /**
 
430
     * Invokes the specified setter method if it exists.
 
431
     *
 
432
     * @param obj The object on which to invoke the method.
 
433
     * @param methodName The name of the method.
 
434
     */
 
435
    public static void invokeIfExists(Object obj, String methodName, int newValue) {
 
436
        try {
 
437
            invoke(obj, methodName, newValue);
 
438
        } catch (NoSuchMethodException e) {
 
439
            // ignore
 
440
        }
 
441
    }
 
442
    /**
 
443
     * Invokes the specified setter method if it exists.
 
444
     *
 
445
     * @param obj The object on which to invoke the method.
 
446
     * @param methodName The name of the method.
 
447
     */
 
448
    public static void invokeIfExists(Object obj, String methodName, float newValue) {
 
449
        try {
 
450
            invoke(obj, methodName, newValue);
 
451
        } catch (NoSuchMethodException e) {
 
452
            // ignore
 
453
        }
 
454
    }
 
455
    /**
 
456
     * Invokes the specified method if it exists.
 
457
     *
 
458
     * @param obj The object on which to invoke the method.
 
459
     * @param methodName The name of the method.
 
460
     */
 
461
    public static void invokeIfExists(Object obj, String methodName, boolean newValue) {
 
462
        try {
 
463
            invoke(obj, methodName, newValue);
 
464
        } catch (NoSuchMethodException e) {
 
465
            // ignore
 
466
        }
 
467
    }
 
468
    /**
 
469
     * Invokes the specified setter method if it exists.
 
470
     *
 
471
     * @param obj The object on which to invoke the method.
 
472
     * @param methodName The name of the method.
 
473
     */
 
474
    public static void invokeIfExists(Object obj, String methodName, Class parameterClass, Object newValue) {
 
475
        try {
 
476
            invoke(obj, methodName, parameterClass, newValue);
 
477
        } catch (NoSuchMethodException e) {
 
478
            // ignore
 
479
        }
 
480
    }
 
481
    /**
 
482
     * Invokes the specified setter method if it exists.
 
483
     *
 
484
     * @param obj The object on which to invoke the method.
 
485
     * @param methodName The name of the method.
 
486
     */
 
487
    public static void invokeIfExistsWithEnum(Object obj, String methodName, String enumClassName, String enumValueName) {
 
488
        try {
 
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}
 
492
            );
 
493
            invoke(obj, methodName, enumClass, enumValue);
 
494
        } catch (ClassNotFoundException e) {
 
495
            // ignore
 
496
            e.printStackTrace();
 
497
        } catch (NoSuchMethodException e) {
 
498
            // ignore
 
499
            e.printStackTrace();
 
500
        }
 
501
    }
 
502
    
 
503
    /**
 
504
     * Invokes the specified constructor if it exists.
 
505
     *
 
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.
 
509
     */
 
510
    public static Object newInstance(Class clazz, Class[] types, Object[] values)
 
511
    throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
 
512
        return clazz.getConstructor(types).newInstance(values);
 
513
    };
 
514
    
 
515
}
 
 
b'\\ No newline at end of file'