Modifier and Type | Method and Description |
---|---|
static <T> T |
accessField(Object instance,
String fieldName)
Returns the value of the field of the given instance on the given field name.
|
static Method |
findMethod(Object base,
String methodName,
Object... params)
Finds a method based on the method name, amount of parameters and limited typing.
|
static <T> T |
instance(Class<T> clazz)
Returns a new instance of the given class object using the default constructor.
|
static <T> T |
instance(String className)
Returns a new instance of the given class name using the default constructor.
|
static <T> T |
invokeMethod(Object instance,
String methodName,
Object... parameters)
Invoke a method of the given instance on the given method name with the given parameters and return the result.
|
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.
|
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.
|
static <T> Class<T> |
toClass(String className)
Returns the class object associated with the given class name, using the context class loader and if
that fails the defining class loader of the current class.
|
static <T> Class<T> |
toClassOrNull(String className)
Returns the class object associated with the given class name, using the context class loader and if
that fails the defining class loader of the current class.
|
public static void setProperties(Object object, Map<String,Object> propertiesToSet)
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.
object
- the object on which properties will be setpropertiesToSet
- the map containing properties and their values to be set on the objectpublic static void setPropertiesWithCoercion(Object object, Map<String,Object> propertiesToSet)
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 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 setProperties(Object, Map)
when all properties need to be set with the exact type as the value
appears in the map.
object
- the object on which properties will be setpropertiesToSet
- the map containing properties and their values to be set on the objectpublic static Method findMethod(Object base, String methodName, Object... params)
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.
base
- the object in which the method is to be foundmethodName
- name of the method to be foundparams
- the method parameterspublic static <T> Class<T> toClass(String className)
T
- The expected class type.className
- Fully qualified class name of the class for which a class object needs to be created.IllegalStateException
- If the class cannot be loaded.ClassCastException
- When T
is of wrong type.public static <T> Class<T> toClassOrNull(String className)
T
- The expected class type.className
- Fully qualified class name of the class for which a class object needs to be created.ClassCastException
- When T
is of wrong type.public static <T> T instance(String className)
T
- The expected return type.className
- Fully qualified class name of the class for which an instance needs to be created.IllegalStateException
- If the class cannot be loaded.ClassCastException
- When T
is of wrong type.public static <T> T instance(Class<T> clazz)
T
- The generic object type.clazz
- The class object for which an instance needs to be created.IllegalStateException
- If the class cannot be found, or cannot be instantiated, or when a security manager
prevents this operation.public static <T> T accessField(Object instance, String fieldName)
T
- The expected return type.instance
- The instance to access the given field on.fieldName
- The name of the field to be accessed on the given instance.ClassCastException
- When T
is of wrong type.IllegalStateException
- If the field cannot be accessed.public static <T> T invokeMethod(Object instance, String methodName, Object... parameters)
Note: the current implementation assumes for simplicity that no one of the given parameters is null. If one of them is still null, a NullPointerException will be thrown.
T
- The expected return type.instance
- The instance to invoke the given method on.methodName
- The name of the method to be invoked on the given instance.parameters
- The method parameters, if any.NullPointerException
- When one of the given parameters is null.IllegalStateException
- If the method cannot be invoked.ClassCastException
- When T
is of wrong type.Copyright © 2012–2016 OmniFaces. All rights reserved.