Class ReflectTools
java.lang.Object
com.vaadin.flow.component.spreadsheet.framework.ReflectTools
- All Implemented Interfaces:
Serializable
An util class with helpers for reflection operations. Used internally by
Vaadin and should not be used by application developers. Subject to change at
any time.
- Since:
- 6.2
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?> convertPrimitiveType(Class<?> type) static <T> TcreateInstance(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 MethodfindMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) Locates the method in the given class.static ObjectgetJavaFieldValue(Object object, Field field) Returns the value of the java field.static ObjectgetJavaFieldValue(Object object, Field field, Class<?> propertyType) Returns the value of the java field that is assignable to the property type.static MethodReturns the first non-synthetic method of the specifiedlistenerClass, which must have single method in the source-code.static voidsetJavaFieldValue(Object object, Field field, Object value) Sets the value of a java field.
-
Method Details
-
findMethod
public static Method findMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) throws ExceptionInInitializerError Locates the method in the given class. Returns null if the method is not found. Throws an ExceptionInInitializerError if there is a problem locating the method as this is mainly called from static blocks.- Parameters:
cls- Class that contains the methodmethodName- The name of the methodparameterTypes- The parameter types for the method.- Returns:
- A reference to the method
- Throws:
ExceptionInInitializerError- Wraps any exception in anExceptionInInitializerErrorso this method can be called from a static initializer.
-
getJavaFieldValue
public static Object getJavaFieldValue(Object object, Field field) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException Returns the value of the java field.Uses getter if present, otherwise tries to access even private fields directly.
- Parameters:
object- The object containing the fieldfield- The field we want to get the value for- Returns:
- The value of the field in the object
- Throws:
InvocationTargetException- If the value could not be retrievedIllegalAccessException- If the value could not be retrievedIllegalArgumentException- If the value could not be retrieved
-
getJavaFieldValue
public static Object getJavaFieldValue(Object object, Field field, Class<?> propertyType) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException Returns the value of the java field that is assignable to the property type.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.
- Parameters:
object- The object containing the fieldfield- The field we want to get the value forpropertyType- The type the field must be assignable to- Returns:
- The value of the field in the object
- Throws:
InvocationTargetException- If the value could not be retrievedIllegalAccessException- If the value could not be retrievedIllegalArgumentException- If the value could not be retrieved
-
setJavaFieldValue
public static void setJavaFieldValue(Object object, Field field, Object value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException Sets the value of a java field.Uses setter if present, otherwise tries to access even private fields directly.
- Parameters:
object- The object containing the fieldfield- The field we want to set the value forvalue- The value to set- Throws:
IllegalAccessException- 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 field
-
convertPrimitiveType
- Since:
- 7.4
-
findCommonBaseType
Finds the most specific class that both provided classes extend from.- Parameters:
a- one class to get the base type for, notnullb- another class to get the base type for, notnull- Returns:
- the most specific base class, not
null - Since:
- 8.0
-
createInstance
Creates a instance of the given class with a no-arg constructor.Catches all exceptions which might occur and wraps them in a
IllegalArgumentExceptionwith a descriptive error message hinting of what might be wrong with the class that could not be instantiated.- Parameters:
cls- the class to instantiate- Returns:
- an instance of the class
- Since:
- 8.1.1
-
getMethod
Returns the first non-synthetic method of the specifiedlistenerClass, which must have single method in the source-code. This is needed, to remove the synthetic methods added if the class is instrumented.- Parameters:
listenerClass- TheClassof the listener, which has a single method in the source code- Returns:
- the first non-synthetic method
- Throws:
IllegalStateException- if the specified class does not have found method- Since:
- 8.2
-