1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.reflect;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.util.Iterator;
25 import java.util.List;
26
27 /***
28 * @author carrd
29 */
30 public class ReflectionUtil {
31 /***
32 * Attempts to create an instance of a class based on its name and a
33 * {@link List} of {@link ParamArgPair}s.
34 *
35 * @param className the name of a class
36 * @param sigs a list of {@link ParamArgPair}s to try in order
37 * @return an instance of the class
38 * @throws ReflectionException if an instance could not be obtained
39 */
40 public static Object instantiate(String className, List sigs)
41 throws ReflectionException {
42 try {
43 return instantiate(Class.forName(className), sigs);
44 } catch (ClassNotFoundException cnfe) {
45 throw new ReflectionException("Class not found: " + className, cnfe);
46 }
47 }
48
49 /***
50 * Attempts to create an instance of a {@link Class} given a {@link List} of
51 * {@link ParamArgPair}s.
52 *
53 * @param clazz a {@link Class} object
54 * @param sigs a list of {@link ParamArgPair}s to try in order
55 * @return an instance of the class
56 * @throws ReflectionException if an instance could not be obtained
57 */
58 public static Object instantiate(Class clazz, List sigs)
59 throws ReflectionException {
60 Throwable cause = null;
61 for (Iterator it = sigs.iterator(); it.hasNext();) {
62 ParamArgPair sig = (ParamArgPair) it.next();
63 try {
64 Constructor constructor = clazz.getConstructor(sig.getParameterTypes());
65 return constructor.newInstance(sig.getArguments());
66 } catch (NoSuchMethodException nsme) {
67 cause = nsme; //Ignore and try the next sig
68 } catch (IllegalAccessException iae) {
69 cause = iae; //Ignore and try the next sig
70 } catch (InstantiationException ie) {
71 cause = ie; //Ignore and try the next sig
72 } catch (InvocationTargetException ite) {
73 cause = ite; //Ignore and try the next sig
74 }
75 }
76 throw new ReflectionException(
77 "Error instantiating " + clazz.getName(),
78 cause);
79 }
80
81 /***
82 * Attempts to invoke a named method on an arbitrary object, given a
83 * {@link List} of {@link ParamArgPair}s.
84 *
85 * @param obj the object to invoke the method on
86 * @param methodName the name of the method to invoke
87 * @param sigs the {@link List} of {@link ParamArgPair}s
88 * @return the return value of the method, or null if none
89 * @throws ReflectionException if the method cannot be called
90 */
91 public static Object invokeMethod(Object obj, String methodName, List sigs)
92 throws ReflectionException {
93 Throwable cause = null;
94 for (Iterator it = sigs.iterator(); it.hasNext();) {
95 ParamArgPair sig = (ParamArgPair) it.next();
96 try {
97 Method method =
98 obj.getClass().getMethod(methodName, sig.getParameterTypes());
99 return method.invoke(obj, sig.getArguments());
100 } catch (NoSuchMethodException nsme) {
101 cause = nsme; //Ignore and try the next sig
102 } catch (IllegalAccessException iae) {
103 cause = iae; //Ignore and try the next sig
104 } catch (InvocationTargetException ite) {
105 cause = ite; //Ignore and try the next sig
106 }
107 }
108 throw new ReflectionException(
109 "Error invoking " + obj.getClass().getName() + "." + methodName,
110 cause);
111 }
112 }
This page was automatically generated by Maven