1 22 package org.jboss.reflect.plugins.introspection; 23 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.Field ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.util.ArrayList ; 29 30 import org.jboss.util.Strings; 31 32 38 public class ReflectionUtils 39 { 40 49 public static Object invoke(Method method, Object target, Object [] arguments) throws Throwable 50 { 51 if (method == null) 52 throw new IllegalArgumentException ("Null method"); 53 try 54 { 55 return method.invoke(target, arguments); 56 } 57 catch (Throwable t) 58 { 59 throw handleErrors(method.getName(), Strings.defaultToString(target), method.getParameterTypes(), arguments, t); 60 } 61 } 62 63 70 public static Object newInstance(Class clazz) throws Throwable 71 { 72 if (clazz == null) 73 throw new IllegalArgumentException ("Null clazz"); 74 try 75 { 76 return clazz.newInstance(); 77 } 78 catch (Throwable t) 79 { 80 throw handleErrors("new", clazz.getName(), null, null, t); 81 } 82 } 83 84 92 public static Object newInstance(String className, ClassLoader cl) throws Throwable 93 { 94 if (className == null) 95 throw new IllegalArgumentException ("Null class name"); 96 if (cl == null) 97 throw new IllegalArgumentException ("Null classloader"); 98 Class clazz = cl.loadClass(className); 99 try 100 { 101 return clazz.newInstance(); 102 } 103 catch (Throwable t) 104 { 105 throw handleErrors("new", clazz.getName(), null, null, t); 106 } 107 } 108 109 116 public static Object newInstance(String className) throws Throwable 117 { 118 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 119 return newInstance(className, cl); 120 } 121 122 130 public static Object newInstance(Constructor constructor, Object [] arguments) throws Throwable 131 { 132 if (constructor == null) 133 throw new IllegalArgumentException ("Null constructor"); 134 try 135 { 136 return constructor.newInstance(arguments); 137 } 138 catch (Throwable t) 139 { 140 throw handleErrors("new", constructor.getClass().getName(), constructor.getParameterTypes(), arguments, t); 141 } 142 } 143 144 152 public static Object getField(Field field, Object target) throws Throwable 153 { 154 if (field == null) 155 throw new IllegalArgumentException ("Null field"); 156 try 157 { 158 return field.get(target); 159 } 160 catch (Throwable t) 161 { 162 throw handleErrors("set", field, target, null, t); 163 } 164 } 165 166 175 public static Object setField(Field field, Object target, Object value) throws Throwable 176 { 177 if (field == null) 178 throw new IllegalArgumentException ("Null field"); 179 try 180 { 181 field.set(target, value); 182 return null; 183 } 184 catch (Throwable t) 185 { 186 throw handleErrors("set", field, target, value, t); 187 } 188 } 189 190 201 public static Throwable handleErrors(String context, Object target, Class [] parameters, Object [] arguments, Throwable t) throws Throwable 202 { 203 if (t instanceof IllegalArgumentException ) 204 { 205 if (target == null) 206 throw new IllegalArgumentException ("Null target for " + context); 207 ArrayList <String > expected = new ArrayList <String >(); 208 if (parameters != null) 209 { 210 for (int i = 0; i < parameters.length; ++i) 211 expected.add(parameters[i].getName()); 212 } 213 ArrayList <String > actual = new ArrayList <String >(); 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 ("Wrong arguments. " + context + " for target " + target + " expected=" + expected + " actual=" + actual); 225 } 226 else if (t instanceof InvocationTargetException ) 227 { 228 throw ((InvocationTargetException ) t).getTargetException(); 229 } 230 throw t; 231 } 232 233 244 public static Throwable handleErrors(String context, Field field, Object target, Object value, Throwable t) throws Throwable 245 { 246 if (t instanceof IllegalArgumentException ) 247 { 248 String valueType = null; 249 if (value != null) 250 valueType = value.getClass().getName(); 251 throw new IllegalArgumentException ("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 |