KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > testutil > PrivilegedAccessor


1 package net.sf.uitags.testutil;
2
3 import java.lang.reflect.Field JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6
7 /**
8  * a.k.a. The "ObjectMolester"
9  * <p>
10  * This class is used to access a method or field of an object no
11  * matter what the access modifier of the method or field. The syntax
12  * for accessing fields and methods is out of the ordinary because this
13  * class uses reflection to peel away protection.
14  * <p>
15  * Here is an example of using this to access a private member.
16  * <code>resolveName</code> is a private method of <code>Class</code>.
17  *
18  * <pre>
19  * Class c = Class.class;
20  * System.out.println(
21  * PrivilegedAccessor.invokeMethod(
22  * c,
23  * "resolveName",
24  * "/net/iss/common/PrivilegeAccessor" ) );
25  * </pre>
26  *
27  * @author Charlie Hubbard (chubbard@iss.net)
28  * @author Prashant Dhokte (pdhokte@iss.net)
29  */

30 public final class PrivilegedAccessor {
31   /**
32    * Hides the constructor
33    */

34   private PrivilegedAccessor() {
35     // empty constructor
36
}
37
38   /**
39    * Gets the value of the named field and returns it as an object.
40    *
41    * @param instance the object instance
42    * @param fieldName the name of the field
43    * @return an object representing the value of the field
44    * @throws IllegalAccessException to communicate error
45    * @throws NoSuchFieldException to communicate error
46    */

47   public static Object JavaDoc getValue(Object JavaDoc instance, String JavaDoc fieldName)
48       throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
49       Field JavaDoc field = getField(instance.getClass(), fieldName);
50       field.setAccessible(true);
51       return field.get(instance);
52   }
53
54   /**
55    * Calls a method on the given object instance with the given argument.
56    *
57    * @param instance the object instance
58    * @param methodName the name of the method to invoke
59    * @param arg the argument to pass to the method
60    * @see PrivilegedAccessor#invokeMethod(Object,String,Object[])
61    * @throws NoSuchMethodException to communicate error
62    * @throws IllegalAccessException to communicate error
63    * @throws InvocationTargetException to communicate error
64    * @return the object
65    */

66   public static Object JavaDoc invokeMethod(
67       Object JavaDoc instance, String JavaDoc methodName, Object JavaDoc arg)
68       throws NoSuchMethodException JavaDoc,
69           IllegalAccessException JavaDoc,
70           InvocationTargetException JavaDoc {
71       Object JavaDoc[] args = new Object JavaDoc[1];
72       args[0] = arg;
73       return invokeMethod(instance, methodName, args);
74   }
75
76   /**
77    * Calls a method on the given object instance with the given arguments.
78    *
79    * @param instance the object instance
80    * @param methodName the name of the method to invoke
81    * @param args an array of objects to pass as arguments
82    * @see PrivilegedAccessor#invokeMethod(Object,String,Object)
83    * @throws NoSuchMethodException to communicate error
84    * @throws IllegalAccessException to communicate error
85    * @throws InvocationTargetException to communicate error
86    * @return the object
87    */

88   public static Object JavaDoc invokeMethod(
89       Object JavaDoc instance, String JavaDoc methodName, Object JavaDoc[] args)
90       throws NoSuchMethodException JavaDoc,
91           IllegalAccessException JavaDoc,
92           InvocationTargetException JavaDoc {
93       Class JavaDoc[] classTypes = null;
94       if (args != null) {
95           classTypes = new Class JavaDoc[args.length];
96           for (int i = 0; i < args.length; i++) {
97               if (args[i] != null) {
98                   classTypes[i] = args[i].getClass();
99               }
100           }
101       }
102       return getMethod(instance,methodName,
103           classTypes).invoke(instance, args);
104   }
105
106   /**
107    *
108    * @param instance the object instance
109    * @param methodName the
110    */

111   public static Method JavaDoc getMethod(Object JavaDoc instance, String JavaDoc methodName,
112       Class JavaDoc[] classTypes) throws NoSuchMethodException JavaDoc {
113       Method JavaDoc accessMethod = getMethod(
114           instance.getClass(), methodName, classTypes);
115       accessMethod.setAccessible(true);
116       return accessMethod;
117   }
118
119   /**
120    * Return the named field from the given class.
121    */

122   private static Field JavaDoc getField(Class JavaDoc thisClass, String JavaDoc fieldName) throws NoSuchFieldException JavaDoc {
123       if (thisClass == null)
124           throw new NoSuchFieldException JavaDoc("Invalid field : " + fieldName);
125       try {
126           return thisClass.getDeclaredField( fieldName );
127       }
128       catch(NoSuchFieldException JavaDoc e) {
129           return getField(thisClass.getSuperclass(), fieldName);
130       }
131   }
132
133   /**
134    * Return the named method with a method signature matching classTypes
135    * from the given class.
136    */

137   private static Method JavaDoc getMethod(Class JavaDoc thisClass, String JavaDoc methodName, Class JavaDoc[] classTypes) throws NoSuchMethodException JavaDoc {
138       if (thisClass == null)
139           throw new NoSuchMethodException JavaDoc("Invalid method : " + methodName);
140       try {
141           return thisClass.getDeclaredMethod( methodName, classTypes );
142       }
143       catch(NoSuchMethodException JavaDoc e) {
144           return getMethod(thisClass.getSuperclass(), methodName, classTypes);
145       }
146   }
147 }
Popular Tags