com.vaadin.flow.internal.
Class ReflectTools
All Implemented Interfaces:
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.
For internal use only. May be renamed or removed in a future release.
Since:
1.0
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkClassAccessibility
(Class<?> clazz) Makes a check whether the
clazz
is externally accessible for instantiation (e.g.static IllegalArgumentException
convertInstantiationException
(Exception exception, Class<?> clazz) Helper to handle all exceptions which might occur during class instantiation and returns an
IllegalArgumentException
with a descriptive error message hinting of what might be wrong with the class that could not be instantiated.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 Optional<ClassLoader>
findClosestCommonClassLoaderAncestor
(ClassLoader classLoaderA, ClassLoader classLoaderB) Finds the common ancestor of the two
ClassLoaders
.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 Optional<Annotation>
getAnnotation
(AnnotatedElement element, String annotationFqn) Gets annotation of
element
whose FQN isannotationFqn
.static Object
getAnnotationMethodValue
(Annotation annotation, String methodName) Gets the annotation method return value.
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.
getGenericInterfaceTypes
(Class<?> clazz, Class<?> interfaceType) Finds the Class type for all parameters defined by the generic interface class extended by given class if exists.
Finds a getter for a property in a bean type.
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.
getSetterMethods
(Class<?> type) Return all the setter methods from the given type.
static boolean
hasAnnotation
(AnnotatedElement element, String annotationFqn) Checks whether the
element
has annotation whose FQN isannotationFqn
.static boolean
hasAnnotationWithSimpleName
(AnnotatedElement element, String simpleName) Checks whether the
element
has annotation whose simple name issimpleName
.static boolean
isCheckedException
(Class<?> exceptionClass) Checks if the given exception class represents a checked exception.
static boolean
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
isInstantiableService
(Class<?> clazz) Check if a class can be instantiated via its default constructor via reflection.
static boolean
isNotObjectMethod
(Method method) Returns whether the given method is NOT declared in
Object
.static boolean
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.
-
Constructor Details
-
ReflectTools
public ReflectTools()
-
-
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 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
Converts the given primitive type to its boxed version.
Parameters:
type
- the primitive type to convertReturns:
the corresponding boxed type
-
getPrimitiveDefaultValue
Gets default value for given
primitiveType
.Parameters:
primitiveType
- the primitive typeReturns:
the corresponding default value
-
isSetter
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
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
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
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
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
Return all the setter methods from the given type.
Parameters:
type
- the type to get setters fromReturns:
a stream of setter methods
-
isNotObjectMethod
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
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
Returns property type from the given getter or setter method.
Parameters:
method
- the method to inspectReturns:
the property type
See Also:
-
createInstance
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
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 -
convertInstantiationException
public static IllegalArgumentException convertInstantiationException(Exception exception, Class<?> clazz) Helper to handle all exceptions which might occur during class instantiation and returns an
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 theclazz
.Parameters:
exception
- original exceptionclazz
- instantiation target classReturns:
an IllegalArgumentException with descriptive message
-
checkClassAccessibility
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
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
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
-
getGenericInterfaceTypes
Finds the Class type for all parameters defined by 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:
List of Class if found else empty List, never null
-
getGetter
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
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
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
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
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 -
findClosestCommonClassLoaderAncestor
public static Optional<ClassLoader> findClosestCommonClassLoaderAncestor(ClassLoader classLoaderA, ClassLoader classLoaderB) Finds the common ancestor of the two
ClassLoaders
. The class loaders themselves are acceptable ancestors; If they are equal,classLoaderA
is returned. An empty optional is returned if the two class loaders aren't equal, no shared ancestor is found, or the implementation of the class loader treats bootstrap class loader asnull
when it is the parent of a class loaders (seeClassLoader.getParent()
.Parameters:
classLoaderA
- class loader AclassLoaderB
- class loader BReturns:
a common ancestor both class loaders share, or an empty optional if there is no shared ancestor. Or if the implementation treats bootstrap loaders as
null
(as perClassLoader.getParent()
). -
hasAnnotation
Checks whether the
element
has annotation whose FQN isannotationFqn
.Parameters:
element
- annotated element (field, method, etc.)annotationFqn
- annotation FQNReturns:
true
iselement
has annotation whose FQN isannotationFqn
,false
otherwise -
hasAnnotationWithSimpleName
Checks whether the
element
has annotation whose simple name issimpleName
.Parameters:
element
- annotated element (field, method, etc.)simpleName
- annotation simple nameReturns:
true
iselement
has annotation whose simple name issimpleName
,false
otherwise -
getAnnotationMethodValue
Gets the annotation method return value.
Parameters:
annotation
- the annotationmethodName
- the method nameReturns:
an optional value, or an empty optional if element has no annotation with required
annotationFqn
-
getAnnotation
Gets annotation of
element
whose FQN isannotationFqn
.Parameters:
element
- annotated element (field, method, etc.)annotationFqn
- annotation FQNReturns:
an optional annotation, or an empty optional if element has no annotation with required
annotationFqn
-
isInstantiableService
Check if a class can be instantiated via its default constructor via reflection.
Parameters:
clazz
- the class to checkReturns:
true if the class can be instantiated, otherwise false
-