junitx.ddtunit.util
Class PrivilegedAccessor

java.lang.Object
  extended by junitx.ddtunit.util.PrivilegedAccessor

public class PrivilegedAccessor
extends 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 Object getFieldValue(Object instance, String fieldName)
          Gets the value of the named field and returns it as an object.
static Method getMethod(Object instance, String methodName, Class[] classTypes)
           
static Object invokeMethod(Object instance, String methodName, Object arg)
          Calls a method on the given object instance with the given argument.
static Object invokeMethod(Object instance, String methodName, Object[] args)
          Calls a method on the given object instance with the given arguments.
static void setFieldValue(Object instance, String fieldName, Object value)
          Set the value of the named field and returns it as an object.
 
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

getFieldValue

public static Object getFieldValue(Object instance,
                                   String fieldName)
                            throws IllegalAccessException,
                                   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:
IllegalAccessException - Description of the Exception
NoSuchFieldException - Description of the Exception

setFieldValue

public static void setFieldValue(Object instance,
                                 String fieldName,
                                 Object value)
                          throws IllegalAccessException,
                                 NoSuchFieldException
Set the value of the named field and returns it as an object.

Parameters:
instance - the object instance
fieldName - the name of the field
value - The new value value
Throws:
IllegalAccessException - Description of the Exception
NoSuchFieldException - Description of the Exception

invokeMethod

public static Object invokeMethod(Object instance,
                                  String methodName,
                                  Object arg)
                           throws NoSuchMethodException,
                                  IllegalAccessException,
                                  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:
Description of the Return Value
Throws:
NoSuchMethodException - Description of the Exception
IllegalAccessException - Description of the Exception
InvocationTargetException - Description of the Exception
See Also:
invokeMethod(Object,String,Object[])

invokeMethod

public static Object invokeMethod(Object instance,
                                  String methodName,
                                  Object[] args)
                           throws NoSuchMethodException,
                                  IllegalAccessException,
                                  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:
Description of the Return Value
Throws:
NoSuchMethodException - Description of the Exception
IllegalAccessException - Description of the Exception
InvocationTargetException - Description of the Exception
See Also:
invokeMethod(Object,String,Object)

getMethod

public static Method getMethod(Object instance,
                               String methodName,
                               Class[] classTypes)
                        throws NoSuchMethodException
Parameters:
instance - the object instance
methodName - the
classTypes - Description of the Parameter
Returns:
The method value
Throws:
NoSuchMethodException - Description of the Exception


Copyright © 2004-2011. All Rights Reserved.