net.sourceforge.stripes.util
Class ReflectUtil

java.lang.Object
  extended by net.sourceforge.stripes.util.ReflectUtil

public class ReflectUtil
extends Object

Common utilty methods that are useful when working with reflection.

Author:
Tim Fennell

Field Summary
protected static Map<Class<?>,Class<?>> interfaceImplementations
          Holds a map of commonly used interface types (mostly collections) to a class that implements the interface and will, by default, be instantiated when an instance of the interface is needed.
protected static Map<Class<?>,Object> primitiveDefaults
          Holds a map of primitive type to the default value for that primitive type.
 
Method Summary
static Method findAccessibleMethod(Method m)
          Attempts to find an accessible version of the method passed in, where accessible is defined as the method itself being public and the declaring class being public.
static Class findClass(String name)
          Utility method used to load a class.
static Type[] getActualTypeArguments(Class<?> clazz, Class<?> targetType)
          Returns an array of Type objects representing the actual type arguments to targetType used by clazz.
static Object getDefaultValue(Class<?> clazz)
          Returns an appropriate default value for the class supplied.
static Field getField(Class<?> clazz, String property)
          Looks for an instance (i.e.
static Collection<Field> getFields(Class<?> clazz)
          Fetches all fields of all access types from the supplied class and super classes.
static Set<Class<?>> getImplementedInterfaces(Class<?> clazz)
          Returns a set of all interfaces implemented by class supplied.
static Class<?> getImplementingClass(Class<?> iface)
          Looks up the default implementing type for the supplied interface.
static
<T> T
getInterfaceInstance(Class<T> interfaceType)
          Attempts to determine an implementing class for the interface provided and instantiate it using a default constructor.
static Collection<Method> getMethods(Class<?> clazz)
          Fetches all methods of all access types from the supplied class and super classes.
static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String property)
          Fetches the property descriptor for the named property of the supplied class.
static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz)
          Get the PropertyDescriptors for a bean class.
static Method resolveBridgedReadMethod(PropertyDescriptor pd)
          Locate and return the bridged read method for a bean property.
static Method resolveBridgedWriteMethod(PropertyDescriptor pd)
          Locate and return the bridged write method for a bean property.
static Class<?> resolvePropertyType(PropertyDescriptor pd)
          Under normal circumstances, a property's getter will return exactly the same type as its setter accepts as a parameter.
static String toString(Annotation ann)
          A better (more concise) toString method for annotation types that yields a String that should look more like the actual usage of the annotation in a class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

interfaceImplementations

protected static final Map<Class<?>,Class<?>> interfaceImplementations
Holds a map of commonly used interface types (mostly collections) to a class that implements the interface and will, by default, be instantiated when an instance of the interface is needed.


primitiveDefaults

protected static final Map<Class<?>,Object> primitiveDefaults
Holds a map of primitive type to the default value for that primitive type. Isn't it odd that there's no way to get this programmatically from the Class objects?

Method Detail

getImplementingClass

public static Class<?> getImplementingClass(Class<?> iface)
Looks up the default implementing type for the supplied interface. This is done based on a static map of known common interface types and implementing classes.

Parameters:
iface - an interface for which an implementing class is needed
Returns:
a Class object representing the implementing type, or null if one is not found

getInterfaceInstance

public static <T> T getInterfaceInstance(Class<T> interfaceType)
                              throws InstantiationException,
                                     IllegalAccessException
Attempts to determine an implementing class for the interface provided and instantiate it using a default constructor.

Parameters:
interfaceType - an interface (or abstract class) to make an instance of
Returns:
an instance of the interface type supplied
Throws:
InstantiationException - if no implementation type has been configured
IllegalAccessException - if thrown by the JVM during class instantiation

findClass

public static Class findClass(String name)
                       throws ClassNotFoundException
Utility method used to load a class. Any time that Stripes needs to load of find a class by name it uses this method. As a result any time the classloading strategy needs to change it can be done in one place! Currently uses Thread.currentThread().getContextClassLoader().loadClass(String).

Parameters:
name - the fully qualified (binary) name of the class to find or load
Returns:
the Class object representing the class
Throws:
ClassNotFoundException - if the class cannot be loaded

toString

public static String toString(Annotation ann)

A better (more concise) toString method for annotation types that yields a String that should look more like the actual usage of the annotation in a class. The String produced is similar to that produced by calling toString() on the annotation directly, with the following differences:


getMethods

public static Collection<Method> getMethods(Class<?> clazz)
Fetches all methods of all access types from the supplied class and super classes. Methods that have been overridden in the inheritance hierarchy are only returned once, using the instance lowest down the hierarchy.

Parameters:
clazz - the class to inspect
Returns:
a collection of methods

getFields

public static Collection<Field> getFields(Class<?> clazz)
Fetches all fields of all access types from the supplied class and super classes.

Parameters:
clazz - the class to inspect
Returns:
a collection of fields

getPropertyDescriptor

public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz,
                                                       String property)
Fetches the property descriptor for the named property of the supplied class. To speed things up a cache is maintained of propertyName to PropertyDescriptor for each class used with this method. If there is no property with the specified name, returns null.

Parameters:
clazz - the class who's properties to examine
property - the String name of the property to look for
Returns:
the PropertyDescriptor or null if none is found with a matching name

findAccessibleMethod

public static Method findAccessibleMethod(Method m)

Attempts to find an accessible version of the method passed in, where accessible is defined as the method itself being public and the declaring class being public. Mostly useful as a workaround to the situation when PropertyDescriptor.getReadMethod() and/or PropertyDescriptor.getWriteMethod() returns methods that are not accessible (usually due to public implementations of interface methods in private classes).

Checks the method passed in and if it already meets these criteria it is returned immediately. In general this leads to very little performance overhead

If the method does not meet the criteria then the class' interfaces are scanned for a matching method. If one is not found, then the class' superclass hierarchy is searched. Finally, if no matching method can be found the original method is returned.

Parameters:
m - a method that may or may not be accessible
Returns:
either an accessible version of the same method, or the method passed in if an accessible version cannot be found

getField

public static Field getField(Class<?> clazz,
                             String property)
Looks for an instance (i.e. non-static) public field with the matching name and returns it if one exists. If no such field exists, returns null.

Parameters:
clazz - the clazz who's fields to examine
property - the name of the property/field to look for
Returns:
the Field object or null if no matching field exists

getDefaultValue

public static Object getDefaultValue(Class<?> clazz)
Returns an appropriate default value for the class supplied. Mirrors the defaults used when the JVM initializes instance variables.

Parameters:
clazz - the class for which to find the default value
Returns:
null for non-primitive types and an appropriate wrapper instance for primitives

getImplementedInterfaces

public static Set<Class<?>> getImplementedInterfaces(Class<?> clazz)
Returns a set of all interfaces implemented by class supplied. This includes all interfaces directly implemented by this class as well as those implemented by superclasses or interface superclasses.

Parameters:
clazz -
Returns:
all interfaces implemented by this class

getActualTypeArguments

public static Type[] getActualTypeArguments(Class<?> clazz,
                                            Class<?> targetType)
Returns an array of Type objects representing the actual type arguments to targetType used by clazz.

Parameters:
clazz - the implementing class (or subclass)
targetType - the implemented generic class or interface
Returns:
an array of Type objects or null

getPropertyDescriptors

public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz)
Get the PropertyDescriptors for a bean class. This is normally easy enough to do except that Java versions 6 and earlier have a bug that can return bridge methods for property getters and/or setters. That can mess up validation and binding and possibly other areas. This method accounts for that bug and attempts to work around it, ensuring the property descriptors contain the true getter and setter methods.

Parameters:
clazz - The bean class to introspect
Returns:
The property descriptors for the bean class, as returned by BeanInfo.getPropertyDescriptors().

resolveBridgedReadMethod

public static Method resolveBridgedReadMethod(PropertyDescriptor pd)
Locate and return the bridged read method for a bean property.

Parameters:
pd - The bean property descriptor
Returns:
The bridged method or the property descriptor's read method, if it is not a bridge method.

resolveBridgedWriteMethod

public static Method resolveBridgedWriteMethod(PropertyDescriptor pd)
Locate and return the bridged write method for a bean property.

Parameters:
pd - The bean property descriptor
Returns:
The bridged method or the property descriptor's write method, if it is not a bridge method.

resolvePropertyType

public static Class<?> resolvePropertyType(PropertyDescriptor pd)
Under normal circumstances, a property's getter will return exactly the same type as its setter accepts as a parameter. However, because we have to hack around the JVM bug dealing with bridge methods this might not always be the case. This method resolves the actual type of the property. In the case where the two types (return type and parameter type) are not identical, the property type is whichever of the two is lower in the class hierarchy.

Parameters:
pd - The property descriptor
Returns:
The type of the property


? Copyright 2005-2006, Stripes Development Team.