Class ReflectTools

java.lang.Object
com.vaadin.flow.internal.ReflectTools
All Implemented Interfaces:
Serializable

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.

For internal use only. May be renamed or removed in a future release.

Since:
1.0
See Also:
  • 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 method
      methodName - The name of the method
      parameterTypes - The parameter types for the method.
      Returns:
      A reference to the method
      Throws:
      ExceptionInInitializerError - Wraps any exception in an ExceptionInInitializerError 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 field
      field - 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 retrieved
      IllegalAccessException - If the value could not be retrieved
      IllegalArgumentException - 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 field
      field - The field we want to get the value for
      propertyType - 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 retrieved
      IllegalAccessException - If the value could not be retrieved
      IllegalArgumentException - 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 field
      field - The field we want to set the value for
      value - The value to set
      Throws:
      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 convert
      Returns:
      the corresponding boxed type
    • getPrimitiveDefaultValue

      public static Serializable getPrimitiveDefaultValue(Class<?> primitiveType)
      Gets default value for given primitiveType.
      Parameters:
      primitiveType - the primitive type
      Returns:
      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 check
      Returns:
      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 check
      Returns:
      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 check
      isBoolean - whether the method is getter for boolean type
      Returns:
      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 check
      Returns:
      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 from
      Returns:
      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 from
      Returns:
      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 check
      Returns:
      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 parse
      Returns:
      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 inspect
      Returns:
      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 type
      Parameters:
      cls - the class to instantiate
      Returns:
      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 the originalClass.

      Type Parameters:
      T - type of a proxy class
      Parameters:
      proxyClass - the proxy class to instantiate
      originalClass - the class that is used to determine exception description, if creation fails
      Returns:
      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 interface
      interfaceType - class type of interface to get generic for
      Returns:
      Class if found else null
    • getGenericInterfaceTypes

      public 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.
      Parameters:
      clazz - class that should extend interface
      interfaceType - class type of interface to get generic for
      Returns:
      List of Class if found else empty List, never 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, not null
      propertyName - the property name, not null
      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 check
      Returns:
      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 for
      Returns:
      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 from
      Returns:
      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, not null
      b - another class to get the base type for, not null
      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 as null when it is the parent of a class loaders (see ClassLoader.getParent().
      Parameters:
      classLoaderA - class loader A
      classLoaderB - class loader B
      Returns:
      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 per ClassLoader.getParent()).
    • hasAnnotation

      public static boolean hasAnnotation(AnnotatedElement element, String annotationFqn)
      Checks whether the element has annotation whose FQN is annotationFqn.
      Parameters:
      element - annotated element (field, method, etc.)
      annotationFqn - annotation FQN
      Returns:
      true is element has annotation whose FQN is annotationFqn, false otherwise
    • hasAnnotationWithSimpleName

      public static boolean hasAnnotationWithSimpleName(AnnotatedElement element, String simpleName)
      Checks whether the element has annotation whose simple name is simpleName.
      Parameters:
      element - annotated element (field, method, etc.)
      simpleName - annotation simple name
      Returns:
      true is element has annotation whose simple name is simpleName, false otherwise
    • getAnnotationMethodValue

      public static Object getAnnotationMethodValue(Annotation annotation, String methodName)
      Gets the annotation method return value.
      Parameters:
      annotation - the annotation
      methodName - the method name
      Returns:
      an optional value, or an empty optional if element has no annotation with required annotationFqn
    • getAnnotation

      public static Optional<Annotation> getAnnotation(AnnotatedElement element, String annotationFqn)
      Gets annotation of element whose FQN is annotationFqn.
      Parameters:
      element - annotated element (field, method, etc.)
      annotationFqn - annotation FQN
      Returns:
      an optional annotation, or an empty optional if element has no annotation with required annotationFqn
    • isInstantiableService

      public static boolean isInstantiableService(Class<?> clazz)
      Check if a class can be instantiated via its default constructor via reflection.
      Parameters:
      clazz - the class to check
      Returns:
      true if the class can be instantiated, otherwise false