KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > util > PrivilegedAccessor


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Charlie Hubbard, Prashant Dhokte.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.util;
26
27 import java.lang.reflect.Field JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 /**
32  * a.k.a. The "ObjectMolester"
33  *
34  * <p>
35  * This class is used to access a method or field of an object no matter what
36  * the access modifier of the method or field. The syntax for accessing fields
37  * and methods is out of the ordinary because this class uses reflection to
38  * peel away protection.
39  *
40  * <p>
41  * Here is an example of using this to access a private member. <code>resolveName</code>
42  * is a private method of <code>Class</code>.
43  *
44  * <pre>
45  * Class c = Class.class; System.out.println( PrivilegedAccessor.invokeMethod(c, "resolveName", "/net/iss/common/PrivilegeAccessor"));
46  * </pre>
47  *
48  * @author <a HREF="mailto:chubbard@iss.net">Charlie Hubbard</a>
49  * @author <a HREF="mailto:pdhokte@iss.net">Prashant Dhokte</a>
50  */

51 public class PrivilegedAccessor
52 {
53   /**
54    * Gets the value of the named field and returns it as an object.
55    *
56    * @param instance the object instance
57    * @param fieldName the name of the field
58    * @return an object representing the value of the field
59    * @throws IllegalAccessException if the access to the class is refused
60    * @throws NoSuchFieldException if the field name does not exist
61    */

62   public static Object JavaDoc getValue(Object JavaDoc instance, String JavaDoc fieldName)
63     throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc
64   {
65     Field JavaDoc field = getField(instance.getClass(), fieldName);
66     field.setAccessible(true);
67     return field.get(instance);
68   }
69
70   /**
71    * Calls a method on the given object instance with the given argument.
72    *
73    * @param instance the object instance
74    * @param methodName the name of the method to invoke
75    * @param arg the argument to pass to the method
76    * @return an <code>Object</code> instance
77    * @throws NoSuchMethodException when no such method exists
78    * @throws IllegalAccessException if the access to the class is refused
79    * @throws InvocationTargetException if the invocation failed
80    * @see PrivilegedAccessor#invokeMethod(Object,String,Object[])
81    */

82   public static Object JavaDoc invokeMethod(
83     Object JavaDoc instance,
84     String JavaDoc methodName,
85     Object JavaDoc arg)
86     throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
87   {
88     Object JavaDoc[] args = new Object JavaDoc[1];
89     args[0] = arg;
90     return invokeMethod(instance, methodName, args);
91   }
92
93   /**
94    * Calls a method on the given object instance with the given arguments.
95    *
96    * @param instance the object instance
97    * @param methodName the name of the method to invoke
98    * @param args an array of objects to pass as arguments
99    * @return an <code>Object</code> instance
100    * @throws NoSuchMethodException when no such method exists
101    * @throws IllegalAccessException if the access to the class is refused
102    * @throws InvocationTargetException if the invocation failed
103    * @see PrivilegedAccessor#invokeMethod(Object,String,Object)
104    */

105   public static Object JavaDoc invokeMethod(
106     Object JavaDoc instance,
107     String JavaDoc methodName,
108     Object JavaDoc[] args)
109     throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
110   {
111     Class JavaDoc[] classTypes = null;
112     if (args != null)
113     {
114       classTypes = new Class JavaDoc[args.length];
115       for (int i = 0; i < args.length; i++)
116       {
117         if (args[i] != null)
118           classTypes[i] = args[i].getClass();
119       }
120     }
121     return getMethod(instance, methodName, classTypes).invoke(instance, args);
122   }
123
124   /**
125    * @param instance the object instance
126    * @param methodName the method name
127    * @param classTypes class types
128    * @return a <code>Method</code> instance
129    * @throws NoSuchMethodException when no such method exists
130    */

131   public static Method JavaDoc getMethod(
132     Object JavaDoc instance,
133     String JavaDoc methodName,
134     Class JavaDoc[] classTypes)
135     throws NoSuchMethodException JavaDoc
136   {
137     Method JavaDoc accessMethod =
138       getMethod(instance.getClass(), methodName, classTypes);
139     accessMethod.setAccessible(true);
140     return accessMethod;
141   }
142
143   /**
144    * Returns the named field from the given class.
145    *
146    * @param thisClass class
147    * @param fieldName field name
148    * @return a <code>Field</code> instance
149    * @throws NoSuchFieldException when no such field exists
150    */

151   private static Field JavaDoc getField(Class JavaDoc thisClass, String JavaDoc fieldName)
152     throws NoSuchFieldException JavaDoc
153   {
154     if (thisClass == null)
155       throw new NoSuchFieldException JavaDoc("Invalid field : " + fieldName);
156     try
157     {
158       return thisClass.getDeclaredField(fieldName);
159     }
160     catch (NoSuchFieldException JavaDoc e)
161     {
162       return getField(thisClass.getSuperclass(), fieldName);
163     }
164   }
165
166   /**
167    * Returns the named method with a method signature matching classTypes from
168    * the given class.
169    *
170    * @param thisClass class
171    * @param methodName method name
172    * @param classTypes class types
173    * @return a <code>Method</code> instance
174    * @throws NoSuchMethodException when no such method exists
175    */

176   private static Method JavaDoc getMethod(
177     Class JavaDoc thisClass,
178     String JavaDoc methodName,
179     Class JavaDoc[] classTypes)
180     throws NoSuchMethodException JavaDoc
181   {
182     if (thisClass == null)
183       throw new NoSuchMethodException JavaDoc("Invalid method : " + methodName);
184     try
185     {
186       return thisClass.getDeclaredMethod(methodName, classTypes);
187     }
188     catch (NoSuchMethodException JavaDoc e)
189     {
190       return getMethod(thisClass.getSuperclass(), methodName, classTypes);
191     }
192   }
193 }
194
Popular Tags