Top Description Inners Fields Constructors Methods
com.sun.faces.util

public final Class ReflectionUtils

extends Object
Class Inheritance
Static Imports
java.beans.Introspector.getBeanInfo, .PropertyEditorManager.findEditor

A set of utility methods to make working with Classes and Reflection a little easier.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ReflectionUtils.MetaData

MetaData contains lookup methods for Constructors and Methods of a particular Class.

Field Summary

Modifier and TypeField and Description
private static final Logger
private static final Map<ClassLoader, ConcurrentMap<String, ReflectionUtils.MetaData>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static synchronized void
clearCache(ClassLoader
the ClassLoader whose associated cache should be cleared
loader
)

Clears the cache for the specified ClassLoader.

public static Method

Returns:

a method if one is found, null otherwise
findMethod
(Object
the object in which the method is to be found
base
,
String
name of the method to be found
methodName
,
Object[]
the method parameters
params
)

Finds a method based on the method name, amount of parameters and limited typing, if necessary prefixed with "get".

private static ReflectionUtils.MetaData

Returns:

a MetaData object for the specified Class
getMetaData
(ClassLoader
ClassLoader
loader
,
Class<?>
the Class of interest
clazz
)

Return the MetaData for the specified Class.

private static ReflectionUtils.MetaData

Returns:

a MetaData object for the specified Class
getMetaData
(ClassLoader
ClassLoader
loader
,
String
the class of interest
className
)

Return the MetaData for the specified className.

public static synchronized void
public static <
The generic object type.
T
>
T

Returns:

an instance of the class denoted by className
instance
(String
fully qualified class name of the class for which an instance needs to be created
className
)

Creates an instance of a class with the given fully qualified class name.

public static <
The generic object type.
T
>
T

Returns:

an instance of the class as given by the clazz parameter
instance
(Class<T>
the Class object for which an instance needs to be created
clazz
)

Creates a new instance of the class represented by the given Class object

public static Class<?>

Returns:

the Class corresponding to className
lookupClass
(String
the class to look up
className
)

Obtain a Class instance based on the provided String name.

public static Constructor<?>

Returns:

a Constructor that can be invoked with the specified parameters
lookupConstructor
(Class<?>
the Class of interest
clazz
,
Class<?>...
the parameters for the constructor of the provided Class
params
)

Returns the Constructor appropriate to the specified Class and parameters.

public static Method

Returns:

a Method that can be invoked with the specified parameters
lookupMethod
(Object
the Object instance of interest
object
,
String
the name of the method
methodName
,
Class<?>...
the parameters for the specified method
params
)

Returns the Method appropriate to the specified object instance, method name, and parameters.

public static Method

Returns:

a Method that can be invoked with the specified parameters
lookupMethod
(Class<?>
the Class of interest
clazz
,
String
the name of the method
methodName
,
Class<?>...
the parameters for the specified method
params
)

Returns the Method appropriate to the specified Class, method name, and parameters.

public static Method

Returns:

a method suitable for obtaining the value of a JavaBeans property, or null if the property doesn't exist or can't be read.
lookupReadMethod
(String
the fully qualified class name
className
,
String
a JavaBeans property name
propertyName
)

public static Method

Returns:

a method suitable for setting a JavaBeans property, or null if the property doesn't exist or is readonly.
lookupWriteMethod
(String
the fully qualified class name
className
,
String
a JavaBeans property name
propertyName
)

public static Object

Returns:

a new instances of said class
newInstance
(String
the class of the object to instantiate
className
)

Constructs a new object instance based off the provided class name.

public static void
setProperties(Object
the object on which properties will be set
object
,
Map<String, Object>
the map containing properties and their values to be set on the object
propertiesToSet
)

Sets a collection of properties of a given object to the values associated with those properties.

public static void
setPropertiesWithCoercion(Object
the object on which properties will be set
object
,
Map<String, Object>
the map containing properties and their values to be set on the object
propertiesToSet
)

Sets a collection of properties of a given object to the (optionally coerced) values associated with those properties.

public static Class<?>

Returns:

the Class object for the class with the given name.
toClass
(String
fully qualified class name of the class for which a Class instance needs to be created
className
)

Returns the Class instance associated with the class of the given string, using the context class loader and if that fails the defining class loader of the current class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LOGGERback to summary
private static final Logger LOGGER
REFLECTION_CACHEback to summary
private static final Map<ClassLoader, ConcurrentMap<String, ReflectionUtils.MetaData>> REFLECTION_CACHE

Cache

Constructor Detail

ReflectionUtilsback to summary
private ReflectionUtils()

Method Detail

clearCacheback to summary
public static synchronized void clearCache(ClassLoader loader)

Clears the cache for the specified ClassLoader.

This method MUST be called when ConfigureListener .contextDestroyed() is called.

Parameters
loader:ClassLoader

the ClassLoader whose associated cache should be cleared

findMethodback to summary
public static Method findMethod(Object base, String methodName, Object[] params)

Finds a method based on the method name, amount of parameters and limited typing, if necessary prefixed with "get".

Note that this supports overloading, but a limited one. Given an actual parameter of type Long, this will select a method accepting Number when the choice is between Number and a non-compatible type like String. However, it will NOT select the best match if the choice is between Number and Long.

Parameters
base:Object

the object in which the method is to be found

methodName:String

name of the method to be found

params:Object[]

the method parameters

Returns:Method

a method if one is found, null otherwise

getMetaDataback to summary
private static ReflectionUtils.MetaData getMetaData(ClassLoader loader, Class<?> clazz)

Return the MetaData for the specified Class.

This will check the cache associated with the specified ClassLoader. If there is no cache hit, then a new MetaData instance will be created and stored.

Parameters
loader:ClassLoader

ClassLoader

clazz:Class<?>

the Class of interest

Returns:ReflectionUtils.MetaData

a MetaData object for the specified Class

getMetaDataback to summary
private static ReflectionUtils.MetaData getMetaData(ClassLoader loader, String className)

Return the MetaData for the specified className.

This will check the cache associated with the specified ClassLoader. If there is no cache hit, then a new MetaData instance will be created and stored.

Parameters
loader:ClassLoader

ClassLoader

className:String

the class of interest

Returns:ReflectionUtils.MetaData

a MetaData object for the specified Class

initCacheback to summary
public static synchronized void initCache(ClassLoader loader)
instanceback to summary
public static <T> T instance(String className)

Creates an instance of a class with the given fully qualified class name.

Parameters
<T>
The generic object type.
className:String

fully qualified class name of the class for which an instance needs to be created

Returns:T

an instance of the class denoted by className

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalStateException:
if the class cannot be found
instanceback to summary
public static <T> T instance(Class<T> clazz)

Creates a new instance of the class represented by the given Class object

Parameters
<T>
The generic object type.
clazz:Class<T>

the Class object for which an instance needs to be created

Returns:T

an instance of the class as given by the clazz parameter

Exceptions
IllegalStateException:
if the class cannot be found, or cannot be instantiated or when a security manager prevents this operation
lookupClassback to summary
public static Class<?> lookupClass(String className)

Obtain a Class instance based on the provided String name.

Parameters
className:String

the class to look up

Returns:Class<?>

the Class corresponding to className

lookupConstructorback to summary
public static Constructor<?> lookupConstructor(Class<?> clazz, Class<?>... params)

Returns the Constructor appropriate to the specified Class and parameters.

Parameters
clazz:Class<?>

the Class of interest

params:Class<?>[]

the parameters for the constructor of the provided Class

Returns:Constructor<?>

a Constructor that can be invoked with the specified parameters

lookupMethodback to summary
public static Method lookupMethod(Object object, String methodName, Class<?>... params)

Returns the Method appropriate to the specified object instance, method name, and parameters.

Parameters
object:Object

the Object instance of interest

methodName:String

the name of the method

params:Class<?>[]

the parameters for the specified method

Returns:Method

a Method that can be invoked with the specified parameters

lookupMethodback to summary
public static Method lookupMethod(Class<?> clazz, String methodName, Class<?>... params)

Returns the Method appropriate to the specified Class, method name, and parameters.

Parameters
clazz:Class<?>

the Class of interest

methodName:String

the name of the method

params:Class<?>[]

the parameters for the specified method

Returns:Method

a Method that can be invoked with the specified parameters

lookupReadMethodback to summary
public static Method lookupReadMethod(String className, String propertyName)
Parameters
className:String

the fully qualified class name

propertyName:String

a JavaBeans property name

Returns:Method

a method suitable for obtaining the value of a JavaBeans property, or null if the property doesn't exist or can't be read.

lookupWriteMethodback to summary
public static Method lookupWriteMethod(String className, String propertyName)
Parameters
className:String

the fully qualified class name

propertyName:String

a JavaBeans property name

Returns:Method

a method suitable for setting a JavaBeans property, or null if the property doesn't exist or is readonly.

newInstanceback to summary
public static Object newInstance(String className) throws IllegalArgumentException, ReflectiveOperationException, SecurityException

Constructs a new object instance based off the provided class name.

Parameters
className:String

the class of the object to instantiate

Returns:Object

a new instances of said class

Exceptions
InstantiationException:
if the class cannot be instantiated
IllegalAccessException:
if there is a security violation
setPropertiesback to summary
public static void setProperties(Object object, Map<String, Object> propertiesToSet)

Sets a collection of properties of a given object to the values associated with those properties.

In the map that represents these properties, each key represents the name of the property, with the value associated with that key being the value that is set for the property.

E.g. map entry key = foo, value = "bar", which "bar" an instance of String, will conceptually result in the following call: object.setFoo("string");

Note

This particular method assumes that there's a write method for each property in the map with the right type. No specific checking is done whether this is indeed the case.

Parameters
object:Object

the object on which properties will be set

propertiesToSet:Map<String, Object>

the map containing properties and their values to be set on the object

setPropertiesWithCoercionback to summary
public static void setPropertiesWithCoercion(Object object, Map<String, Object> propertiesToSet)

Sets a collection of properties of a given object to the (optionally coerced) values associated with those properties.

In the map that represents these properties, each key represents the name of the property, with the value associated with that key being the value that is set for the property.

E.g. map entry key = foo, value = "bar", which "bar" an instance of String, will conceptually result in the following call: object.setFoo("string");

NOTE 1: In case the value is a String, and the target type is not String, the standard property editor mechanism will be used to attempt a conversion.

Note 2: This method operates somewhat as the reverse of Reflection#setProperties(Object, Map) Here only the available writable properties of the object are matched against the map with properties to set. Properties in the map for which there isn't a corresponding writable property on the object are ignored.

Following the above two notes, use this method when attempting to set properties on an object in a lenient best effort basis. Use Reflection#setProperties(Object, Map) when all properties need to be set with the exact type as the value appears in the map.

Parameters
object:Object

the object on which properties will be set

propertiesToSet:Map<String, Object>

the map containing properties and their values to be set on the object

toClassback to summary
public static Class<?> toClass(String className)

Returns the Class instance associated with the class of the given string, using the context class loader and if that fails the defining class loader of the current class.

Parameters
className:String

fully qualified class name of the class for which a Class instance needs to be created

Returns:Class<?>

the Class object for the class with the given name.

Exceptions
IllegalStateException:
if the class cannot be found.
com.sun.faces.util back to summary

private final Class ReflectionUtils.MetaData

extends Object
Class Inheritance

MetaData contains lookup methods for Constructors and Methods of a particular Class.

Field Summary

Modifier and TypeField and Description
pack-priv Class<?>
pack-priv Map<Integer, Constructor<T>>
pack-priv Map<String, HashMap<Integer, Method>>
pack-priv Map<String, HashMap<Integer, Method>>
pack-priv Map<String, PropertyDescriptor>

Constructor Summary

AccessConstructor and Description
public
MetaData(Class<?>
class to construct a new MetaData instance from.
clazz
)

Constructs a new MetaData instance for the specified class.

Method Summary

Modifier and TypeMethod and Description
private static Integer

Returns:

the result of Arrays.deepHashCode
getKey
(Class<?>...
the parameters to a Constructor or a Method instance
params
)

Return a hashcode of all the class parameters.

public Class<?>

Returns:

the Class for this MetaData instance
lookupClass
()

Looks up the class for this MetaData instance.

public Constructor<?>

Returns:

the Constructor appropriate to the specified parameters or null
lookupConstructor
(Class<?>...
constructor parameters
params
)

Looks up a Constructor based off the specified params.

public Method

Returns:

the Method appropriate to the specified name and parameters or null
lookupMethod
(String
the name of the Method
name
,
Class<?>...
the Method parameters
params
)

Looks up a Method based off the specified method name and params.

public Method

Returns:

a method suitable for obtaining the value of a JavaBeans property, or null if the property doesn't exist or can't be read.
lookupReadMethod
(String
a JavaBeans property name
propName
)

public Method

Returns:

a method suitable for setting a JavaBeans property, or null if the property doesn't exist or is readonly.
lookupWriteMethod
(String
a JavaBeans property name
propName
)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clazzback to summary
pack-priv Class<?> clazz
constructorsback to summary
pack-priv Map<Integer, Constructor<T>> constructors
declaredMethodsback to summary
pack-priv Map<String, HashMap<Integer, Method>> declaredMethods
methodsback to summary
pack-priv Map<String, HashMap<Integer, Method>> methods
propertyDescriptorsback to summary
pack-priv Map<String, PropertyDescriptor> propertyDescriptors

Constructor Detail

MetaDataback to summary
public MetaData(Class<?> clazz)

Constructs a new MetaData instance for the specified class.

Parameters
clazz:Class<?>

class to construct a new MetaData instance from.

Method Detail

getKeyback to summary
private static Integer getKey(Class<?>... params)

Return a hashcode of all the class parameters.

Parameters
params:Class<?>[]

the parameters to a Constructor or a Method instance

Returns:Integer

the result of Arrays.deepHashCode

lookupClassback to summary
public Class<?> lookupClass()

Looks up the class for this MetaData instance.

Returns:Class<?>

the Class for this MetaData instance

lookupConstructorback to summary
public Constructor<?> lookupConstructor(Class<?>... params)

Looks up a Constructor based off the specified params.

Parameters
params:Class<?>[]

constructor parameters

Returns:Constructor<?>

the Constructor appropriate to the specified parameters or null

lookupMethodback to summary
public Method lookupMethod(String name, Class<?>... params)

Looks up a Method based off the specified method name and params.

Parameters
name:String

the name of the Method

params:Class<?>[]

the Method parameters

Returns:Method

the Method appropriate to the specified name and parameters or null

lookupReadMethodback to summary
public Method lookupReadMethod(String propName)
Parameters
propName:String

a JavaBeans property name

Returns:Method

a method suitable for obtaining the value of a JavaBeans property, or null if the property doesn't exist or can't be read.

lookupWriteMethodback to summary
public Method lookupWriteMethod(String propName)
Parameters
propName:String

a JavaBeans property name

Returns:Method

a method suitable for setting a JavaBeans property, or null if the property doesn't exist or is readonly.