|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.stripes.util.ReflectUtil
public class ReflectUtil
Common utilty methods that are useful when working with reflection.
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
|
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 PropertyDescriptor s 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 |
---|
protected static final Map<Class<?>,Class<?>> interfaceImplementations
protected static final Map<Class<?>,Object> primitiveDefaults
Method Detail |
---|
public static Class<?> getImplementingClass(Class<?> iface)
iface
- an interface for which an implementing class is needed
public static <T> T getInterfaceInstance(Class<T> interfaceType) throws InstantiationException, IllegalAccessException
interfaceType
- an interface (or abstract class) to make an instance of
InstantiationException
- if no implementation type has been configured
IllegalAccessException
- if thrown by the JVM during class instantiationpublic static Class findClass(String name) throws ClassNotFoundException
Thread.currentThread().getContextClassLoader().loadClass(String)
.
name
- the fully qualified (binary) name of the class to find or load
ClassNotFoundException
- if the class cannot be loadedpublic 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:
If, for some unforseen reason, an exception is thrown within this method it will be
caught and the return value will be ann.toString()
.
ann
- the annotation to convert to a human readable String
public static Collection<Method> getMethods(Class<?> clazz)
clazz
- the class to inspect
public static Collection<Field> getFields(Class<?> clazz)
clazz
- the class to inspect
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String property)
clazz
- the class who's properties to examineproperty
- the String name of the property to look for
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.
m
- a method that may or may not be accessible
public static Field getField(Class<?> clazz, String property)
clazz
- the clazz who's fields to examineproperty
- the name of the property/field to look for
public static Object getDefaultValue(Class<?> clazz)
clazz
- the class for which to find the default value
public static Set<Class<?>> getImplementedInterfaces(Class<?> clazz)
clazz
-
public static Type[] getActualTypeArguments(Class<?> clazz, Class<?> targetType)
clazz
- the implementing class (or subclass)targetType
- the implemented generic class or interface
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz)
PropertyDescriptor
s 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.
clazz
- The bean class to introspect
BeanInfo.getPropertyDescriptors()
.public static Method resolveBridgedReadMethod(PropertyDescriptor pd)
pd
- The bean property descriptor
public static Method resolveBridgedWriteMethod(PropertyDescriptor pd)
pd
- The bean property descriptor
public static Class<?> resolvePropertyType(PropertyDescriptor pd)
pd
- The property descriptor
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |