com.vaadin.flow.internal.
Class ReflectTools
- java.lang.Object
-
- com.vaadin.flow.internal.ReflectTools
-
All Implemented Interfaces:
public class ReflectTools extends Object implements 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.
See Also:
-
-
Constructor Summary
Constructors Constructor and Description ReflectTools()
-
Method Summary
All Methods Modifier and Type Method and Description static void
checkClassAccessibility(Class<?> clazz)
Makes a check whether the
clazz
is externally accessible for instantiation (e.g.static Class<?>
convertPrimitiveType(Class<?> type)
Converts the given primitive type to its boxed version.
static <T> T
createInstance(Class<T> cls)
Creates an instance of the given class with a no-arg constructor.
static Type
createParameterizedType(Class<?> rawType, Type subType)
Creates a parameterized type, e.g.
static <T> T
createProxyInstance(Class<T> proxyClass, Class<?> originalClass)
Creates an instance of the given
proxyClass
with 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<Integer>
getConstantIntValues(Class<?> clazz)
Collect all the integer values for public static final constants found for the given class.
static Method
getFunctionalMethod(Class<?> functionalClass)
Get the functional interface method name defined for given interface class.
static Class<?>
getGenericInterfaceType(Class<?> clazz, Class<?> interfaceType)
Finds the Class type for the generic interface class extended by given class if exists.
static Optional<Method>
getGetter(Class<? extends Object> beanClass, String propertyName)
Finds a getter for a property in a bean type.
static Stream<Method>
getGetterMethods(Class<?> type)
Return all the getter methods from the given type.
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 Serializable
getPrimitiveDefaultValue(Class<?> primitiveType)
Gets default value for given
primitiveType
.static String
getPropertyName(Method method)
Parses the property name from the given getter or setter method.
static Type
getPropertyType(Method method)
Returns property type from the given getter or setter method.
static Stream<Method>
getSetterMethods(Class<?> type)
Return all the setter methods from the given type.
static boolean
isCheckedException(Class<?> exceptionClass)
Checks if the given exception class represents a checked exception.
static boolean
isGetter(Method method)
Checks whether the given method is a valid getter according to JavaBeans Specification.
static boolean
isGetterName(String methodName, boolean isBoolean)
Checks whether the given method name is a valid getter name according to the JavaBeans Specification.
static boolean
isNotObjectMethod(Method method)
Returns whether the given method is NOT declared in
Object
.static boolean
isSetter(Method method)
Checks whether the given method is a valid setter according to the JavaBeans Specification.
static boolean
isSetterName(String methodName)
Checks whether the given method name is a valid setter name according to the JavaBeans Specification.
static void
setJavaFieldValue(Object object, Field field, Object value)
Sets the value of a java field.
-
-
-
Method Detail
-
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 anExceptionInInitializerError
so 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 forReturns:
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 toReturns:
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 IllegalArgumentException
Sets the value of a java field.
Parameters:
object
- The object containing the fieldfield
- The field we want to set the value forvalue
- The value to setThrows:
IllegalArgumentException
- If the value could not be assigned to the field
-
convertPrimitiveType
public static Class<?> convertPrimitiveType(Class<?> type)
Converts the given primitive type to its boxed version.
Parameters:
type
- the primitive type to convertReturns:
the corresponding boxed type
-
getPrimitiveDefaultValue
public static Serializable getPrimitiveDefaultValue(Class<?> primitiveType)
Gets default value for given
primitiveType
.Parameters:
primitiveType
- the primitive typeReturns:
the corresponding default value
-
isSetter
public static boolean isSetter(Method method)
Checks whether the given method is a valid setter according to the JavaBeans Specification.
Parameters:
method
- the method to checkReturns:
true
if the method is a setter,false
if not
-
isSetterName
public static boolean isSetterName(String methodName)
Checks whether the given method name is a valid setter name according to the JavaBeans Specification.
Parameters:
methodName
- the method name to checkReturns:
true
if the method name is a setter name,false
if not
-
isGetterName
public static boolean isGetterName(String methodName, boolean isBoolean)
Checks whether the given method name is a valid getter name according to the JavaBeans Specification.
Parameters:
methodName
- the method name to checkisBoolean
- whether the method is getter for boolean typeReturns:
true
if the method name is a getter name,false
if not
-
isGetter
public static boolean isGetter(Method method)
Checks whether the given method is a valid getter according to JavaBeans Specification.
Parameters:
method
- the method to checkReturns:
true
if the method is a getter,false
if not
-
getGetterMethods
public static Stream<Method> getGetterMethods(Class<?> type)
Return all the getter methods from the given type.
Any getter methods from
Object
are excluded.Parameters:
type
- the type to get getters fromReturns:
a stream of getter methods
-
getSetterMethods
public static Stream<Method> getSetterMethods(Class<?> type)
Return all the setter methods from the given type.
Parameters:
type
- the type to get setters fromReturns:
a stream of setter methods
-
isNotObjectMethod
public static boolean isNotObjectMethod(Method method)
Returns whether the given method is NOT declared in
Object
.Parameters:
method
- the method to checkReturns:
true
if method is NOT declared in Object,false
if it is
-
getPropertyName
public static String getPropertyName(Method method)
Parses the property name from the given getter or setter method.
If the given method does not have a valid setter or getter name, this method may produce unexpected results.
Parameters:
method
- the method to parseReturns:
the name of the property
See Also:
-
getPropertyType
public static Type getPropertyType(Method method)
Returns property type from the given getter or setter method.
Parameters:
method
- the method to inspectReturns:
the property type
See Also:
-
createInstance
public static <T> T createInstance(Class<T> cls)
Creates an instance of the given class with a no-arg constructor.
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.Type Parameters:
T
- the instance typeParameters:
cls
- the class to instantiateReturns:
an instance of the class
-
createProxyInstance
public static <T> T createProxyInstance(Class<T> proxyClass, Class<?> originalClass)
Creates an instance of the given
proxyClass
with no-arg constructor.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. Descriptive message is derived based on the information about theoriginalClass
.Type Parameters:
T
- type of a proxy classParameters:
proxyClass
- the proxy class to instantiateoriginalClass
- the class that is used to determine exception description, if creation failsReturns:
instance of a proxyClass
Throws:
IllegalArgumentException
- if class instance creation fails
-
checkClassAccessibility
public static void checkClassAccessibility(Class<?> clazz)
Makes a check whether the
clazz
is externally accessible for instantiation (e.g. it's not inner class (nested and not static) and is not a local class).Parameters:
clazz
- type to check
-
createParameterizedType
public static Type createParameterizedType(Class<?> rawType, Type subType)
Creates a parameterized type, e.g.
List<Bean>
.Parameters:
rawType
- the raw type, e.g.List
subType
- the sub type, e.g.Bean
Returns:
a parameterized type
-
getGenericInterfaceType
public static Class<?> getGenericInterfaceType(Class<?> clazz, Class<?> interfaceType)
Finds the Class type for the generic interface class extended by given class if exists.
Parameters:
clazz
- class that should extend interfaceinterfaceType
- class type of interface to get generic forReturns:
Class if found else
null
-
getGetter
public static Optional<Method> getGetter(Class<? extends Object> beanClass, String propertyName)
Finds a getter for a property in a bean type.
Parameters:
beanClass
- the bean type, notnull
propertyName
- the property name, notnull
Returns:
a getter method, or an empty optional if the bean type has no readable property with the provided name
-
isCheckedException
public static boolean isCheckedException(Class<?> exceptionClass)
Checks if the given exception class represents a checked exception.
Parameters:
exceptionClass
- the class to checkReturns:
true
if the class represents a checked exception, false otherwise
-
getFunctionalMethod
public static Method getFunctionalMethod(Class<?> functionalClass)
Get the functional interface method name defined for given interface class.
Parameters:
functionalClass
- interface class to get the functional method forReturns:
functional interface method
-
getConstantIntValues
public static List<Integer> getConstantIntValues(Class<?> clazz)
Collect all the integer values for public static final constants found for the given class.
Parameters:
clazz
- class to collect constants fromReturns:
list of all integer constants in class
-
findCommonBaseType
public static Class<?> findCommonBaseType(Class<?> a, Class<?> b)
Finds the most specific class that both provided classes extend from.
Parameters:
a
- one class to get the base type for, notnull
b
- another class to get the base type for, notnull
Returns:
the most specific base class, not
null
Throws:
IllegalArgumentException
- if a or b are interfaces or primitive types
-
-