public class ReflectUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected static java.util.Map<java.lang.Class<?>,java.lang.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 java.util.Map<java.lang.Class<?>,java.lang.Object> |
primitiveDefaults
Holds a map of primitive type to the default value for that primitive type.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.reflect.Method |
findAccessibleMethod(java.lang.reflect.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 java.lang.Class |
findClass(java.lang.String name)
Utility method used to load a class.
|
static java.lang.reflect.Type[] |
getActualTypeArguments(java.lang.Class<?> clazz,
java.lang.Class<?> targetType)
Returns an array of Type objects representing the actual type arguments
to targetType used by clazz.
|
static java.lang.Object |
getDefaultValue(java.lang.Class<?> clazz)
Returns an appropriate default value for the class supplied.
|
static java.lang.reflect.Field |
getField(java.lang.Class<?> clazz,
java.lang.String property)
Looks for an instance (i.e.
|
static java.util.Collection<java.lang.reflect.Field> |
getFields(java.lang.Class<?> clazz)
Fetches all fields of all access types from the supplied class and super classes.
|
static java.util.Set<java.lang.Class<?>> |
getImplementedInterfaces(java.lang.Class<?> clazz)
Returns a set of all interfaces implemented by class supplied.
|
static java.lang.Class<?> |
getImplementingClass(java.lang.Class<?> iface)
Looks up the default implementing type for the supplied interface.
|
static <T> T |
getInterfaceInstance(java.lang.Class<T> interfaceType)
Attempts to determine an implementing class for the interface provided and instantiate
it using a default constructor.
|
static java.util.Collection<java.lang.reflect.Method> |
getMethods(java.lang.Class<?> clazz)
Fetches all methods of all access types from the supplied class and super
classes.
|
static java.beans.PropertyDescriptor |
getPropertyDescriptor(java.lang.Class<?> clazz,
java.lang.String property)
Fetches the property descriptor for the named property of the supplied class.
|
static java.beans.PropertyDescriptor[] |
getPropertyDescriptors(java.lang.Class<?> clazz)
Get the
PropertyDescriptor s for a bean class. |
static java.lang.reflect.Method |
resolveBridgedReadMethod(java.beans.PropertyDescriptor pd)
Locate and return the bridged read method for a bean property.
|
static java.lang.reflect.Method |
resolveBridgedWriteMethod(java.beans.PropertyDescriptor pd)
Locate and return the bridged write method for a bean property.
|
static java.lang.Class<?> |
resolvePropertyType(java.beans.PropertyDescriptor pd)
Under normal circumstances, a property's getter will return exactly the same type as its
setter accepts as a parameter.
|
static java.lang.String |
toString(java.lang.annotation.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.
|
protected static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> interfaceImplementations
protected static final java.util.Map<java.lang.Class<?>,java.lang.Object> primitiveDefaults
public static java.lang.Class<?> getImplementingClass(java.lang.Class<?> iface)
iface
- an interface for which an implementing class is neededpublic static <T> T getInterfaceInstance(java.lang.Class<T> interfaceType) throws java.lang.InstantiationException, java.lang.IllegalAccessException
interfaceType
- an interface (or abstract class) to make an instance ofjava.lang.InstantiationException
- if no implementation type has been configuredjava.lang.IllegalAccessException
- if thrown by the JVM during class instantiationpublic static java.lang.Class findClass(java.lang.String name) throws java.lang.ClassNotFoundException
Thread.currentThread().getContextClassLoader().loadClass(String)
.name
- the fully qualified (binary) name of the class to find or loadjava.lang.ClassNotFoundException
- if the class cannot be loadedpublic static java.lang.String toString(java.lang.annotation.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 Stringpublic static java.util.Collection<java.lang.reflect.Method> getMethods(java.lang.Class<?> clazz)
clazz
- the class to inspectpublic static java.util.Collection<java.lang.reflect.Field> getFields(java.lang.Class<?> clazz)
clazz
- the class to inspectpublic static java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.Class<?> clazz, java.lang.String property)
clazz
- the class who's properties to examineproperty
- the String name of the property to look forpublic static java.lang.reflect.Method findAccessibleMethod(java.lang.reflect.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 accessiblepublic static java.lang.reflect.Field getField(java.lang.Class<?> clazz, java.lang.String property)
clazz
- the clazz who's fields to examineproperty
- the name of the property/field to look forpublic static java.lang.Object getDefaultValue(java.lang.Class<?> clazz)
clazz
- the class for which to find the default valuepublic static java.util.Set<java.lang.Class<?>> getImplementedInterfaces(java.lang.Class<?> clazz)
clazz
- public static java.lang.reflect.Type[] getActualTypeArguments(java.lang.Class<?> clazz, java.lang.Class<?> targetType)
clazz
- the implementing class (or subclass)targetType
- the implemented generic class or interfacepublic static java.beans.PropertyDescriptor[] getPropertyDescriptors(java.lang.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 introspectBeanInfo.getPropertyDescriptors()
.public static java.lang.reflect.Method resolveBridgedReadMethod(java.beans.PropertyDescriptor pd)
pd
- The bean property descriptorpublic static java.lang.reflect.Method resolveBridgedWriteMethod(java.beans.PropertyDescriptor pd)
pd
- The bean property descriptorpublic static java.lang.Class<?> resolvePropertyType(java.beans.PropertyDescriptor pd)
pd
- The property descriptor? Copyright 2005-2006, Stripes Development Team.