Class ReflectionUtils


  • public class ReflectionUtils
    extends Object
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • typeFromString

        public static Type typeFromString​(String type)
      • loadClassByName

        public static Class<?> loadClassByName​(String className)
                                        throws ClassNotFoundException
        Load Class by class name. If class not found in it's Class loader or one of the parent class loaders - delegate to the Thread's ContextClassLoader
        Parameters:
        className - Canonical class name
        Returns:
        Class definition of className
        Throws:
        ClassNotFoundException
      • isOverriddenMethod

        public static boolean isOverriddenMethod​(Method methodToFind,
                                                 Class<?> cls)
        Checks if the method methodToFind is the overridden method from the superclass or superinterface.
        Parameters:
        methodToFind - is method to check
        cls - is method class
        Returns:
        true if the method is overridden method
      • hasOverriddenMethods

        public static boolean hasOverriddenMethods​(Method methodToFind,
                                                   Class<?> cls)
      • getOverriddenMethod

        public static Method getOverriddenMethod​(Method method)
        Returns overridden method from superclass if it exists. If method was not found returns null.
        Parameters:
        method - is method to find
        Returns:
        overridden method from superclass
      • findField

        public static Field findField​(String name,
                                      Class<?> cls)
        Searches the field name in given class cls. If the field is found returns it, else return null.
        Parameters:
        name - is the field to search
        cls - is the class or interface where to search
        Returns:
        field if it is found
      • findMethod

        public static Method findMethod​(Method methodToFind,
                                        Class<?> cls)
        Searches the method methodToFind in given class cls. If the method is found returns it, else return null.
        Parameters:
        methodToFind - is the method to search
        cls - is the class or interface where to search
        Returns:
        method if it is found
      • isInject

        public static boolean isInject​(List<Annotation> annotations)
      • isConstructorCompatible

        public static boolean isConstructorCompatible​(Constructor<?> constructor)
      • getDeclaredFields

        public static List<Field> getDeclaredFields​(Class<?> cls)
        Returns the list of declared fields from the class cls and its superclasses excluding Object class. If the field from child class hides the field from superclass, the field from superclass won't be added to the result list. The list is sorted by name to make the output of this method deterministic. See https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getFields--
        Parameters:
        cls - is the processing class
        Returns:
        list of Fields
      • getAnnotation

        public static <A extends Annotation> A getAnnotation​(Method method,
                                                             Class<A> annotationClass)
        Returns an annotation by type from a method.
        Type Parameters:
        A - is the type of annotation
        Parameters:
        method - is the method to find
        annotationClass - is the type of annotation
        Returns:
        annotation if it is found
      • getAnnotation

        public static <A extends Annotation> A getAnnotation​(Class<?> cls,
                                                             Class<A> annotationClass)
      • getRepeatableAnnotations

        public static <A extends AnnotationList<A> getRepeatableAnnotations​(Method method,
                                                                              Class<A> annotationClass)
        Returns a List of repeatable annotations by type from a method.
        Type Parameters:
        A - is the type of annotation
        Parameters:
        method - is the method to find
        annotationClass - is the type of annotation
        Returns:
        List of repeatable annotations if it is found
      • getRepeatableAnnotations

        public static <A extends AnnotationList<A> getRepeatableAnnotations​(Class<?> cls,
                                                                              Class<A> annotationClass)
      • getRepeatableAnnotationsArray

        public static <A extends Annotation> A[] getRepeatableAnnotationsArray​(Class<?> cls,
                                                                               Class<A> annotationClass)
      • getParameterAnnotations

        public static Annotation[][] getParameterAnnotations​(Method method)
      • isVoid

        public static boolean isVoid​(Type type)
        Checks if the type is void.
        Parameters:
        type - is the type to check
        Returns:
        true if the type is void
      • isSystemType

        public static boolean isSystemType​(com.fasterxml.jackson.databind.JavaType type)
      • isSystemTypeNotArray

        public static boolean isSystemTypeNotArray​(com.fasterxml.jackson.databind.JavaType type)
      • safeInvoke

        public static Optional<Object> safeInvoke​(Method method,
                                                  Object obj,
                                                  Object... args)
        A utility method to get an optional containing result from method or empty optional if unable to access
        Parameters:
        method - from reflect, a method of a class or interface
        obj - the class object in which the method exists
        args - varags of the parameters passed to the method
        Returns:
        the result of the method, or empty conditional
      • safeGet

        public static Optional<Object> safeGet​(Field field,
                                               Object obj)
        A utility method to get an optional containing value of field or empty optional if unable to access
        Parameters:
        field - from reflect, a field of a class or interface
        obj - the class object in which the field exists
        Returns:
        optional containing the value of the field on the specified object, or empty optional