|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.util.ReflectUtil
public final class ReflectUtil
Provides static utility methods related to reflection.
This class is multithread safe: it is stateless.
Nested Class Summary | |
---|---|
static class |
ReflectUtil.FieldComparator
Imposes an ordering on Fields that is consistent with equals; see compare for details. |
static class |
ReflectUtil.UnitTest
See the Overview page of the project's javadocs for a general description of this unit test class. |
Field Summary | |
---|---|
private static Object[] |
noArgParameters
An empty Object[] that caches the parameter used by callLogError(Object, String) . |
private static Class[] |
noArgSignature
An empty Class[] that caches the signature used by callLogError(Object, String) . |
Constructor Summary | |
---|---|
private |
ReflectUtil()
This private constructor suppresses the default (public) constructor, ensuring non-instantiability. |
Method Summary | ||
---|---|---|
static Object |
callLogError(Object obj,
String methodName)
Returns . |
|
static Object |
callLogError(Object obj,
String methodName,
Class[] paramClasses,
Object[] paramValues)
Uses getMethod to find a method named methodName with a signature given by paramClasses on obj,
and then invokes that method on obj using paramValues and returns the result. |
|
static String |
diagnoseGetProblem(Throwable t)
Returns a useful diagnostic message for a Throwable generated when calling Field.get . |
|
static String |
fieldsDeclaredReport(Object obj)
Returns a String describing all of obj's declared fields (i.e. just those fields declared in the obj's class itself, whether static or instance, regardless of access level, but never including fields from superclasses/superinterfaces). |
|
static Class[] |
findSignature(Object[] arguments)
Returns a Class[] where each element is the Class of the corresponding Object in the arguments array. |
|
static Object |
get(Object obj,
String fieldName)
Accesses the value of the field of obj which is named fieldName. |
|
static
|
getEnumValues(E e)
Uses reflection to find and execute a static method named "values" that takes no arguments. |
|
static Method |
getMethod(Object obj,
String methodName,
Class[] paramClasses)
Uses reflection to find a method named methodName with a signature given by paramClasses on obj. |
|
static boolean |
isDefault(Field f)
Returns true if f has default (unnamed) access. |
|
static boolean |
isFinal(Field f)
Returns
|
|
static boolean |
isPrivate(Field f)
Returns
|
|
static boolean |
isProtected(Field f)
Returns
|
|
static boolean |
isPublic(Field f)
Returns
|
|
static boolean |
isStatic(Field f)
Returns
|
|
static void |
set(Object obj,
String fieldName,
Object value)
Mutates the value of the field of obj which is named fieldName. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final Class[] noArgSignature
callLogError(Object, String)
.
private static final Object[] noArgParameters
callLogError(Object, String)
.
Constructor Detail |
---|
private ReflectUtil()
Method Detail |
---|
public static <E extends Enum<E>> E[] getEnumValues(E e) throws RuntimeException
You can always easily get all the values of an enum if you know the specific class name at compile time.
For example, if you have an enum named Planet
, then your can call Planet.values()
.
The purpose of this method, however, is to get all the values of an enum when you do not know its specific classname,
but are working with a type parameter that merely says that it is an enum.
RuntimeException
- should actually be one of:
Class.getMethod
public static Object get(Object obj, String fieldName) throws Exception
The implementation here suppresses all Java access checks, which means that it can be used to return the values of, say, private fields. Warning: this check suppression fails with certain classes (e.g. security sensitive ones from the JDK).
Exception
- should actually be one of:
blank
Class.getDeclaredField
; Field.setAccessible
public static void set(Object obj, String fieldName, Object value) throws Exception
The implementation here suppresses all Java access checks, which means that it can be used to set the values of, say, private and/or final fields. Warning: this check suppression fails with certain classes (e.g. security sensitive ones from the JDK).
Exception
- should actually be one of:
blank
Class.getDeclaredField
; Field.setAccessible
public static String diagnoseGetProblem(Throwable t) throws IllegalArgumentException
Field.get
.
IllegalArgumentException
- if t is nullpublic static String fieldsDeclaredReport(Object obj) throws RuntimeException
Field.toString()
and ends with its value in obj.
The fields are ordered by ReflectUtil.FieldComparator
instance.
RuntimeException
- should actually be one of:
Class.getDeclaredField
; Field.setAccessible
public static Method getMethod(Object obj, String methodName, Class[] paramClasses) throws Exception
Contract: the result is never null, and is always marked as accessible by a call to setAccessible
(true).
Exception
- should actually be one of:
Class.getMethod
; Class.getDeclaredMethod
public static Object callLogError(Object obj, String methodName)
callLogError(Object, String, Class[], Object[])
(obj, methodName, noArgSignature
, noArgParameters
)
.
This is a convenience method for calling no-arg methods.
public static Object callLogError(Object obj, String methodName, Class[] paramClasses, Object[] paramValues)
getMethod
to find a method named methodName with a signature given by paramClasses on obj,
and then invokes that method on obj using paramValues and returns the result.
Contract: this method should never throw a Throwable.
Any Throwable that is raised is caught and logged robustly
to the default Logger
.
An example of such a Throwable is a NoSuchMethodException if obj does not actually have the specified method.
If such a Throwable is caught, the result of this method is null.
Warning: null could also be the normal result of the method invocation, so it is imposible to use that to determine if a problem happened.
Motivation: this method was originally written to support resource closing methods of objects inside finally blocks
(e.g. this method is used by StreamUtil.close(Object)
).
The issue here is that the finally block may need multiple such methods to be called,
so none should not throw a Throwable because that would stop subsequent resources from being closed.
Using this method allows each to be conveniently called with its own error handling,
as opposed to wrapping each method inside its own dedicated try-catch block.
public static boolean isPublic(Field f)
Modifier.isPublic
( f.Field.getModifiers()
)
.
public static boolean isProtected(Field f)
Modifier.isProtected
( f.Field.getModifiers()
)
.
public static boolean isDefault(Field f)
!isPublic
(f) && !isProtected
(f) && >!isPrivate
(f)
.
public static boolean isPrivate(Field f)
Modifier.isPrivate
( f.Field.getModifiers()
)
.
public static boolean isStatic(Field f)
Modifier.isStatic
( f.Field.getModifiers()
)
.
public static boolean isFinal(Field f)
Modifier.isFinal
( f.Field.getModifiers()
)
.
public static Class[] findSignature(Object[] arguments)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |