KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > introspection > ReflectionUtils


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.reflect.plugins.introspection;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.jboss.util.Strings;
31
32 /**
33  * ReflectionUtils.
34  *
35  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
36  * @version $Revision: 45764 $
37  */

38 public class ReflectionUtils
39 {
40    /**
41     * Invoke on a method
42     *
43     * @param method the method
44     * @param target the target
45     * @param arguments the arguments
46     * @return the result of the invocation
47     * @throws Throwable for any error
48     */

49    public static Object JavaDoc invoke(Method JavaDoc method, Object JavaDoc target, Object JavaDoc[] arguments) throws Throwable JavaDoc
50    {
51       if (method == null)
52          throw new IllegalArgumentException JavaDoc("Null method");
53       try
54       {
55          return method.invoke(target, arguments);
56       }
57       catch (Throwable JavaDoc t)
58       {
59          throw handleErrors(method.getName(), Strings.defaultToString(target), method.getParameterTypes(), arguments, t);
60       }
61    }
62
63    /**
64     * Create a new instance
65     *
66     * @param clazz the class
67     * @return the constructed object
68     * @throws Throwable for any error
69     */

70    public static Object JavaDoc newInstance(Class JavaDoc clazz) throws Throwable JavaDoc
71    {
72       if (clazz == null)
73          throw new IllegalArgumentException JavaDoc("Null clazz");
74       try
75       {
76          return clazz.newInstance();
77       }
78       catch (Throwable JavaDoc t)
79       {
80          throw handleErrors("new", clazz.getName(), null, null, t);
81       }
82    }
83
84    /**
85     * Create a new instance
86     *
87     * @param className the class name
88     * @param cl the classloader
89     * @return the constructed object
90     * @throws Throwable for any error
91     */

92    public static Object JavaDoc newInstance(String JavaDoc className, ClassLoader JavaDoc cl) throws Throwable JavaDoc
93    {
94       if (className == null)
95          throw new IllegalArgumentException JavaDoc("Null class name");
96       if (cl == null)
97          throw new IllegalArgumentException JavaDoc("Null classloader");
98       Class JavaDoc clazz = cl.loadClass(className);
99       try
100       {
101          return clazz.newInstance();
102       }
103       catch (Throwable JavaDoc t)
104       {
105          throw handleErrors("new", clazz.getName(), null, null, t);
106       }
107    }
108
109    /**
110     * Create a new instance
111     *
112     * @param className the class name
113     * @return the constructed object
114     * @throws Throwable for any error
115     */

116    public static Object JavaDoc newInstance(String JavaDoc className) throws Throwable JavaDoc
117    {
118       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
119       return newInstance(className, cl);
120    }
121
122    /**
123     * Create a new instance
124     *
125     * @param constructor the constructor
126     * @param arguments the arguments
127     * @return the constructed object
128     * @throws Throwable for any error
129     */

130    public static Object JavaDoc newInstance(Constructor JavaDoc constructor, Object JavaDoc[] arguments) throws Throwable JavaDoc
131    {
132       if (constructor == null)
133          throw new IllegalArgumentException JavaDoc("Null constructor");
134       try
135       {
136          return constructor.newInstance(arguments);
137       }
138       catch (Throwable JavaDoc t)
139       {
140          throw handleErrors("new", constructor.getClass().getName(), constructor.getParameterTypes(), arguments, t);
141       }
142    }
143
144    /**
145     * Get a field
146     *
147     * @param field the field
148     * @param target the target
149     * @return null
150     * @throws Throwable for any error
151     */

152    public static Object JavaDoc getField(Field JavaDoc field, Object JavaDoc target) throws Throwable JavaDoc
153    {
154       if (field == null)
155          throw new IllegalArgumentException JavaDoc("Null field");
156       try
157       {
158          return field.get(target);
159       }
160       catch (Throwable JavaDoc t)
161       {
162          throw handleErrors("set", field, target, null, t);
163       }
164    }
165
166    /**
167     * Set a field
168     *
169     * @param field the field
170     * @param target the target
171     * @param value the value
172     * @return null
173     * @throws Throwable for any error
174     */

175    public static Object JavaDoc setField(Field JavaDoc field, Object JavaDoc target, Object JavaDoc value) throws Throwable JavaDoc
176    {
177       if (field == null)
178          throw new IllegalArgumentException JavaDoc("Null field");
179       try
180       {
181          field.set(target, value);
182          return null;
183       }
184       catch (Throwable JavaDoc t)
185       {
186          throw handleErrors("set", field, target, value, t);
187       }
188    }
189    
190    /**
191     * Handle errors
192     *
193     * @param context the context
194     * @param target the target
195     * @param parameters the parameters
196     * @param arguments the arguments
197     * @param t the error
198     * @return never
199     * @throws Throwable always
200     */

201    public static Throwable JavaDoc handleErrors(String JavaDoc context, Object JavaDoc target, Class JavaDoc[] parameters, Object JavaDoc[] arguments, Throwable JavaDoc t) throws Throwable JavaDoc
202    {
203       if (t instanceof IllegalArgumentException JavaDoc)
204       {
205          if (target == null)
206             throw new IllegalArgumentException JavaDoc("Null target for " + context);
207          ArrayList JavaDoc<String JavaDoc> expected = new ArrayList JavaDoc<String JavaDoc>();
208          if (parameters != null)
209          {
210             for (int i = 0; i < parameters.length; ++i)
211                expected.add(parameters[i].getName());
212          }
213          ArrayList JavaDoc<String JavaDoc> actual = new ArrayList JavaDoc<String JavaDoc>();
214          if (arguments != null)
215          {
216             for (int i = 0; i < arguments.length; ++i)
217             {
218                if (arguments[i] == null)
219                   actual.add(null);
220                else
221                   actual.add(arguments[i].getClass().getName());
222             }
223          }
224          throw new IllegalArgumentException JavaDoc("Wrong arguments. " + context + " for target " + target + " expected=" + expected + " actual=" + actual);
225       }
226       else if (t instanceof InvocationTargetException JavaDoc)
227       {
228          throw ((InvocationTargetException JavaDoc) t).getTargetException();
229       }
230       throw t;
231    }
232    
233    /**
234     * Handle errors
235     *
236     * @param context the context
237     * @param field the field
238     * @param target the target
239     * @param value the value
240     * @param t the error
241     * @return never
242     * @throws Throwable always
243     */

244    public static Throwable JavaDoc handleErrors(String JavaDoc context, Field JavaDoc field, Object JavaDoc target, Object JavaDoc value, Throwable JavaDoc t) throws Throwable JavaDoc
245    {
246       if (t instanceof IllegalArgumentException JavaDoc)
247       {
248          String JavaDoc valueType = null;
249          if (value != null)
250             valueType = value.getClass().getName();
251          throw new IllegalArgumentException JavaDoc("Error invoking field " + context + " for target " + target + " field " + field.getName() + " expected=" + field.getType().getName() + " actual=" + valueType);
252       }
253       throw t;
254    }
255 }
256
Popular Tags