public static class BTraceUtils.Reflective
extends java.lang.Object
Constructor and Description |
---|
Reflective() |
Modifier and Type | Method and Description |
---|---|
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.
|
public static java.lang.Class classOf(java.lang.Object obj)
obj
- the Object whose Class is returnedpublic static java.lang.Class declaringClass(java.lang.reflect.Field field)
field
- whose declaring Class is returnedpublic static java.lang.String name(java.lang.Class clazz)
public static java.lang.String name(java.lang.reflect.Field field)
field
- Field for which name is returnedpublic static java.lang.Class type(java.lang.reflect.Field field)
field
- Field for which type is returnedpublic static int accessFlags(java.lang.Class clazz)
public static int accessFlags(java.lang.reflect.Field field)
public static java.lang.ClassLoader contextClassLoader()
public static java.lang.Class classForName(java.lang.String name)
public static java.lang.Class classForName(java.lang.String name, java.lang.ClassLoader cl)
public static boolean isAssignableFrom(java.lang.Class<?> a, java.lang.Class<?> b)
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
.public static boolean isInstance(java.lang.Class clazz, java.lang.Object obj)
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.clazz
- the class that is checked.obj
- the object to check.obj
is an instance of the given class.public static java.lang.Class getSuperclass(java.lang.Class clazz)
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.clazz
- the Class whose super class is returned.public static boolean isInterface(java.lang.Class clazz)
Class
object represents an
interface type.clazz
- the Class object to check.true
if the Class represents an interface;
false
otherwise.public static boolean isArray(java.lang.Class clazz)
Class
object represents an array class.clazz
- Class object to check.true
if the given object represents an array class;
false
otherwise.public static boolean isPrimitive(java.lang.Class clazz)
public static java.lang.Class getComponentType(java.lang.Class clazz)
public static java.lang.reflect.Field field(java.lang.Class clazz, java.lang.String name, boolean throwException)
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.clazz
- Class whose field is returnedname
- the name of the fieldthrowException
- whether to throw exception on failing to find field or notField
object for the specified field in this
classpublic static java.lang.reflect.Field field(java.lang.Class clazz, java.lang.String name)
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.clazz
- Class whose field is returnedname
- the name of the fieldField
object for the specified field in this
classpublic static java.lang.reflect.Field field(java.lang.String clazz, java.lang.String name, boolean throwException)
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.clazz
- Class whose field is returnedname
- the name of the fieldthrowException
- whether to throw exception on failing to find field or notField
object for the specified field in this
classpublic static java.lang.reflect.Field field(java.lang.String clazz, java.lang.String name)
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.clazz
- Class whose field is returnedname
- the name of the fieldField
object for the specified field in this
classpublic static byte getByte(java.lang.reflect.Field field)
byte
field.field
- Field object whose value is returned.byte
fieldpublic static byte getByte(java.lang.reflect.Field field, java.lang.Object obj)
byte
field.field
- Field object whose value is returned.obj
- the object to extract the byte
value
frombyte
fieldpublic static short getShort(java.lang.reflect.Field field)
short
field.field
- Field object whose value is returned.short
fieldpublic static short getShort(java.lang.reflect.Field field, java.lang.Object obj)
short
field.field
- Field object whose value is returned.obj
- the object to extract the short
value
fromshort
fieldpublic static int getInt(java.lang.reflect.Field field)
int
field.field
- Field object whose value is returned.int
fieldpublic static int getInt(java.lang.reflect.Field field, java.lang.Object obj)
int
field.field
- Field object whose value is returned.obj
- the object to extract the int
value
fromint
fieldpublic static long getLong(java.lang.reflect.Field field)
long
field.field
- Field object whose value is returned.long
fieldpublic static long getLong(java.lang.reflect.Field field, java.lang.Object obj)
long
field.field
- Field object whose value is returned.obj
- the object to extract the long
value
fromlong
fieldpublic static float getFloat(java.lang.reflect.Field field)
float
field.field
- Field object whose value is returned.float
fieldpublic static float getFloat(java.lang.reflect.Field field, java.lang.Object obj)
float
field.field
- Field object whose value is returned.obj
- the object to extract the float
value
fromfloat
fieldpublic static double getDouble(java.lang.reflect.Field field)
double
field.field
- Field object whose value is returned.double
fieldpublic static double getDouble(java.lang.reflect.Field field, java.lang.Object obj)
double
field.field
- Field object whose value is returned.obj
- the object to extract the double
value
fromdouble
fieldpublic static boolean getBoolean(java.lang.reflect.Field field)
boolean
field.field
- Field object whose value is returned.boolean
fieldpublic static boolean getBoolean(java.lang.reflect.Field field, java.lang.Object obj)
boolean
field.field
- Field object whose value is returned.obj
- the object to extract the boolean
value
fromboolean
fieldpublic static char getChar(java.lang.reflect.Field field)
char
field.field
- Field object whose value is returned.char
fieldpublic static char getChar(java.lang.reflect.Field field, java.lang.Object obj)
char
field.field
- Field object whose value is returned.obj
- the object to extract the char
value
fromchar
fieldpublic static java.lang.Object get(java.lang.reflect.Field field)
field
- Field object whose value is returned.public static java.lang.Object get(java.lang.reflect.Field field, java.lang.Object obj)
field
- Field object whose value is returned.obj
- the object to extract the reference value
frompublic static void printFields(java.lang.Object obj)
obj
- Object whose fields are printed.public static void printFields(java.lang.Object obj, boolean classNamePrefix)
obj
- Object whose fields are printed.classNamePrefix
- flag to tell whether to prefix field names
names by class name or not.public static void printStaticFields(java.lang.Class clazz)
clazz
- Class whose static fields are printed.public static void printStaticFields(java.lang.Class clazz, boolean classNamePrefix)
clazz
- Class whose static fields are printed.classNamePrefix
- flag to tell whether to prefix field names
names by class name or not.