1 54 55 package junitx.util; 56 57 import java.lang.reflect.Field ; 58 import java.lang.reflect.InvocationTargetException ; 59 import java.lang.reflect.Method ; 60 61 80 public class PrivateAccessor { 81 82 private PrivateAccessor() { 83 } 84 85 96 public static Object getField(Object object, 97 String name) 98 throws NoSuchFieldException { 99 if (object == null) { 100 throw new IllegalArgumentException ("Invalid null object argument"); 101 } 102 for (Class cls = object.getClass(); 103 cls != null; 104 cls = cls.getSuperclass()) { 105 try { 106 Field field = cls.getDeclaredField(name); 107 field.setAccessible(true); 108 return field.get(object); 109 } catch (Exception ex) { 110 112 ; 113 } 114 } 115 throw new NoSuchFieldException ("Could get value for field " + 116 object.getClass().getName() + "." + name); 117 } 118 119 130 public static Object getField(Class cls, 131 String name) 132 throws NoSuchFieldException { 133 if (cls == null) { 134 throw new IllegalArgumentException ("Invalid null cls argument"); 135 } 136 Class base = cls; 137 while (base != null) { 138 try { 139 Field field = base.getDeclaredField(name); 140 field.setAccessible(true); 141 return field.get(base); 142 } catch (Exception ex) { 143 145 ; 146 } 147 base = base.getSuperclass(); 148 } 149 throw new NoSuchFieldException ("Could get value for static field " + 150 cls.getName() + "." + name); 151 } 152 153 164 public static void setField(Object object, 165 String name, 166 Object value) 167 throws NoSuchFieldException { 168 if (object == null) { 169 throw new IllegalArgumentException ("Invalid null object argument"); 170 } 171 for (Class cls = object.getClass(); 172 cls != null; 173 cls = cls.getSuperclass()) { 174 try { 175 Field field = cls.getDeclaredField(name); 176 field.setAccessible(true); 177 field.set(object, value); 178 return; 179 } catch (Exception ex) { 180 182 ; 183 } 184 } 185 throw new NoSuchFieldException ("Could set value for field " + 186 object.getClass().getName() + "." + name); 187 } 188 189 200 public static void setField(Class cls, 201 String name, 202 Object value) 203 throws NoSuchFieldException { 204 if (cls == null) { 205 throw new IllegalArgumentException ("Invalid null cls argument"); 206 } 207 Class base = cls; 208 while (base != null) { 209 try { 210 Field field = base.getDeclaredField(name); 211 field.setAccessible(true); 212 field.set(base, value); 213 return; 214 } catch (Exception ex) { 215 217 ; 218 } 219 base = base.getSuperclass(); 220 } 221 throw new NoSuchFieldException ("Could set value for static field " + 222 cls.getName() + "." + name); 223 } 224 225 239 public static Object invoke(Object object, 240 String name, 241 Class parameterTypes[], 242 Object args[]) 243 throws Throwable { 244 if (object == null) { 245 throw new IllegalArgumentException ("Invalid null object argument"); 246 } 247 Class cls = object.getClass(); 248 while (cls != null) { 249 try { 250 Method method = cls.getDeclaredMethod(name, 251 parameterTypes); 252 method.setAccessible(true); 253 return method.invoke(object, args); 254 } catch (InvocationTargetException e) { 255 257 throw e.getTargetException(); 258 } catch (Exception ex) { 259 261 ; 262 } 263 cls = cls.getSuperclass(); 264 } 265 throw new NoSuchMethodException ("Failed method invocation: " + 266 object.getClass().getName() + "." + name + "()"); 267 } 268 269 283 public static Object invoke(Class cls, 284 String name, 285 Class parameterTypes[], 286 Object args[]) 287 throws Throwable { 288 if (cls == null) { 289 throw new IllegalArgumentException ("Invalid null cls argument"); 290 } 291 Class base = cls; 292 while (base != null) { 293 try { 294 Method method = base.getDeclaredMethod(name, 295 parameterTypes); 296 method.setAccessible(true); 297 return method.invoke(base, args); 298 } catch (InvocationTargetException e) { 299 301 throw (Exception ) e.getTargetException(); 302 } catch (Exception ex) { 303 305 ; 306 } 307 base = base.getSuperclass(); 308 } 309 throw new NoSuchMethodException ("Failed static method invocation: " + 310 cls.getName() + "." + name + "()"); 311 } 312 313 } 314 | Popular Tags |