public class ReflectTools extends Object implements Serializable
Modifier and Type | Method and Description |
---|---|
static Class<?> |
convertPrimitiveType(Class<?> type) |
static <T> T |
createInstance(Class<T> cls)
Creates a instance of the given class with a no-arg constructor.
|
static Class<?> |
findCommonBaseType(Class<?> a,
Class<?> b)
Finds the most specific class that both provided classes extend from.
|
static Method |
findMethod(Class<?> cls,
String methodName,
Class<?>... parameterTypes)
Locates the method in the given class.
|
static List<Class<?>> |
getGenericInterfaceTypes(Class<?> clazz,
Class<?> interfaceType)
Finds the Class type for all parameters defined by the generic interface
class extended by given class if exists.
|
static Object |
getJavaFieldValue(Object object,
Field field)
Returns the value of the java field.
|
static Object |
getJavaFieldValue(Object object,
Field field,
Class<?> propertyType)
Returns the value of the java field that is assignable to the property
type.
|
static Method |
getMethod(Class<?> listenerClass)
Returns the first non-synthetic method of the specified
listenerClass , which must have single method in the source-code. |
static void |
setJavaFieldValue(Object object,
Field field,
Object value)
Sets the value of a java field.
|
public static Method findMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) throws ExceptionInInitializerError
cls
- Class that contains the methodmethodName
- The name of the methodparameterTypes
- The parameter types for the method.ExceptionInInitializerError
- Wraps any exception in an ExceptionInInitializerError
so this method can be called from a static initializer.public static Object getJavaFieldValue(Object object, Field field) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
Uses getter if present, otherwise tries to access even private fields directly.
object
- The object containing the fieldfield
- The field we want to get the value forInvocationTargetException
- If the value could not be retrievedIllegalAccessException
- If the value could not be retrievedIllegalArgumentException
- If the value could not be retrievedpublic static Object getJavaFieldValue(Object object, Field field, Class<?> propertyType) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
Uses getter if a getter for the correct return type is present, otherwise tries to access even private fields directly. If the java field is not assignable to the property type throws an IllegalArgumentException.
object
- The object containing the fieldfield
- The field we want to get the value forpropertyType
- The type the field must be assignable toInvocationTargetException
- If the value could not be retrievedIllegalAccessException
- If the value could not be retrievedIllegalArgumentException
- If the value could not be retrievedpublic static void setJavaFieldValue(Object object, Field field, Object value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
Uses setter if present, otherwise tries to access even private fields directly.
object
- The object containing the fieldfield
- The field we want to set the value forvalue
- The value to setIllegalAccessException
- If the value could not be assigned to the fieldIllegalArgumentException
- If the value could not be assigned to the fieldInvocationTargetException
- If the value could not be assigned to the fieldpublic static Class<?> findCommonBaseType(Class<?> a, Class<?> b)
a
- one class to get the base type for, not null
b
- another class to get the base type for, not null
null
public static <T> T createInstance(Class<T> cls)
Catches all exceptions which might occur and wraps them in a
IllegalArgumentException
with a descriptive error message hinting
of what might be wrong with the class that could not be instantiated.
cls
- the class to instantiatepublic static Method getMethod(Class<?> listenerClass)
listenerClass
, which must have single method in the source-code.
This is needed, to remove the synthetic methods added if the class is
instrumented.listenerClass
- The Class
of the listener, which has a single method
in the source codeIllegalStateException
- if the specified class does not have found methodpublic static List<Class<?>> getGenericInterfaceTypes(Class<?> clazz, Class<?> interfaceType)
clazz
- class that should extend interfaceinterfaceType
- class type of interface to get generic forCopyright © 2023 Vaadin Ltd. All rights reserved.