001: /*
002: * @(#)Methods.java 1.3.1 2006-09-18
003: *
004: * Copyright (c) 2005-2006 Werner Randelshofer
005: * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information of
009: * Werner Randelshofer. ("Confidential Information"). You shall not
010: * disclose such Confidential Information and shall use it only in
011: * accordance with the terms of the license agreement you entered into
012: * with Werner Randelshofer.
013: */
014:
015: package contrib.ch.randelshofer.quaqua.util;
016:
017: import java.lang.reflect.*;
018:
019: /**
020: * Methods contains convenience methods for method invocations using
021: * java.lang.reflect.
022: *
023: * @author Werner Randelshofer
024: * @version 1.3.1 2006-09-18 Fixed javadoc warnings.
025: * <br>1.2 2006-08-20 Additional invokeIfExists method added.
026: * <br>1.2 2006-05-07 Added invokeNew method.
027: * <br>1.1 2006-02-18 Added more convenience methods.
028: * <br>1.0 September 24, 2005 Created.
029: */
030: public class Methods {
031: /**
032: * Prevent instance creation.
033: */
034: private Methods() {
035: }
036:
037: /**
038: * Invokes the specified accessible parameterless method if it exists.
039: *
040: * @param obj The object on which to invoke the method.
041: * @param methodName The name of the method.
042: * @return The return value of the method.
043: * @return NoSuchMethodException if the method does not exist or is not
044: * accessible.
045: */
046: public static Object invoke(Object obj, String methodName)
047: throws NoSuchMethodException {
048: try {
049: Method method = obj.getClass().getMethod(methodName,
050: new Class[0]);
051: Object result = method.invoke(obj, new Object[0]);
052: return result;
053: } catch (IllegalAccessException e) {
054: throw new NoSuchMethodException(methodName
055: + " is not accessible");
056: } catch (InvocationTargetException e) {
057: // The method is not supposed to throw exceptions
058: throw new InternalError(e.getMessage());
059: }
060: }
061:
062: /**
063: * Invokes the specified accessible method with a string parameter if it exists.
064: *
065: * @param obj The object on which to invoke the method.
066: * @param methodName The name of the method.
067: * @param stringParameter The String parameter
068: * @return The return value of the method or METHOD_NOT_FOUND.
069: * @return NoSuchMethodException if the method does not exist or is not accessible.
070: */
071: public static Object invoke(Object obj, String methodName,
072: String stringParameter) throws NoSuchMethodException {
073: try {
074: Method method = obj.getClass().getMethod(methodName,
075: new Class[] { String.class });
076: Object result = method.invoke(obj,
077: new Object[] { stringParameter });
078: return result;
079: } catch (IllegalAccessException e) {
080: throw new NoSuchMethodException(methodName
081: + " is not accessible");
082: } catch (InvocationTargetException e) {
083: // The method is not supposed to throw exceptions
084: throw new InternalError(e.getMessage());
085: }
086: }
087:
088: /**
089: * Invokes the specified method if it exists.
090: *
091: * @param obj The object on which to invoke the method.
092: * @param methodName The name of the method.
093: * @param types The parameter types.
094: * @param values The parameter values.
095: * @return The return value of the method.
096: * @return NoSuchMethodException if the method does not exist or is not accessible.
097: */
098: public static Object invoke(Object obj, String methodName,
099: Class[] types, Object[] values)
100: throws NoSuchMethodException {
101: try {
102: Method method = obj.getClass().getMethod(methodName, types);
103: Object result = method.invoke(obj, values);
104: return result;
105: } catch (IllegalAccessException e) {
106: throw new NoSuchMethodException(methodName
107: + " is not accessible");
108: } catch (InvocationTargetException e) {
109: // The method is not supposed to throw exceptions
110: throw new InternalError(e.getMessage());
111: }
112: }
113:
114: /**
115: * Invokes the specified accessible parameterless method if it exists.
116: *
117: * @param clazz The class on which to invoke the method.
118: * @param methodName The name of the method.
119: * @return The return value of the method or METHOD_NOT_FOUND.
120: * @return NoSuchMethodException if the method does not exist or is not accessible.
121: */
122: public static Object invokeStatic(Class clazz, String methodName)
123: throws NoSuchMethodException {
124: try {
125: Method method = clazz.getMethod(methodName, new Class[0]);
126: Object result = method.invoke(null, new Object[0]);
127: return result;
128: } catch (IllegalAccessException e) {
129: throw new NoSuchMethodException(methodName
130: + " is not accessible");
131: } catch (InvocationTargetException e) {
132: // The method is not supposed to throw exceptions
133: throw new InternalError(e.getMessage());
134: }
135: }
136:
137: /**
138: * Invokes the specified static parameterless method if it exists.
139: *
140: * @param clazz The class on which to invoke the method.
141: * @param methodName The name of the method.
142: * @return The return value of the method.
143: * @return NoSuchMethodException if the method does not exist or is not accessible.
144: */
145: public static Object invokeStatic(String clazz, String methodName)
146: throws NoSuchMethodException {
147: try {
148: return invokeStatic(Class.forName(clazz), methodName);
149: } catch (ClassNotFoundException e) {
150: throw new NoSuchMethodException("class " + clazz
151: + " not found");
152: }
153: }
154:
155: /**
156: * Invokes the specified static method if it exists.
157: *
158: * @param clazz The class on which to invoke the method.
159: * @param methodName The name of the method.
160: * @param types The parameter types.
161: * @param values The parameter values.
162: * @return The return value of the method.
163: * @return NoSuchMethodException if the method does not exist or is not accessible.
164: */
165: public static Object invokeStatic(Class clazz, String methodName,
166: Class[] types, Object[] values)
167: throws NoSuchMethodException {
168: try {
169: Method method = clazz.getMethod(methodName, types);
170: Object result = method.invoke(null, values);
171: return result;
172: } catch (IllegalAccessException e) {
173: throw new NoSuchMethodException(methodName
174: + " is not accessible");
175: } catch (InvocationTargetException e) {
176: // The method is not supposed to throw exceptions
177: throw new InternalError(e.getMessage());
178: }
179: }
180:
181: /**
182: * Invokes the specified static method if it exists.
183: *
184: * @param clazz The class on which to invoke the method.
185: * @param methodName The name of the method.
186: * @param types The parameter types.
187: * @param values The parameter values.
188: * @return The return value of the method.
189: * @return NoSuchMethodException if the method does not exist or is not accessible.
190: */
191: public static Object invokeStatic(String clazz, String methodName,
192: Class[] types, Object[] values)
193: throws NoSuchMethodException {
194: try {
195: return invokeStatic(Class.forName(clazz), methodName,
196: types, values);
197: } catch (ClassNotFoundException e) {
198: throw new NoSuchMethodException("class " + clazz
199: + " not found");
200: }
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 type The parameter types.
209: * @param value The parameter values.
210: * @return The return value of the method.
211: * @return NoSuchMethodException if the method does not exist or is not accessible.
212: */
213: public static Object invokeStatic(String clazz, String methodName,
214: Class type, Object value) throws NoSuchMethodException {
215: try {
216: return invokeStatic(Class.forName(clazz), methodName,
217: new Class[] { type }, new Object[] { value });
218: } catch (ClassNotFoundException e) {
219: throw new NoSuchMethodException("class " + clazz
220: + " not found");
221: }
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 types The parameter types.
230: * @param values The parameter values.
231: * @param defaultValue The default value.
232: * @return The return value of the method or the default value if the method
233: * does not exist or is not accessible.
234: */
235: public static Object invokeStatic(String clazz, String methodName,
236: Class[] types, Object[] values, Object defaultValue) {
237: try {
238: return invokeStatic(Class.forName(clazz), methodName,
239: types, values);
240: } catch (ClassNotFoundException e) {
241: return defaultValue;
242: } catch (NoSuchMethodException e) {
243: return defaultValue;
244: }
245: }
246:
247: /**
248: * Invokes the specified static method if it exists.
249: *
250: * @param clazz The class on which to invoke the method.
251: * @param methodName The name of the method.
252: * @param type The parameter type.
253: * @param value The parameter value.
254: * @return The return value of the method or the default value if the method
255: * does not exist or is not accessible.
256: */
257: public static Object invokeStatic(Class clazz, String methodName,
258: Class type, Object value) throws NoSuchMethodException {
259: return invokeStatic(clazz, methodName, new Class[] { type },
260: new Object[] { value });
261: }
262:
263: /**
264: * Invokes the specified getter method if it exists.
265: *
266: * @param obj The object on which to invoke the method.
267: * @param methodName The name of the method.
268: * @param defaultValue This value is returned, if the method does not exist.
269: * @return The value returned by the getter method or the default value.
270: */
271: public static int invokeGetter(Object obj, String methodName,
272: int defaultValue) {
273: try {
274: Method method = obj.getClass().getMethod(methodName,
275: new Class[0]);
276: Object result = method.invoke(obj, new Object[0]);
277: return ((Integer) result).intValue();
278: } catch (NoSuchMethodException e) {
279: return defaultValue;
280: } catch (IllegalAccessException e) {
281: return defaultValue;
282: } catch (InvocationTargetException e) {
283: return defaultValue;
284: }
285: }
286:
287: /**
288: * Invokes the specified getter method if it exists.
289: *
290: * @param obj The object on which to invoke the method.
291: * @param methodName The name of the method.
292: * @param defaultValue This value is returned, if the method does not exist.
293: * @return The value returned by the getter method or the default value.
294: */
295: public static long invokeGetter(Object obj, String methodName,
296: long defaultValue) {
297: try {
298: Method method = obj.getClass().getMethod(methodName,
299: new Class[0]);
300: Object result = method.invoke(obj, new Object[0]);
301: return ((Long) result).longValue();
302: } catch (NoSuchMethodException e) {
303: return defaultValue;
304: } catch (IllegalAccessException e) {
305: return defaultValue;
306: } catch (InvocationTargetException e) {
307: return defaultValue;
308: }
309: }
310:
311: /**
312: * Invokes the specified getter method if it exists.
313: *
314: * @param obj The object on which to invoke the method.
315: * @param methodName The name of the method.
316: * @param defaultValue This value is returned, if the method does not exist.
317: * @return The value returned by the getter method or the default value.
318: */
319: public static boolean invokeGetter(Object obj, String methodName,
320: boolean defaultValue) {
321: try {
322: Method method = obj.getClass().getMethod(methodName,
323: new Class[0]);
324: Object result = method.invoke(obj, new Object[0]);
325: return ((Boolean) result).booleanValue();
326: } catch (NoSuchMethodException e) {
327: return defaultValue;
328: } catch (IllegalAccessException e) {
329: return defaultValue;
330: } catch (InvocationTargetException e) {
331: return defaultValue;
332: }
333: }
334:
335: /**
336: * Invokes the specified getter method if it exists.
337: *
338: * @param obj The object on which to invoke the method.
339: * @param methodName The name of the method.
340: * @param defaultValue This value is returned, if the method does not exist.
341: * @return The value returned by the getter method or the default value.
342: */
343: public static Object invokeGetter(Object obj, String methodName,
344: Object defaultValue) {
345: try {
346: Method method = obj.getClass().getMethod(methodName,
347: new Class[0]);
348: Object result = method.invoke(obj, new Object[0]);
349: return result;
350: } catch (NoSuchMethodException e) {
351: return defaultValue;
352: } catch (IllegalAccessException e) {
353: return defaultValue;
354: } catch (InvocationTargetException e) {
355: return defaultValue;
356: }
357: }
358:
359: /**
360: * Invokes the specified getter method if it exists.
361: *
362: * @param clazz The class on which to invoke the method.
363: * @param methodName The name of the method.
364: * @param defaultValue This value is returned, if the method does not exist.
365: * @return The value returned by the getter method or the default value.
366: */
367: public static boolean invokeStaticGetter(Class clazz,
368: String methodName, boolean defaultValue) {
369: try {
370: Method method = clazz.getMethod(methodName, new Class[0]);
371: Object result = method.invoke(null, new Object[0]);
372: return ((Boolean) result).booleanValue();
373: } catch (NoSuchMethodException e) {
374: return defaultValue;
375: } catch (IllegalAccessException e) {
376: return defaultValue;
377: } catch (InvocationTargetException e) {
378: return defaultValue;
379: }
380: }
381:
382: /**
383: * Invokes the specified setter method if it exists.
384: *
385: * @param obj The object on which to invoke the method.
386: * @param methodName The name of the method.
387: */
388: public static Object invoke(Object obj, String methodName,
389: boolean newValue) throws NoSuchMethodException {
390: try {
391: Method method = obj.getClass().getMethod(methodName,
392: new Class[] { Boolean.TYPE });
393: return method.invoke(obj, new Object[] { new Boolean(
394: newValue) });
395: } catch (IllegalAccessException e) {
396: throw new NoSuchMethodException(methodName
397: + " is not accessible");
398: } catch (InvocationTargetException e) {
399: // The method is not supposed to throw exceptions
400: throw new InternalError(e.getMessage());
401: }
402: }
403:
404: /**
405: * Invokes the specified method if it exists.
406: *
407: * @param obj The object on which to invoke the method.
408: * @param methodName The name of the method.
409: */
410: public static Object invoke(Object obj, String methodName,
411: int newValue) throws NoSuchMethodException {
412: try {
413: Method method = obj.getClass().getMethod(methodName,
414: new Class[] { Integer.TYPE });
415: return method.invoke(obj, new Object[] { new Integer(
416: newValue) });
417: } catch (IllegalAccessException e) {
418: throw new NoSuchMethodException(methodName
419: + " is not accessible");
420: } catch (InvocationTargetException e) {
421: // The method is not supposed to throw exceptions
422: throw new InternalError(e.getMessage());
423: }
424: }
425:
426: /**
427: * Invokes the specified setter method if it exists.
428: *
429: * @param obj The object on which to invoke the method.
430: * @param methodName The name of the method.
431: */
432: public static Object invoke(Object obj, String methodName,
433: float newValue) throws NoSuchMethodException {
434: try {
435: Method method = obj.getClass().getMethod(methodName,
436: new Class[] { Float.TYPE });
437: return method.invoke(obj,
438: new Object[] { new Float(newValue) });
439: } catch (IllegalAccessException e) {
440: throw new NoSuchMethodException(methodName
441: + " is not accessible");
442: } catch (InvocationTargetException e) {
443: // The method is not supposed to throw exceptions
444: throw new InternalError(e.getMessage());
445: }
446: }
447:
448: /**
449: * Invokes the specified setter method if it exists.
450: *
451: * @param obj The object on which to invoke the method.
452: * @param methodName The name of the method.
453: */
454: public static Object invoke(Object obj, String methodName,
455: Class clazz, Object newValue) throws NoSuchMethodException {
456: try {
457: Method method = obj.getClass().getMethod(methodName,
458: new Class[] { clazz });
459: return method.invoke(obj, new Object[] { newValue });
460: } catch (IllegalAccessException e) {
461: throw new NoSuchMethodException(methodName
462: + " is not accessible");
463: } catch (InvocationTargetException e) {
464: // The method is not supposed to throw exceptions
465: throw new InternalError(e.getMessage());
466: }
467: }
468:
469: /**
470: * Invokes the specified setter method if it exists.
471: *
472: * @param obj The object on which to invoke the method.
473: * @param methodName The name of the method.
474: */
475: public static void invokeIfExists(Object obj, String methodName) {
476: try {
477: invoke(obj, methodName);
478: } catch (NoSuchMethodException e) {
479: // ignore
480: }
481: }
482:
483: /**
484: * Invokes the specified setter method if it exists.
485: *
486: * @param obj The object on which to invoke the method.
487: * @param methodName The name of the method.
488: */
489: public static void invokeIfExists(Object obj, String methodName,
490: int newValue) {
491: try {
492: invoke(obj, methodName, newValue);
493: } catch (NoSuchMethodException e) {
494: // ignore
495: }
496: }
497:
498: /**
499: * Invokes the specified setter method if it exists.
500: *
501: * @param obj The object on which to invoke the method.
502: * @param methodName The name of the method.
503: */
504: public static void invokeIfExists(Object obj, String methodName,
505: float newValue) {
506: try {
507: invoke(obj, methodName, newValue);
508: } catch (NoSuchMethodException e) {
509: // ignore
510: }
511: }
512:
513: /**
514: * Invokes the specified method if it exists.
515: *
516: * @param obj The object on which to invoke the method.
517: * @param methodName The name of the method.
518: */
519: public static void invokeIfExists(Object obj, String methodName,
520: boolean newValue) {
521: try {
522: invoke(obj, methodName, newValue);
523: } catch (NoSuchMethodException e) {
524: // ignore
525: }
526: }
527:
528: /**
529: * Invokes the specified setter method if it exists.
530: *
531: * @param obj The object on which to invoke the method.
532: * @param methodName The name of the method.
533: */
534: public static void invokeIfExists(Object obj, String methodName,
535: Class parameterClass, Object newValue) {
536: try {
537: invoke(obj, methodName, parameterClass, newValue);
538: } catch (NoSuchMethodException e) {
539: // ignore
540: }
541: }
542:
543: /**
544: * Invokes the specified setter method if it exists.
545: *
546: * @param obj The object on which to invoke the method.
547: * @param methodName The name of the method.
548: */
549: public static void invokeIfExistsWithEnum(Object obj,
550: String methodName, String enumClassName,
551: String enumValueName) {
552: try {
553: Class enumClass = Class.forName(enumClassName);
554: Object enumValue = invokeStatic("java.lang.Enum",
555: "valueOf",
556: new Class[] { Class.class, String.class },
557: new Object[] { enumClass, enumValueName });
558: invoke(obj, methodName, enumClass, enumValue);
559: } catch (ClassNotFoundException e) {
560: // ignore
561: e.printStackTrace();
562: } catch (NoSuchMethodException e) {
563: // ignore
564: e.printStackTrace();
565: }
566: }
567:
568: /**
569: * Invokes the specified constructor if it exists.
570: *
571: * @param clazz The Class on which to invoke the constructor.
572: * @param types The parameter types of the constructor.
573: * @param values The parameter values of the constructor.
574: */
575: public static Object newInstance(Class clazz, Class[] types,
576: Object[] values) throws NoSuchMethodException,
577: InstantiationException, IllegalAccessException,
578: InvocationTargetException {
579: return clazz.getConstructor(types).newInstance(values);
580: };
581:
582: }
|