com.sun.btrace
Class BTraceUtils.Reflective

java.lang.Object
  extended by com.sun.btrace.BTraceUtils.Reflective
Enclosing class:
BTraceUtils

public static class BTraceUtils.Reflective
extends java.lang.Object


Constructor Summary
BTraceUtils.Reflective()
           
 
Method Summary
static int accessFlags(java.lang.Class clazz)
          Returns the access flags of the given Class.
static int accessFlags(java.lang.reflect.Field field)
          Returns the access flags of the given Field.
static java.lang.Class classForName(java.lang.String name)
          Returns Class object for given class name.
static java.lang.Class classForName(java.lang.String name, java.lang.ClassLoader cl)
          Returns the Class for the given class name using the given class loader.
static java.lang.Class classOf(java.lang.Object obj)
          Returns the runtime class of the given Object.
static java.lang.ClassLoader contextClassLoader()
          Returns the current context class loader
static java.lang.Class declaringClass(java.lang.reflect.Field field)
          Returns the Class object representing the class or interface that declares the field represented by the given Field object.
static java.lang.reflect.Field field(java.lang.Class clazz, java.lang.String name)
          Returns a Field object that reflects the specified declared field of the class or interface represented by the given Class object.
static java.lang.reflect.Field field(java.lang.Class clazz, java.lang.String name, boolean throwException)
          Returns a Field object that reflects the specified declared field of the class or interface represented by the given Class object.
static java.lang.reflect.Field field(java.lang.String clazz, java.lang.String name)
          Returns a Field object that reflects the specified declared field of the class or interface represented by the given Class object.
static java.lang.reflect.Field field(java.lang.String clazz, java.lang.String name, boolean throwException)
          Returns a Field object that reflects the specified declared field of the class or interface represented by the given Class object.
static java.lang.Object get(java.lang.reflect.Field field)
          Gets the value of a static reference field.
static java.lang.Object get(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance reference field.
static boolean getBoolean(java.lang.reflect.Field field)
          Gets the value of a static boolean field.
static boolean getBoolean(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance boolean field.
static byte getByte(java.lang.reflect.Field field)
          Gets the value of a static byte field.
static byte getByte(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance byte field.
static char getChar(java.lang.reflect.Field field)
          Gets the value of a static char field.
static char getChar(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance char field.
static java.lang.Class getComponentType(java.lang.Class clazz)
          returns component type of an array Class.
static double getDouble(java.lang.reflect.Field field)
          Gets the value of a static double field.
static double getDouble(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance double field.
static float getFloat(java.lang.reflect.Field field)
          Gets the value of a static float field.
static float getFloat(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance float field.
static int getInt(java.lang.reflect.Field field)
          Gets the value of a static int field.
static int getInt(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance int field.
static long getLong(java.lang.reflect.Field field)
          Gets the value of a static long field.
static long getLong(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance long field.
static short getShort(java.lang.reflect.Field field)
          Gets the value of a static short field.
static short getShort(java.lang.reflect.Field field, java.lang.Object obj)
          Gets the value of an instance short field.
static java.lang.Class getSuperclass(java.lang.Class clazz)
          Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by the given Class.
static boolean isArray(java.lang.Class clazz)
          Determines if the given Class object represents an array class.
static boolean isAssignableFrom(java.lang.Class<?> a, java.lang.Class<?> b)
          Determines if the class or interface represented by the first Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the second Class parameter.
static boolean isInstance(java.lang.Class clazz, java.lang.Object obj)
          Determines if the specified Object is assignment-compatible with the object represented by the specified Class.
static boolean isInterface(java.lang.Class clazz)
          Determines if the specified Class object represents an interface type.
static boolean isPrimitive(java.lang.Class clazz)
          Returns whether the given Class represent primitive type or not.
static java.lang.String name(java.lang.Class clazz)
          Returns the name of the given Class object.
static java.lang.String name(java.lang.reflect.Field field)
          Returns the name of the Field object.
static void printFields(java.lang.Object obj)
          Print all instance fields of an object as name-value pairs.
static void printFields(java.lang.Object obj, boolean classNamePrefix)
          Print all instance fields of an object as name-value pairs.
static void printStaticFields(java.lang.Class clazz)
          Print all static fields of the class as name-value pairs.
static void printStaticFields(java.lang.Class clazz, boolean classNamePrefix)
          Print all static fields of the class as name-value pairs.
static java.lang.Class type(java.lang.reflect.Field field)
          Returns the type of the Field object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BTraceUtils.Reflective

public BTraceUtils.Reflective()
Method Detail

classOf

public static java.lang.Class classOf(java.lang.Object obj)
Returns the runtime class of the given Object.

Parameters:
obj - the Object whose Class is returned
Returns:
the Class object of given object

declaringClass

public static java.lang.Class declaringClass(java.lang.reflect.Field field)
Returns the Class object representing the class or interface that declares the field represented by the given Field object.

Parameters:
field - whose declaring Class is returned

name

public static java.lang.String name(java.lang.Class clazz)
Returns the name of the given Class object.


name

public static java.lang.String name(java.lang.reflect.Field field)
Returns the name of the Field object.

Parameters:
field - Field for which name is returned
Returns:
name of the given field

type

public static java.lang.Class type(java.lang.reflect.Field field)
Returns the type of the Field object.

Parameters:
field - Field for which type is returned
Returns:
type of the given field

accessFlags

public static int accessFlags(java.lang.Class clazz)
Returns the access flags of the given Class.


accessFlags

public static int accessFlags(java.lang.reflect.Field field)
Returns the access flags of the given Field.


contextClassLoader

public static java.lang.ClassLoader contextClassLoader()
Returns the current context class loader


classForName

public static java.lang.Class classForName(java.lang.String name)
Returns Class object for given class name.


classForName

public static java.lang.Class classForName(java.lang.String name,
                                           java.lang.ClassLoader cl)
Returns the Class for the given class name using the given class loader.


isAssignableFrom

public static boolean isAssignableFrom(java.lang.Class<?> a,
                                       java.lang.Class<?> b)
Determines if the class or interface represented by the first Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the second Class parameter. It returns true if so; otherwise it returns false.


isInstance

public static boolean isInstance(java.lang.Class clazz,
                                 java.lang.Object obj)
Determines if the specified Object is assignment-compatible with the object represented by the specified Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

Parameters:
clazz - the class that is checked.
obj - the object to check.
Returns:
true if obj is an instance of the given class.

getSuperclass

public static java.lang.Class getSuperclass(java.lang.Class clazz)
Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by the given Class. If the given Class represents either the Object class, an interface, a primitive type, or void, then null is returned. If the given object represents an array class then the Class object representing the Object class is returned.

Parameters:
clazz - the Class whose super class is returned.
Returns:
the superclass of the class represented by the given object.

isInterface

public static boolean isInterface(java.lang.Class clazz)
Determines if the specified Class object represents an interface type.

Parameters:
clazz - the Class object to check.
Returns:
true if the Class represents an interface; false otherwise.

isArray

public static boolean isArray(java.lang.Class clazz)
Determines if the given Class object represents an array class.

Parameters:
clazz - Class object to check.
Returns:
true if the given object represents an array class; false otherwise.

isPrimitive

public static boolean isPrimitive(java.lang.Class clazz)
Returns whether the given Class represent primitive type or not.


getComponentType

public static java.lang.Class getComponentType(java.lang.Class clazz)
returns component type of an array Class.


field

public static java.lang.reflect.Field field(java.lang.Class clazz,
                                            java.lang.String name,
                                            boolean throwException)
Returns a Field object that reflects the specified declared field of the class or interface represented by the given Class object. The name parameter is a String that specifies the simple name of the desired field. Returns null on not finding field if throwException parameter is false. Else throws a RuntimeException when field is not found.

Parameters:
clazz - Class whose field is returned
name - the name of the field
throwException - whether to throw exception on failing to find field or not
Returns:
the Field object for the specified field in this class

field

public static java.lang.reflect.Field field(java.lang.Class clazz,
                                            java.lang.String name)
Returns a Field object that reflects the specified declared field of the class or interface represented by the given Class object. The name parameter is a String that specifies the simple name of the desired field. Throws a RuntimeException when field is not found.

Parameters:
clazz - Class whose field is returned
name - the name of the field
Returns:
the Field object for the specified field in this class

field

public static java.lang.reflect.Field field(java.lang.String clazz,
                                            java.lang.String name,
                                            boolean throwException)
Returns a Field object that reflects the specified declared field of the class or interface represented by the given Class object. The name parameter is a String that specifies the simple name of the desired field. Returns null on not finding field if throwException parameter is false. Else throws a RuntimeException when field is not found.

Parameters:
clazz - Class whose field is returned
name - the name of the field
throwException - whether to throw exception on failing to find field or not
Returns:
the Field object for the specified field in this class

field

public static java.lang.reflect.Field field(java.lang.String clazz,
                                            java.lang.String name)
Returns a Field object that reflects the specified declared field of the class or interface represented by the given Class object. The name parameter is a String that specifies the simple name of the desired field. Throws a RuntimeException when field is not found.

Parameters:
clazz - Class whose field is returned
name - the name of the field
Returns:
the Field object for the specified field in this class

getByte

public static byte getByte(java.lang.reflect.Field field)
Gets the value of a static byte field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the byte field

getByte

public static byte getByte(java.lang.reflect.Field field,
                           java.lang.Object obj)
Gets the value of an instance byte field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the byte value from
Returns:
the value of the byte field

getShort

public static short getShort(java.lang.reflect.Field field)
Gets the value of a static short field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the short field

getShort

public static short getShort(java.lang.reflect.Field field,
                             java.lang.Object obj)
Gets the value of an instance short field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the short value from
Returns:
the value of the short field

getInt

public static int getInt(java.lang.reflect.Field field)
Gets the value of a static int field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the int field

getInt

public static int getInt(java.lang.reflect.Field field,
                         java.lang.Object obj)
Gets the value of an instance int field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the int value from
Returns:
the value of the int field

getLong

public static long getLong(java.lang.reflect.Field field)
Gets the value of a static long field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the long field

getLong

public static long getLong(java.lang.reflect.Field field,
                           java.lang.Object obj)
Gets the value of an instance long field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the long value from
Returns:
the value of the long field

getFloat

public static float getFloat(java.lang.reflect.Field field)
Gets the value of a static float field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the float field

getFloat

public static float getFloat(java.lang.reflect.Field field,
                             java.lang.Object obj)
Gets the value of an instance float field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the float value from
Returns:
the value of the float field

getDouble

public static double getDouble(java.lang.reflect.Field field)
Gets the value of a static double field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the double field

getDouble

public static double getDouble(java.lang.reflect.Field field,
                               java.lang.Object obj)
Gets the value of an instance double field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the double value from
Returns:
the value of the double field

getBoolean

public static boolean getBoolean(java.lang.reflect.Field field)
Gets the value of a static boolean field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the boolean field

getBoolean

public static boolean getBoolean(java.lang.reflect.Field field,
                                 java.lang.Object obj)
Gets the value of an instance boolean field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the boolean value from
Returns:
the value of the boolean field

getChar

public static char getChar(java.lang.reflect.Field field)
Gets the value of a static char field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the char field

getChar

public static char getChar(java.lang.reflect.Field field,
                           java.lang.Object obj)
Gets the value of an instance char field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the char value from
Returns:
the value of the char field

get

public static java.lang.Object get(java.lang.reflect.Field field)
Gets the value of a static reference field.

Parameters:
field - Field object whose value is returned.
Returns:
the value of the reference field

get

public static java.lang.Object get(java.lang.reflect.Field field,
                                   java.lang.Object obj)
Gets the value of an instance reference field.

Parameters:
field - Field object whose value is returned.
obj - the object to extract the reference value from
Returns:
the value of the reference field

printFields

public static void printFields(java.lang.Object obj)
Print all instance fields of an object as name-value pairs. Includes the inherited fields as well.

Parameters:
obj - Object whose fields are printed.

printFields

public static void printFields(java.lang.Object obj,
                               boolean classNamePrefix)
Print all instance fields of an object as name-value pairs. Includes the inherited fields as well. Optionally, prints name of the declaring class before each field - so that if same named field in super class chain may be disambiguated.

Parameters:
obj - Object whose fields are printed.
classNamePrefix - flag to tell whether to prefix field names names by class name or not.

printStaticFields

public static void printStaticFields(java.lang.Class clazz)
Print all static fields of the class as name-value pairs. Includes the inherited fields as well.

Parameters:
clazz - Class whose static fields are printed.

printStaticFields

public static void printStaticFields(java.lang.Class clazz,
                                     boolean classNamePrefix)
Print all static fields of the class as name-value pairs. Includes the inherited fields as well. Optionally, prints name of the declaring class before each field - so that if same named field in super class chain may be disambigated.

Parameters:
clazz - Class whose static fields are printed.
classNamePrefix - flag to tell whether to prefix field names names by class name or not.