JUnit-addons
version 1.4

junitx.util
Class PrivateAccessor

java.lang.Object
  |
  +--junitx.util.PrivateAccessor

public class PrivateAccessor
extends java.lang.Object

Utility class to bypass the Java modifiers security and access protected and private fields and methods.

Note

When specifying parameter types, use int.class, double.class, short.class, long.class, float.class and char.class for primitive types.

Example: To set the value of an object obj to 100 via the method setValue():

    PrivateAccessor.invoke( obj, "setValue", new Class[]{int.class}, new Object[]{new Integer( 100 )} );
 

Version:
$Revision: 1.5 $ $Date: 2003/03/21 06:13:49 $
Author:
Vladimir R. Bossicard

Method Summary
static java.lang.Object getField(java.lang.Class cls, java.lang.String name)
          Returns the value of the field on the specified class.
static java.lang.Object getField(java.lang.Object object, java.lang.String name)
          Returns the value of the field on the specified object.
static java.lang.Object invoke(java.lang.Class cls, java.lang.String name, java.lang.Class[] parameterTypes, java.lang.Object[] args)
          Invokes the method represented by the name value on the specified class with the specified parameters.
static java.lang.Object invoke(java.lang.Object object, java.lang.String name, java.lang.Class[] parameterTypes, java.lang.Object[] args)
          Invokes the method represented by the name value on the specified object with the specified parameters.
static void setField(java.lang.Class cls, java.lang.String name, java.lang.Object value)
          Sets the field represented by the name value on the specified class argument to the specified new value.
static void setField(java.lang.Object object, java.lang.String name, java.lang.Object value)
          Sets the field represented by the name value on the specified object argument to the specified new value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getField

public static java.lang.Object getField(java.lang.Object object,
                                        java.lang.String name)
                                 throws java.lang.NoSuchFieldException
Returns the value of the field on the specified object. The name parameter is a String specifying the simple name of the desired field.

The object is first searched for any matching field. If no matching field is found, the superclasses are recursively searched.

Throws:
java.lang.NoSuchFieldException - if a field with the specified name is not found.

getField

public static java.lang.Object getField(java.lang.Class cls,
                                        java.lang.String name)
                                 throws java.lang.NoSuchFieldException
Returns the value of the field on the specified class. The name parameter is a String specifying the simple name of the desired field.

The class is first searched for any matching field. If no matching field is found, the superclasses are recursively searched.

Throws:
java.lang.NoSuchFieldException - if a field with the specified name is not found.

setField

public static void setField(java.lang.Object object,
                            java.lang.String name,
                            java.lang.Object value)
                     throws java.lang.NoSuchFieldException
Sets the field represented by the name value on the specified object argument to the specified new value. The new value is automatically unwrapped if the underlying field has a primitive type.

The object is first searched for any matching field. If no matching field is found, the superclasses are recursively searched.

Throws:
java.lang.NoSuchFieldException - if a field with the specified name is not found.

setField

public static void setField(java.lang.Class cls,
                            java.lang.String name,
                            java.lang.Object value)
                     throws java.lang.NoSuchFieldException
Sets the field represented by the name value on the specified class argument to the specified new value. The new value is automatically unwrapped if the underlying field has a primitive type.

The class is first searched for any matching field. If no matching field is found, the superclasses are recursively searched.

Throws:
java.lang.NoSuchFieldException - if a field with the specified name is not found.

invoke

public static java.lang.Object invoke(java.lang.Object object,
                                      java.lang.String name,
                                      java.lang.Class[] parameterTypes,
                                      java.lang.Object[] args)
                               throws java.lang.Throwable
Invokes the method represented by the name value on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to widening conversions as necessary. The value returned by the method is automatically wrapped in an object if it has a primitive type.

The object is first searched for any matching method. If no matching method is found, the superclasses are recursively searched.

Throws:
java.lang.NoSuchMethodException - if a matching method is not found or if the name is ""or "".
java.lang.Throwable

invoke

public static java.lang.Object invoke(java.lang.Class cls,
                                      java.lang.String name,
                                      java.lang.Class[] parameterTypes,
                                      java.lang.Object[] args)
                               throws java.lang.Throwable
Invokes the method represented by the name value on the specified class with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to widening conversions as necessary. The value returned by the method is automatically wrapped in an object if it has a primitive type.

The class is first searched for any matching method. If no matching class is found, the superclasses are recursively searched.

Throws:
java.lang.NoSuchMethodException - if a matching method is not found or if the name is ""or "".
java.lang.Throwable

JUnit-addons
version 1.4

Copyright © 2002-2003 Vladimir R. Bossicard. All Rights Reserved.