consciouscode.util
Class ReflectionUtils

java.lang.Object
  extended by consciouscode.util.ReflectionUtils

public class ReflectionUtils
extends Object

Utility methods for simplifying reflective operations.


Field Summary
static Class<?>[] NO_PARAMETERS
           
 
Constructor Summary
ReflectionUtils()
           
 
Method Summary
static boolean constructorIsApplicable(Constructor<?> ctor, Class<?>[] paramTypes)
           
static String describeParameters(Class<?>[] parameterTypes)
           
static void describeParameters(StringBuilder buffer, Class<?>... parameterTypes)
           
static String describeType(Class<?> type)
          Ensures a friendly representation of a Java type.
static void describeType(StringBuilder buffer, Class<?> type)
          Renders a friendly representation of a Java type into a buffer.
static String describeTypeOfValue(Object value)
          Ensures a visible representation of a value.
static void describeTypeOfValue(StringBuilder buffer, Object value)
          Renders a friendly representation of a value's type into a buffer.
static void ensureNoCheckedExceptions(Method method)
          Checks that a method throws no checked exception.
static void ensurePublic(Method method)
          Check that a method is public.
static Method getMethod(Class<?> targetClass, String methodName)
          Gets the reflection of a specified no-argument method.
static Method getMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes)
          Get the reflection of a specified method.
static Object invokeSafeMethod(Object object, Method method)
          Invoke a no-argument method that is known to be safe.
static Object invokeSafeMethod(Object object, Method method, Object[] arguments)
          Invokes a method that is known to be safe.
static boolean methodIsApplicable(Method method, Class<?>[] paramTypes)
           
static boolean parametersAreApplicable(Class<?>[] declaredParamTypes, Class<?>[] actualParamTypes)
          Determines whether certain actual parameter types are applicable to certain declared parameter types.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_PARAMETERS

public static final Class<?>[] NO_PARAMETERS
Constructor Detail

ReflectionUtils

public ReflectionUtils()
Method Detail

describeType

public static String describeType(Class<?> type)
Ensures a friendly representation of a Java type. The result is similar to Class.getName(), but without its clumsy internal notation for arrays: instead of (for example) the string "[B" this method returns the string "byte[]".

Parameters:
type - the type to describe. If null, this method returns the string "null".
Returns:
a non-empty String.

describeType

public static void describeType(StringBuilder buffer,
                                Class<?> type)
Renders a friendly representation of a Java type into a buffer. The result is similar to Class.getName(), but without its clumsy internal notation for arrays: instead of (for example) the string "[B" this method appends the string "byte[]".

Parameters:
buffer - must not be null.
type - the type to describe. If null, this method appends the string "null".

describeTypeOfValue

public static String describeTypeOfValue(Object value)
Ensures a visible representation of a value. If the value is null, returns the string "null", otherwise passes the object's class to describeType(Class).

Parameters:
value - may be null.
Returns:
a non-empty String.
See Also:
describeType(Class)

describeTypeOfValue

public static void describeTypeOfValue(StringBuilder buffer,
                                       Object value)
Renders a friendly representation of a value's type into a buffer. If the value is null, appends the string "null", otherwise appends the object's type as per describeType(Class).

Parameters:
buffer - must not be null.
value - may be null.
See Also:
describeType(Class)

getMethod

public static Method getMethod(Class<?> targetClass,
                               String methodName,
                               Class<?>[] parameterTypes)
Get the reflection of a specified method. Note that the result is not guaranteed to be public or accessible.

Throws:
IllegalArgumentException - if the requested method does not exist.

describeParameters

public static void describeParameters(StringBuilder buffer,
                                      Class<?>... parameterTypes)

describeParameters

public static String describeParameters(Class<?>[] parameterTypes)

getMethod

public static Method getMethod(Class<?> targetClass,
                               String methodName)
Gets the reflection of a specified no-argument method. Note that the result is not guaranteed to be public or accessible. This is a convenience overload of getMethod(Class, String, Class[])

Throws:
IllegalArgumentException - if the requested method does not exist.

ensureNoCheckedExceptions

public static void ensureNoCheckedExceptions(Method method)
Checks that a method throws no checked exception.

Throws:
IllegalArgumentException - if method throws an exception that must be declared.

ensurePublic

public static void ensurePublic(Method method)
Check that a method is public.

Throws:
IllegalArgumentException - if method is not public.

constructorIsApplicable

public static boolean constructorIsApplicable(Constructor<?> ctor,
                                              Class<?>[] paramTypes)

methodIsApplicable

public static boolean methodIsApplicable(Method method,
                                         Class<?>[] paramTypes)

parametersAreApplicable

public static boolean parametersAreApplicable(Class<?>[] declaredParamTypes,
                                              Class<?>[] actualParamTypes)
Determines whether certain actual parameter types are applicable to certain declared parameter types. In other words, can a method with this declared signature be invoked with parameters having these actual types?

It would be nice to use ClassUtils from Apache Commons Lang, but that doesn't seem to handle boxing/unboxing conversions.


invokeSafeMethod

public static Object invokeSafeMethod(Object object,
                                      Method method,
                                      Object[] arguments)
Invokes a method that is known to be safe. A "safe" method is one that is public and that throws no unchecked exceptions. This method will propagate any unchecked exceptions (that is, instances of subclasses of RuntimeException or Error) that are raised by the method invocation.

Throws:
RuntimeException - if the method is not accessible, or if it throws a checked exception.

invokeSafeMethod

public static Object invokeSafeMethod(Object object,
                                      Method method)
Invoke a no-argument method that is known to be safe. This is a convenience overload of invokeSafeMethod(Object, Method, Object[])



Copyright © 2001–2012 Todd V. Jonker. All Rights Reserved.