org.objectweb.cjdbc.test.util
Class PrivilegedAccessor

java.lang.Object
  extended byorg.objectweb.cjdbc.test.util.PrivilegedAccessor

public class PrivilegedAccessor
extends java.lang.Object

a.k.a. The "ObjectMolester"

This class is used to access a method or field of an object no matter what the access modifier of the method or field. The syntax for accessing fields and methods is out of the ordinary because this class uses reflection to peel away protection.

Here is an example of using this to access a private member. resolveName is a private method of Class.

 Class c = Class.class;
 System.out.println(
      PrivilegedAccessor.invokeMethod(c,
                                      "resolveName",
                                      "/net/iss/common/PrivilegeAccessor"));
 

Author:
Charlie Hubbard, Prashant Dhokte

Constructor Summary
PrivilegedAccessor()
           
 
Method Summary
private static java.lang.reflect.Field getField(java.lang.Class thisClass, java.lang.String fieldName)
          Returns the named field from the given class.
private static java.lang.reflect.Method getMethod(java.lang.Class thisClass, java.lang.String methodName, java.lang.Class[] classTypes)
          Returns the named method with a method signature matching classTypes from the given class.
static java.lang.reflect.Method getMethod(java.lang.Object instance, java.lang.String methodName, java.lang.Class[] classTypes)
           
static java.lang.Object getValue(java.lang.Object instance, java.lang.String fieldName)
          Gets the value of the named field and returns it as an object.
static java.lang.Object invokeMethod(java.lang.Object instance, java.lang.String methodName, java.lang.Object arg)
          Calls a method on the given object instance with the given argument.
static java.lang.Object invokeMethod(java.lang.Object instance, java.lang.String methodName, java.lang.Object[] args)
          Calls a method on the given object instance with the given arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrivilegedAccessor

public PrivilegedAccessor()
Method Detail

getValue

public static java.lang.Object getValue(java.lang.Object instance,
                                        java.lang.String fieldName)
                                 throws java.lang.IllegalAccessException,
                                        java.lang.NoSuchFieldException
Gets the value of the named field and returns it as an object.

Parameters:
instance - the object instance
fieldName - the name of the field
Returns:
an object representing the value of the field
Throws:
java.lang.IllegalAccessException - if the access to the class is refused
java.lang.NoSuchFieldException - if the field name does not exist

invokeMethod

public static java.lang.Object invokeMethod(java.lang.Object instance,
                                            java.lang.String methodName,
                                            java.lang.Object arg)
                                     throws java.lang.NoSuchMethodException,
                                            java.lang.IllegalAccessException,
                                            java.lang.reflect.InvocationTargetException
Calls a method on the given object instance with the given argument.

Parameters:
instance - the object instance
methodName - the name of the method to invoke
arg - the argument to pass to the method
Returns:
an Object instance
Throws:
java.lang.NoSuchMethodException - when no such method exists
java.lang.IllegalAccessException - if the access to the class is refused
java.lang.reflect.InvocationTargetException - if the invocation failed
See Also:
invokeMethod(Object,String,Object[])

invokeMethod

public static java.lang.Object invokeMethod(java.lang.Object instance,
                                            java.lang.String methodName,
                                            java.lang.Object[] args)
                                     throws java.lang.NoSuchMethodException,
                                            java.lang.IllegalAccessException,
                                            java.lang.reflect.InvocationTargetException
Calls a method on the given object instance with the given arguments.

Parameters:
instance - the object instance
methodName - the name of the method to invoke
args - an array of objects to pass as arguments
Returns:
an Object instance
Throws:
java.lang.NoSuchMethodException - when no such method exists
java.lang.IllegalAccessException - if the access to the class is refused
java.lang.reflect.InvocationTargetException - if the invocation failed
See Also:
invokeMethod(Object,String,Object)

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Object instance,
                                                 java.lang.String methodName,
                                                 java.lang.Class[] classTypes)
                                          throws java.lang.NoSuchMethodException
Parameters:
instance - the object instance
methodName - the method name
classTypes - class types
Returns:
a Method instance
Throws:
java.lang.NoSuchMethodException - when no such method exists

getField

private static java.lang.reflect.Field getField(java.lang.Class thisClass,
                                                java.lang.String fieldName)
                                         throws java.lang.NoSuchFieldException
Returns the named field from the given class.

Parameters:
thisClass - class
fieldName - field name
Returns:
a Field instance
Throws:
java.lang.NoSuchFieldException - when no such field exists

getMethod

private static java.lang.reflect.Method getMethod(java.lang.Class thisClass,
                                                  java.lang.String methodName,
                                                  java.lang.Class[] classTypes)
                                           throws java.lang.NoSuchMethodException
Returns the named method with a method signature matching classTypes from the given class.

Parameters:
thisClass - class
methodName - method name
classTypes - class types
Returns:
a Method instance
Throws:
java.lang.NoSuchMethodException - when no such method exists


Copyright © 2002, 2003 - ObjectWeb Consortium - All Rights Reserved.