silk.performer
Class ReflectionUtil


java.lang.Object

  extended bysilk.performer.ReflectionUtil


public class ReflectionUtil
extends java.lang.Object

The ReflectionUtil class provides methods for reading and writing non-public fields of objects, for reading non-public static fields of classes and for reconstructing objects from a serialized representation stored in a file. These methods are recorded as needed by SilkPerformer's Java Recorder.


Constructor Summary
ReflectionUtil()
          Default constructor.
ReflectionUtil(SilkPerformer silkPerformer)
          Constructor for use within SilkPerformers JavaFramework.
 
Method Summary
 java.lang.Object deserialize(java.lang.String identifier)
          Reconstructs an object from a serialized version stored in a file.
 void ensureSecurityManager()
          Ensures that a security manager is installed.
static boolean getNpBoolean(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the boolean value of the specified field of the specified object.
static byte getNpByte(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the byte value of the specified field of the specified object.
static char getNpChar(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the char value of the specified field of the specified object.
static double getNpDouble(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the double value of the specified field of the specified object.
static float getNpFloat(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the float value of the specified field of the specified object.
static int getNpInt(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the int value of the specified field of the specified object.
static long getNpLong(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the long value of the specified field of the specified object.
static java.lang.Object getNpObject(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the Object value of the specified field of the specified object.
static short getNpShort(java.lang.Object obj, java.lang.String fieldName)
          Retrieves the short value of the specified field of the specified object.
static boolean getNpStaticBoolean(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the boolean value of the specified static field of the specified class.
static byte getNpStaticByte(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the byte value of the specified static field of the specified class.
static char getNpStaticChar(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the char value of the specified static field of the specified class.
static double getNpStaticDouble(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the double value of the specified static field of the specified class.
static float getNpStaticFloat(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the float value of the specified static field of the specified class.
static int getNpStaticInt(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the int value of the specified static field of the specified class.
static long getNpStaticLong(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the long value of the specified static field of the specified class.
static java.lang.Object getNpStaticObject(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the Object value of the specified static field of the specified class.
static short getNpStaticShort(java.lang.Class clazz, java.lang.String fieldName)
          Retrieves the short value of the specified static field of the specified class.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, boolean value)
          Assigns a boolean value to the specified field of the specified object.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, byte value)
          Assigns a byte value to the specified field of the specified object.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, char value)
          Assigns a char value to the specified field of the specified object.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, double value)
          Assigns a double value to the specified field of the specified object.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, float value)
          Assigns a float value to the specified field of the specified object.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, int value)
          Assigns an int value to the specified field of the specified object.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, long value)
          Assigns a long value to the specified field of the specified object.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, java.lang.Object value)
          Assigns an Object value to the specified field of the specified object.
static void setPrivateField(java.lang.Object obj, java.lang.String fieldName, short value)
          Assigns a short value to the specified field of the specified object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtil


public ReflectionUtil()
Default constructor.

For use when scripts recorded with SilkPerformer's Java Recorder are executed outside of SilkPerformer, e.g. JUnit. The method deserialize will use the context classloader to find the file which contains a serialized object.


ReflectionUtil


public ReflectionUtil(SilkPerformer silkPerformer)
Constructor for use within SilkPerformers JavaFramework.

The method deserialize will use the specified SilkPerformer object to find the file which contains a serialized object. This includes searching in datafiles attached to a SilkPerformer project.

Parameters:
silkPerformer - The SilkPerformer object which links to SilkPerformer's replaying engine.
Method Detail

ensureSecurityManager


public void ensureSecurityManager()
Ensures that a security manager is installed.

If no security manager is installed, a new RMISecurityManager will be installed, otherwise no action is performed.

This is useful for RMI applications, which require that a security manager is installed.


deserialize


public java.lang.Object deserialize(java.lang.String identifier)
                             throws SilkPerformerException
Reconstructs an object from a serialized version stored in a file.

When constructed with the default constructor, the specified file must be available within the classpath of the context classloader. When used within SilkPerformer's replay engine and constructed with ReflectionUtil(SilkPerformer), the specified file will be search in the datafiles attached to the current SilkPerformer project.

Parameters:
identifier - Specifies the filename without extension. The extension ".ser" is added automatically.
Returns:
The object reconstructed from the serialized representation.

Throws:
SilkPerformerException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   java.lang.Object value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns an Object value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   boolean value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns a boolean value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   byte value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns a byte value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   short value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns a short value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   int value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns an int value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   long value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns a long value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   char value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns a char value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   float value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns a float value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

setPrivateField


public static void setPrivateField(java.lang.Object obj,
                                   java.lang.String fieldName,
                                   double value)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Assigns a double value to the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
value - Value to assign to the field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpObject


public static java.lang.Object getNpObject(java.lang.Object obj,
                                           java.lang.String fieldName)
                                    throws java.lang.NoSuchFieldException,
                                           java.lang.IllegalAccessException
Retrieves the Object value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpBoolean


public static boolean getNpBoolean(java.lang.Object obj,
                                   java.lang.String fieldName)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException
Retrieves the boolean value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpByte


public static byte getNpByte(java.lang.Object obj,
                             java.lang.String fieldName)
                      throws java.lang.NoSuchFieldException,
                             java.lang.IllegalAccessException
Retrieves the byte value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpShort


public static short getNpShort(java.lang.Object obj,
                               java.lang.String fieldName)
                        throws java.lang.NoSuchFieldException,
                               java.lang.IllegalAccessException
Retrieves the short value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpInt


public static int getNpInt(java.lang.Object obj,
                           java.lang.String fieldName)
                    throws java.lang.NoSuchFieldException,
                           java.lang.IllegalAccessException
Retrieves the int value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpLong


public static long getNpLong(java.lang.Object obj,
                             java.lang.String fieldName)
                      throws java.lang.NoSuchFieldException,
                             java.lang.IllegalAccessException
Retrieves the long value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpChar


public static char getNpChar(java.lang.Object obj,
                             java.lang.String fieldName)
                      throws java.lang.NoSuchFieldException,
                             java.lang.IllegalAccessException
Retrieves the char value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpFloat


public static float getNpFloat(java.lang.Object obj,
                               java.lang.String fieldName)
                        throws java.lang.NoSuchFieldException,
                               java.lang.IllegalAccessException
Retrieves the float value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpDouble


public static double getNpDouble(java.lang.Object obj,
                                 java.lang.String fieldName)
                          throws java.lang.NoSuchFieldException,
                                 java.lang.IllegalAccessException
Retrieves the double value of the specified field of the specified object.

Parameters:
obj - The object.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getNpStaticObject


public static java.lang.Object getNpStaticObject(java.lang.Class clazz,
                                                 java.lang.String fieldName)
                                          throws java.lang.NoSuchFieldException,
                                                 java.lang.IllegalAccessException,
                                                 SilkPerformerException
Retrieves the Object value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException

getNpStaticBoolean


public static boolean getNpStaticBoolean(java.lang.Class clazz,
                                         java.lang.String fieldName)
                                  throws java.lang.NoSuchFieldException,
                                         java.lang.IllegalAccessException,
                                         SilkPerformerException
Retrieves the boolean value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException

getNpStaticByte


public static byte getNpStaticByte(java.lang.Class clazz,
                                   java.lang.String fieldName)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException,
                                   SilkPerformerException
Retrieves the byte value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException

getNpStaticShort


public static short getNpStaticShort(java.lang.Class clazz,
                                     java.lang.String fieldName)
                              throws java.lang.NoSuchFieldException,
                                     java.lang.IllegalAccessException,
                                     SilkPerformerException
Retrieves the short value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException

getNpStaticInt


public static int getNpStaticInt(java.lang.Class clazz,
                                 java.lang.String fieldName)
                          throws java.lang.NoSuchFieldException,
                                 java.lang.IllegalAccessException,
                                 SilkPerformerException
Retrieves the int value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException

getNpStaticLong


public static long getNpStaticLong(java.lang.Class clazz,
                                   java.lang.String fieldName)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException,
                                   SilkPerformerException
Retrieves the long value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException

getNpStaticChar


public static char getNpStaticChar(java.lang.Class clazz,
                                   java.lang.String fieldName)
                            throws java.lang.NoSuchFieldException,
                                   java.lang.IllegalAccessException,
                                   SilkPerformerException
Retrieves the char value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException

getNpStaticFloat


public static float getNpStaticFloat(java.lang.Class clazz,
                                     java.lang.String fieldName)
                              throws java.lang.NoSuchFieldException,
                                     java.lang.IllegalAccessException,
                                     SilkPerformerException
Retrieves the float value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException

getNpStaticDouble


public static double getNpStaticDouble(java.lang.Class clazz,
                                       java.lang.String fieldName)
                                throws java.lang.NoSuchFieldException,
                                       java.lang.IllegalAccessException,
                                       SilkPerformerException
Retrieves the double value of the specified static field of the specified class.

Parameters:
clazz - The class.
fieldName - The name of the field. This field needs not to be public.
Returns:
Value of the specified field.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
SilkPerformerException