Class ReflectHelper


  • public final class ReflectHelper
    extends Object
    Utility class for various reflection operations.
    • Field Detail

      • NO_PARAM_SIGNATURE

        public static final Class<?>[] NO_PARAM_SIGNATURE
      • SINGLE_OBJECT_PARAM_SIGNATURE

        public static final Class<?>[] SINGLE_OBJECT_PARAM_SIGNATURE
    • Method Detail

      • extractEqualsMethod

        public static Method extractEqualsMethod​(Class<?> clazz)
                                          throws NoSuchMethodException
        Encapsulation of getting hold of a class's equals method.
        Parameters:
        clazz - The class from which to extract the equals method.
        Returns:
        The equals method reference
        Throws:
        NoSuchMethodException - Should indicate an attempt to extract equals method from interface.
      • extractHashCodeMethod

        public static Method extractHashCodeMethod​(Class<?> clazz)
                                            throws NoSuchMethodException
        Encapsulation of getting hold of a class's hashCode method.
        Parameters:
        clazz - The class from which to extract the hashCode method.
        Returns:
        The hashCode method reference
        Throws:
        NoSuchMethodException - Should indicate an attempt to extract hashCode method from interface.
      • overridesEquals

        public static boolean overridesEquals​(Class<?> clazz)
        Determine if the given class defines an Object.equals(java.lang.Object) override.
        Parameters:
        clazz - The class to check
        Returns:
        True if clazz defines an equals override.
      • overridesHashCode

        public static boolean overridesHashCode​(Class<?> clazz)
        Determine if the given class defines a Object.hashCode() override.
        Parameters:
        clazz - The class to check
        Returns:
        True if clazz defines an hashCode override.
      • implementsInterface

        public static boolean implementsInterface​(Class<?> clazz,
                                                  Class<?> intf)
        Determine if the given class implements the given interface.
        Parameters:
        clazz - The class to check
        intf - The interface to check it against.
        Returns:
        True if the class does implement the interface, false otherwise.
      • isPublic

        public static boolean isPublic​(Class<?> clazz,
                                       Member member)
        Is this member publicly accessible.
        Parameters:
        clazz - The class which defines the member
        member - The member.
        Returns:
        True if the member is publicly accessible, false otherwise.
      • reflectedPropertyClass

        public static Class<?> reflectedPropertyClass​(String className,
                                                      String name,
                                                      ClassLoaderService classLoaderService)
                                               throws MappingException
        Attempt to resolve the specified property type through reflection.
        Parameters:
        className - The name of the class owning the property.
        name - The name of the property.
        classLoaderService - ClassLoader services
        Returns:
        The type of the property.
        Throws:
        MappingException - Indicates we were unable to locate the property.
      • reflectedPropertyClass

        public static Class<?> reflectedPropertyClass​(Class<?> clazz,
                                                      String name)
                                               throws MappingException
        Attempt to resolve the specified property type through reflection.
        Parameters:
        clazz - The class owning the property.
        name - The name of the property.
        Returns:
        The type of the property.
        Throws:
        MappingException - Indicates we were unable to locate the property.
      • getDefaultConstructor

        public static <T> Constructor<T> getDefaultConstructor​(Class<T> clazz)
                                                        throws PropertyNotFoundException
        Retrieve the default (no arg) constructor from the given class.
        Parameters:
        clazz - The class for which to retrieve the default ctor.
        Returns:
        The default constructor.
        Throws:
        PropertyNotFoundException - Indicates there was not publicly accessible, no-arg constructor (todo : why PropertyNotFoundException???)
      • getDefaultSupplier

        public static <T> Supplier<T> getDefaultSupplier​(Class<T> clazz)
      • isAbstractClass

        public static boolean isAbstractClass​(Class<?> clazz)
        Determine if the given class is declared abstract.
        Parameters:
        clazz - The class to check.
        Returns:
        True if the class is abstract, false otherwise.
      • isFinalClass

        public static boolean isFinalClass​(Class<?> clazz)
        Determine is the given class is declared final.
        Parameters:
        clazz - The class to check.
        Returns:
        True if the class is final, false otherwise.
      • getConstructor

        @Deprecated(since="6",
                    forRemoval=true)
        public static Constructor<?> getConstructor​(Class<?> clazz,
                                                    Type[] types)
                                             throws PropertyNotFoundException
        Deprecated, for removal: This API element is subject to removal in a future version.
        no longer used, since we moved away from the Type interface
        Retrieve a constructor for the given class, with arguments matching the specified Hibernate mapping types.
        Parameters:
        clazz - The class needing instantiation
        types - The types representing the required ctor param signature
        Returns:
        The matching constructor
        Throws:
        PropertyNotFoundException - Indicates we could not locate an appropriate constructor
      • getConstructorOrNull

        public static <T> Constructor<T> getConstructorOrNull​(Class<T> clazz,
                                                              Class<?>... constructorArgs)
        Retrieve a constructor for the given class, with arguments matching the specified Java types, or return null if no such constructor exists.
        Parameters:
        clazz - The class needing instantiation
        constructorArgs - The types representing the required ctor param signature
        Returns:
        The matching constructor, or null
      • findField

        public static Field findField​(Class<?> containerClass,
                                      String propertyName)
      • ensureAccessibility

        public static void ensureAccessibility​(AccessibleObject accessibleObject)
      • isStaticField

        public static boolean isStaticField​(Field field)
      • findGetterMethod

        public static Method findGetterMethod​(Class<?> containerClass,
                                              String propertyName)
      • getGetterOrNull

        public static Method getGetterOrNull​(Class<?> containerClass,
                                             String propertyName)
        Find the method that can be used as the getter for this property.
        Parameters:
        containerClass - The Class which contains the property
        propertyName - The name of the property
        Returns:
        The getter method, or null if there is none.
        Throws:
        MappingException - If the containerClass has both a get- and an is- form.
      • verifyNoIsVariantExists

        public static void verifyNoIsVariantExists​(Class<?> containerClass,
                                                   String propertyName,
                                                   Method getMethod,
                                                   String stemName)
      • checkGetAndIsVariants

        public static void checkGetAndIsVariants​(Class<?> containerClass,
                                                 String propertyName,
                                                 Method getMethod,
                                                 Method isMethod)
      • verifyNoGetVariantExists

        public static void verifyNoGetVariantExists​(Class<?> containerClass,
                                                    String propertyName,
                                                    Method isMethod,
                                                    String stemName)
      • getterMethodOrNull

        public static Method getterMethodOrNull​(Class<?> containerJavaType,
                                                String propertyName)
      • setterMethodOrNull

        public static Method setterMethodOrNull​(Class<?> containerClass,
                                                String propertyName,
                                                Class<?> propertyType)
      • setterMethodOrNullBySetterName

        public static Method setterMethodOrNullBySetterName​(Class<?> containerClass,
                                                            String setterName,
                                                            Class<?> propertyType)
      • findSetterMethod

        public static Method findSetterMethod​(Class<?> containerClass,
                                              String propertyName,
                                              Class<?> propertyType)
      • findGetterMethodForFieldAccess

        public static Method findGetterMethodForFieldAccess​(Field field,
                                                            String propertyName)
        Similar to getterMethodOrNull(java.lang.Class<?>, java.lang.String), except that here we are just looking for the corresponding getter for a field (defined as field access) if one exists.

        We do not look at supers, although conceivably the super could declare the method as an abstract - but again, that is such an edge case...

      • isRecord

        public static boolean isRecord​(Class<?> declaringClass)
      • getRecordComponentTypes

        public static Class<?>[] getRecordComponentTypes​(Class<?> javaType)
      • getRecordComponentNames

        public static String[] getRecordComponentNames​(Class<?> javaType)
      • getClass

        public static <T> Class<T> getClass​(Type type)
      • getPropertyType

        public static Class<?> getPropertyType​(Member member)
      • isClass

        public static boolean isClass​(Class<?> resultClass)