net.sourceforge.domian.util
Class ReflectionUtils

java.lang.Object
  extended by net.sourceforge.domian.util.ReflectionUtils

public final class ReflectionUtils
extends java.lang.Object

Generic static convenience methods dealing with reflection stuff.

Since:
0.1
Author:
Eirik Torske

Field Summary
static java.lang.Boolean DO_COPY_ENTITIES
          Often there is no real necessity to copy entity objects.
static java.lang.Boolean DO_COPY_OBJECTS
          This flag is used by the cloneOrDeepCopyIfNotImmutable() method.
static int RECURSIVE_COPYING_DEPTH_TRESHOLD
          Recursive copying of objects cannot go on forever!
 
Constructor Summary
ReflectionUtils()
           
 
Method Summary
static java.lang.Boolean canCast(java.lang.Class fromClass, java.lang.Class toClass)
           
static java.lang.Boolean canCast(java.lang.reflect.Type fromType, java.lang.reflect.Type toType)
           
static java.lang.Boolean canCastAtLeastOneWay(java.lang.reflect.Type type1, java.lang.reflect.Type type2)
           
static java.lang.Boolean canCastFrom_To(java.lang.Class fromClass, java.lang.Class toClass)
           
static java.lang.Boolean canCastFrom_To(java.lang.reflect.Type fromType, java.lang.reflect.Type toType)
           
static
<T> T
cloneOrDeepCopyIfNotImmutable(T object)
           
static
<T> T
cloneOrDeepCopyIfNotImmutable(T object, java.lang.Boolean doCopyEntities)
           
static java.util.Map<java.lang.String,java.lang.reflect.AccessibleObject> getAllAccessibleObjectsFrom(java.lang.Object object)
           
static java.lang.Class<?> getClass(java.lang.reflect.Type type)
           
static
<T> java.lang.reflect.Field
getFieldByName(java.lang.String fieldName, java.lang.Class<T> declaringType)
           
static
<T> java.lang.reflect.Method
getMethodByName(java.lang.String methodName, java.lang.Class[] parameterArray, java.lang.Class<T> declaringType)
           
static
<T> java.lang.reflect.Method
getMethodByName(java.lang.String methodName, java.lang.Class<T> declaringType)
           
static
<T> java.lang.reflect.Method
getMethodByNameWithPossiblePrefix(java.lang.String methodName, java.lang.Class<T> declaringType, java.util.List<java.lang.String> possibleMethodPrefixes)
          If no method is found with the given name, each of the prefixes in the given list will be added until a valid method are found, or no more prefixes are left to try.
static
<T> java.util.List<java.lang.Class<?>>
getTypeArguments(java.lang.Class<T> baseClass, java.lang.Class<? extends T> subclass)
          Gets the actual type arguments a subclass has used to extend a generic base class.
static
<T> java.lang.Object
invokeMethod(T object, java.lang.String methodName, java.lang.Object[] parameterArray)
           
static java.lang.Boolean isDate(java.lang.Object candidateObject)
           
static java.lang.Boolean isDecimalNumber(java.lang.Object number)
           
static
<T> java.lang.Boolean
isEntity(T object)
          An object is regarded as an entity if and only if:
  • the object is an instance of AbstractEntity The reason instances have to be of type AbstractEntity, and not just Entity, is simply that for Entity objects there is no assurance that the semantics of consistent/unchanging equals/hashCode methods are addressed.
  • static
    <T> T
    replicate(T object)
              Replicates the given object.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    DO_COPY_OBJECTS

    public static java.lang.Boolean DO_COPY_OBJECTS
    This flag is used by the cloneOrDeepCopyIfNotImmutable() method.

    The default value is true. If object copying is not wanted, override it by setting this flag to false. Such an operation is completely sound, given the premis of objects in use being either immutable value objets, or entity objects having equals/hashCode methods not affected by mutable state.

    WARNING! By setting this field to false, certain specifications turn mutable, e.g. the ValueBoundSpecification. This is because the state of the object given to the specification (the value that is) may be altered.

    WARNING! For now, this is simply a static non-thread-safe JVM wide flag.


    DO_COPY_ENTITIES

    public static java.lang.Boolean DO_COPY_ENTITIES
    Often there is no real necessity to copy entity objects. Entity objects are inherently mutable, but all state involved in the equals/hashCode methods should not be allowed to change in any way. In cases where only the equals/hashCode methods are to be invoked, copying the object is not really needed.

    This flag is used by the cloneOrDeepCopyIfNotImmutable() method. Which objects that are regarded as entities is specified by the isEntity() method

    The default value is false. If entity copying is wanted, override it by setting this flag to true.

    WARNING! For now, this is simply a static non-thread-safe JVM wide flag.

    See Also:
    ReflectionUtils.isEntity(T)

    RECURSIVE_COPYING_DEPTH_TRESHOLD

    public static int RECURSIVE_COPYING_DEPTH_TRESHOLD
    Recursive copying of objects cannot go on forever! The default treshold when to stop recursively copying fields is set to 5. This means; all kinds of objects given as a value to ValueBoundSpecifications should not include more than five levels of referencing in its equals() or compareTo() method. (If you are doing such a thing, you should seriously consider refactoring anyway!)

    This flag is used by the cloneOrDeepCopyIfNotImmutable() method.

    WARNING! For now, this is simply a static non-thread-safe JVM wide flag.

    Constructor Detail

    ReflectionUtils

    public ReflectionUtils()
    Method Detail

    canCast

    public static java.lang.Boolean canCast(java.lang.Class fromClass,
                                            java.lang.Class toClass)
    Returns:
    true if the fromClass class parameter can be casted to the toClass class parameter

    canCast

    public static java.lang.Boolean canCast(java.lang.reflect.Type fromType,
                                            java.lang.reflect.Type toType)
    Returns:
    true if the fromType type parameter can be casted to the toType type parameter

    canCastFrom_To

    public static java.lang.Boolean canCastFrom_To(java.lang.Class fromClass,
                                                   java.lang.Class toClass)
    Returns:
    true if the fromClass class parameter can be casted to the toClass class parameter

    canCastFrom_To

    public static java.lang.Boolean canCastFrom_To(java.lang.reflect.Type fromType,
                                                   java.lang.reflect.Type toType)
    Returns:
    true if the fromType type parameter can be casted to the toType type parameter

    canCastAtLeastOneWay

    public static java.lang.Boolean canCastAtLeastOneWay(java.lang.reflect.Type type1,
                                                         java.lang.reflect.Type type2)
    Returns:
    true if it is possible to cast on way or another between the two type parameters

    isDecimalNumber

    public static java.lang.Boolean isDecimalNumber(java.lang.Object number)
    Returns:
    true if given number is a decimal number/floating point number

    isDate

    public static java.lang.Boolean isDate(java.lang.Object candidateObject)

    getFieldByName

    public static <T> java.lang.reflect.Field getFieldByName(java.lang.String fieldName,
                                                             java.lang.Class<T> declaringType)
    Returns:
    the field object with given name and parent class, or null if no field is found

    getMethodByName

    public static <T> java.lang.reflect.Method getMethodByName(java.lang.String methodName,
                                                               java.lang.Class<T> declaringType)
    Returns:
    the method (with no formal parameters) with given name and parent class, or null if no method is found
    Since:
    0.3

    getMethodByName

    public static <T> java.lang.reflect.Method getMethodByName(java.lang.String methodName,
                                                               java.lang.Class[] parameterArray,
                                                               java.lang.Class<T> declaringType)
    Returns:
    the method with given name, parameter types, and parent class, or null if no method is found
    Since:
    0.3

    getMethodByNameWithPossiblePrefix

    public static <T> java.lang.reflect.Method getMethodByNameWithPossiblePrefix(java.lang.String methodName,
                                                                                 java.lang.Class<T> declaringType,
                                                                                 java.util.List<java.lang.String> possibleMethodPrefixes)
    If no method is found with the given name, each of the prefixes in the given list will be added until a valid method are found, or no more prefixes are left to try.

    Returns:
    the method with given prefix+name, and parent class, or null if no method is found
    Since:
    0.4

    invokeMethod

    public static <T> java.lang.Object invokeMethod(T object,
                                                    java.lang.String methodName,
                                                    java.lang.Object[] parameterArray)
    Since:
    0.3

    getAllAccessibleObjectsFrom

    public static java.util.Map<java.lang.String,java.lang.reflect.AccessibleObject> getAllAccessibleObjectsFrom(java.lang.Object object)
    Returns:
    Map of all fields, and methods (not constructors) in the given object, indexed by their names
    Since:
    0.3

    replicate

    public static <T> T replicate(T object)
    Replicates the given object. The given object is cloned if it is Cloneable. If not, it is deep-copied, except if the given object is regarded as immutable. Deep-copying follows the DO_COPY_OBJECTS, DO_COPY_ENTITIES, and RECURSIVE_COPYING_DEPTH_TRESHOLD settings.

    Parameters:
    object - object to replicate
    Returns:
    a replicated version, or the same object if it is regarded as immutable
    Throws:
    java.lang.SecurityException - if a security manager exists and if the caller does not have granted java.lang.reflect.ReflectPermission.suppressAccessChecks
    Since:
    0.4.1

    cloneOrDeepCopyIfNotImmutable

    public static <T> T cloneOrDeepCopyIfNotImmutable(T object)
    Parameters:
    object - object to clone or deep copy, if necessery
    Returns:
    a cloned version, deep copied version, or the same object if it is immutable
    Throws:
    java.lang.SecurityException - if a security manager exists and if the caller does not have granted java.lang.reflect.ReflectPermission.suppressAccessChecks
    Since:
    0.2.1

    cloneOrDeepCopyIfNotImmutable

    public static <T> T cloneOrDeepCopyIfNotImmutable(T object,
                                                      java.lang.Boolean doCopyEntities)
    Parameters:
    object - object to clone or deep copy, if necessery
    doCopyEntities - flag to indicate whether copying of entites are wanted or not
    Returns:
    a cloned version, deep copied version, or the same object if it is immutable
    Throws:
    java.lang.SecurityException - if a security manager exists and if the caller does not have granted java.lang.reflect.ReflectPermission.suppressAccessChecks
    Since:
    0.2.1

    isEntity

    public static <T> java.lang.Boolean isEntity(T object)
    An object is regarded as an entity if and only if: The reason instances have to be of type AbstractEntity, and not just Entity, is simply that for Entity objects there is no assurance that the semantics of consistent/unchanging equals/hashCode methods are addressed. For AbstractEntity objects on the other hand, there is.

    Returns:
    true if the given object is safely regarded by Domian as a real entity object
    Since:
    0.4

    getClass

    public static java.lang.Class<?> getClass(java.lang.reflect.Type type)
    Parameters:
    type - the type
    Returns:
    the underlying class for the given type, or null if the type is a variable type
    Since:
    0.4
    See Also:
    Reflecting generics, by Ian Robertson

    getTypeArguments

    public static <T> java.util.List<java.lang.Class<?>> getTypeArguments(java.lang.Class<T> baseClass,
                                                                          java.lang.Class<? extends T> subclass)
    Gets the actual type arguments a subclass has used to extend a generic base class.

    Parameters:
    baseClass - the base class
    subclass - the subclass
    Returns:
    a list of the raw classes for the actual type arguments.
    Since:
    0.4
    See Also:
    Reflecting generics, by Ian Robertson


    Copyright © 2006-2010. All Rights Reserved.