Class ReflectionUtils

  • All Implemented Interfaces:

    
    public final class ReflectionUtils
    
                        

    Reflection utility methods.

    This class is intended for internal use only. All public methods (except asRandomizer might change between minor versions without notice.

    Author:

    Mahmoud Ben Hassine ([email protected])

    • Constructor Detail

    • Method Detail

      • getDeclaredFields

         static <T> List<Field> getDeclaredFields(T type)

        Get declared fields of a given type.

        Parameters:
        type - the type to introspect
        Returns:

        list of declared fields

      • getInheritedFields

         static List<Field> getInheritedFields(Class<out Object> type)

        Get inherited fields of a given type.

        Parameters:
        type - the type to introspect
        Returns:

        list of inherited fields

      • setProperty

         static void setProperty(Object object, Field field, Object value)

        Set a value in a field of a target object. If the target object provides a setter for the field, this setter will be used. Otherwise, the field will be set using reflection.

        Parameters:
        object - instance to set the property on
        field - field to set the property on
        value - value to set
      • setFieldValue

         static void setFieldValue(Object object, Field field, Object value)

        Set a value (accessible or not accessible) in a field of a target object.

        Parameters:
        object - instance to set the property on
        field - field to set the property on
        value - value to set
      • getFieldValue

         static Object getFieldValue(Object object, Field field)

        Get the value (accessible or not accessible) of a field of a target object.

        Parameters:
        object - instance to get the field of
        field - field to get the value of
        Returns:

        the value of the field

      • getWrapperType

         static Class<out Object> getWrapperType(Class<out Object> primitiveType)

        Get wrapper type of a primitive type.

        Parameters:
        primitiveType - to get its wrapper type
        Returns:

        the wrapper type of the given primitive type

      • isPrimitiveFieldWithDefaultValue

         static boolean isPrimitiveFieldWithDefaultValue(Object object, Field field)

        Check if a field has a primitive type and matching default value which is set by the compiler.

        Parameters:
        object - instance to get the field value of
        field - field to check
        Returns:

        true if the field is primitive and is set to the default value, false otherwise

      • isStatic

         static boolean isStatic(Field field)

        Check if a field is static.

        Parameters:
        field - the field to check
        Returns:

        true if the field is static, false otherwise

      • isInterface

         static boolean isInterface(Class<out Object> type)

        Check if a type is an interface.

        Parameters:
        type - the type to check
        Returns:

        true if the type is an interface, false otherwise

      • isAbstract

         static <T> boolean isAbstract(Class<T> type)

        Check if the type is abstract (either an interface or an abstract class).

        Parameters:
        type - the type to check
        Returns:

        true if the type is abstract, false otherwise

      • isPublic

         static <T> boolean isPublic(Class<T> type)

        Check if the type is public.

        Parameters:
        type - the type to check
        Returns:

        true if the type is public, false otherwise

      • isArrayType

         static boolean isArrayType(Class<out Object> type)

        Check if a type is an array type.

        Parameters:
        type - the type to check.
        Returns:

        true if the type is an array type, false otherwise.

      • isEnumType

         static boolean isEnumType(Class<out Object> type)

        Check if a type is an enum type.

        Parameters:
        type - the type to check.
        Returns:

        true if the type is an enum type, false otherwise.

      • isCollectionType

         static boolean isCollectionType(Class<out Object> type)

        Check if a type is a collection type.

        Parameters:
        type - the type to check.
        Returns:

        true if the type is a collection type, false otherwise

      • isCollectionType

         static boolean isCollectionType(Type type)

        Check if a type is a collection type.

        Parameters:
        type - the type to check.
        Returns:

        true if the type is a collection type, false otherwise

      • isPopulatable

         static boolean isPopulatable(Type type)

        Check if a type is populatable.

        Parameters:
        type - the type to check
        Returns:

        true if the type is populatable, false otherwise

      • isIntrospectable

         static boolean isIntrospectable(Class<out Object> type)

        Check if a type should be introspected for internal fields.

        Parameters:
        type - the type to check
        Returns:

        true if the type should be introspected, false otherwise

      • isMapType

         static boolean isMapType(Class<out Object> type)

        Check if a type is a map type.

        Parameters:
        type - the type to check
        Returns:

        true if the type is a map type, false otherwise.

      • isJdkBuiltIn

         static boolean isJdkBuiltIn(Class<out Object> type)

        Check if a type is a JDK built-in collection/map.

        Parameters:
        type - the type to check
        Returns:

        true if the type is a built-in collection/map type, false otherwise.

      • isParameterizedType

         static boolean isParameterizedType(Type type)

        Check if a type is a parameterized type

        Parameters:
        type - the type to check
        Returns:

        true if the type is parameterized, false otherwise

      • isWildcardType

         static boolean isWildcardType(Type type)

        Check if a type is a wildcard type

        Parameters:
        type - the type to check
        Returns:

        true if the type is a wildcard type, false otherwise

      • isTypeVariable

         static boolean isTypeVariable(Type type)

        Check if a type is a type variable

        Parameters:
        type - the type to check
        Returns:

        true if the type is a type variable, false otherwise

      • getPublicConcreteSubTypesOf

         static <T> List<Class<out Object>> getPublicConcreteSubTypesOf(Class<T> type)

        Searches the classpath for all public concrete subtypes of the given interface or abstract class.

        Parameters:
        type - to search concrete subtypes of
        Returns:

        a list of all concrete subtypes found

      • filterSameParameterizedTypes

         static List<Class<out Object>> filterSameParameterizedTypes(List<Class<out Object>> types, Type type)

        Filters a list of types to keep only elements having the same parameterized types as the given type.

        Parameters:
        types - a list of types to filter
        type - the type to use for the search
        Returns:

        a list of types having the same parameterized types as the given type

      • getAnnotation

         static <T extends Annotation> T getAnnotation(Field field, Class<T> annotationType)

        Looks for given annotationType on given field or read method for field.

        Parameters:
        field - field to check
        annotationType - Type of annotation you're looking for.
        Returns:

        given annotation if field or read method has this annotation or null.

      • isAnnotationPresent

         static boolean isAnnotationPresent(Field field, Class<out Annotation> annotationType)

        Checks if field or corresponding read method is annotated with given annotationType.

        Parameters:
        field - Field to check
        annotationType - Annotation you're looking for.
        Returns:

        true if field or read method it annotated with given annotationType or false.

      • createEmptyCollectionForType

         static Collection<out Object> createEmptyCollectionForType(Class<out Object> fieldType, int initialSize)

        Create an empty collection for the given type.

        Parameters:
        fieldType - for which an empty collection should we created
        initialSize - initial size of the collection
        Returns:

        empty collection

      • getWriteMethod

         static Optional<Method> getWriteMethod(Field field)

        Get the write method for given field.

        Parameters:
        field - field to get the write method for
        Returns:

        Optional of write method or empty if field has no write method

      • getReadMethod

         static Optional<Method> getReadMethod(Field field)

        Get the read method for given field.

        Parameters:
        field - field to get the read method for.
        Returns:

        Optional of read method or empty if field has no read method

      • getCanonicalConstructor

         static <T> Constructor<T> getCanonicalConstructor(Class<T> recordType)

        Get the canonical constructor of a record

        Parameters:
        recordType - the type of the record
        Returns:

        the canonical constructor of the record