|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object junitx.ddtunit.util.PrivilegedAccessor
public class PrivilegedAccessor
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"));
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 |
---|
public PrivilegedAccessor()
Method Detail |
---|
public static Object getFieldValue(Object instance, String fieldName) throws IllegalAccessException, NoSuchFieldException
instance
- the object instancefieldName
- the name of the field
IllegalAccessException
- Description of the Exception
NoSuchFieldException
- Description of the Exceptionpublic static void setFieldValue(Object instance, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException
instance
- the object instancefieldName
- the name of the fieldvalue
- The new value value
IllegalAccessException
- Description of the Exception
NoSuchFieldException
- Description of the Exceptionpublic static Object invokeMethod(Object instance, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
instance
- the object instancemethodName
- the name of the method to invokearg
- the argument to pass to the method
NoSuchMethodException
- Description of the Exception
IllegalAccessException
- Description of the Exception
InvocationTargetException
- Description of the ExceptioninvokeMethod(Object,String,Object[])
public static Object invokeMethod(Object instance, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
instance
- the object instancemethodName
- the name of the method to invokeargs
- an array of objects to pass as arguments
NoSuchMethodException
- Description of the Exception
IllegalAccessException
- Description of the Exception
InvocationTargetException
- Description of the ExceptioninvokeMethod(Object,String,Object)
public static Method getMethod(Object instance, String methodName, Class[] classTypes) throws NoSuchMethodException
instance
- the object instancemethodName
- theclassTypes
- Description of the Parameter
NoSuchMethodException
- Description of the Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |