pt.moredata.tests
Class PrivilegedAccessor

java.lang.Object
  extended bypt.moredata.tests.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 (chubbard@iss.net), Prashant Dhokte (pdhokte@iss.net)

Constructor Summary
PrivilegedAccessor()
           
 
Method Summary
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
java.lang.NoSuchFieldException

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
Throws:
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
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
Throws:
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
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
Throws:
java.lang.NoSuchMethodException