Class ReflectionUtils


  • @API(status=INTERNAL,
         since="1.0")
    public final class ReflectionUtils
    extends Object
    Collection of utilities for working with the Java reflection APIs.

    DISCLAIMER

    These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!

    Some utilities are published via the maintained ReflectionSupport class.

    Since:
    1.0
    See Also:
    ReflectionSupport
    • Method Detail

      • isPublic

        public static boolean isPublic​(Class<?> clazz)
      • isPublic

        public static boolean isPublic​(Member member)
      • isPrivate

        public static boolean isPrivate​(Class<?> clazz)
      • isPrivate

        public static boolean isPrivate​(Member member)
      • isNotPrivate

        public static boolean isNotPrivate​(Member member)
      • isAbstract

        public static boolean isAbstract​(Class<?> clazz)
      • isAbstract

        public static boolean isAbstract​(Member member)
      • isStatic

        public static boolean isStatic​(Class<?> clazz)
      • isStatic

        public static boolean isStatic​(Member member)
      • isNotStatic

        public static boolean isNotStatic​(Member member)
      • isInnerClass

        public static boolean isInnerClass​(Class<?> clazz)
        Determine if the supplied class is an inner class (i.e., a non-static member class).

        Technically speaking (i.e., according to the Java Language Specification), "an inner class may be a non-static member class, a local class, or an anonymous class." However, this method does not return true for a local or anonymous class.

        Parameters:
        clazz - the class to check; never null
        Returns:
        true if the class is an inner class
      • returnsVoid

        public static boolean returnsVoid​(Method method)
      • isArray

        public static boolean isArray​(Object obj)
        Determine if the supplied object is an array.
        Parameters:
        obj - the object to test; potentially null
        Returns:
        true if the object is an array
      • isMultidimensionalArray

        @API(status=INTERNAL,
             since="1.3.2")
        public static boolean isMultidimensionalArray​(Object obj)
        Determine if the supplied object is a multidimensional array.
        Parameters:
        obj - the object to test; potentially null
        Returns:
        true if the object is a multidimensional array
        Since:
        1.3.2
      • isAssignableTo

        public static boolean isAssignableTo​(Object obj,
                                             Class<?> targetType)
        Determine if the supplied object can be assigned to the supplied target type for the purpose of reflective method invocations.

        In contrast to Class.isInstance(Object), this method returns true if the target type represents a primitive type whose wrapper matches the supplied object's type. In addition, this method also supports widening conversions for primitive types and their corresponding wrapper types.

        If the supplied object is null and the supplied type does not represent a primitive type, this method returns true.

        Parameters:
        obj - the object to test for assignment compatibility; potentially null
        targetType - the type to check against; never null
        Returns:
        true if the object is assignment compatible
        See Also:
        Class.isInstance(Object), Class.isAssignableFrom(Class)
      • getWrapperType

        public static Class<?> getWrapperType​(Class<?> type)
        Get the wrapper type for the supplied primitive type.
        Parameters:
        type - the primitive type for which to retrieve the wrapper type
        Returns:
        the corresponding wrapper type or null if the supplied type is null or not a primitive type
      • newInstance

        public static <T> T newInstance​(Constructor<T> constructor,
                                        Object... args)
        Create a new instance of type T by invoking the supplied constructor with the supplied arguments.

        The constructor will be made accessible if necessary, and any checked exception will be masked as an unchecked exception.

        Parameters:
        constructor - the constructor to invoke; never null
        args - the arguments to pass to the constructor
        Returns:
        the new instance; never null
        See Also:
        newInstance(Class, Object...), ExceptionUtils.throwAsUncheckedException(Throwable)
      • readFieldValue

        public static <T> Optional<Object> readFieldValue​(Class<T> clazz,
                                                          String fieldName,
                                                          T instance)
        Read the value of a potentially inaccessible or nonexistent field.

        If the field does not exist or the value of the field is null, an empty Optional will be returned.

        Parameters:
        clazz - the class where the field is declared; never null
        fieldName - the name of the field; never null or empty
        instance - the instance from where the value is to be read; may be null for a static field
        See Also:
        readFieldValue(Field), readFieldValue(Field, Object)
      • readFieldValue

        public static <T> Optional<Object> readFieldValue​(Field field,
                                                          T instance)
        Read the value of the supplied field, making it accessible if necessary and masking any checked exception as an unchecked exception.

        If the value of the field is null, an empty Optional will be returned.

        Parameters:
        field - the field to read; never null
        instance - the instance from which the value is to be read; may be null for a static field
        See Also:
        readFieldValue(Field), readFieldValue(Class, String, Object)
      • getFullyQualifiedMethodName

        public static String getFullyQualifiedMethodName​(Class<?> clazz,
                                                         String methodName,
                                                         Class<?>... parameterTypes)
        Build the fully qualified method name for the method described by the supplied class, method name, and parameter types.
        Parameters:
        clazz - the class that declares the method; never null
        methodName - the name of the method; never null or blank
        parameterTypes - the parameter types of the method; may be null or empty
        Returns:
        fully qualified method name; never null
      • parseFullyQualifiedMethodName

        public static String[] parseFullyQualifiedMethodName​(String fullyQualifiedMethodName)
        Parse the supplied fully qualified method name into a 3-element String[] with the following content.
        • index 0: the fully qualified class name
        • index 1: the name of the method
        • index 2: a comma-separated list of parameter types, or a blank string if the method does not declare any formal parameters
        Parameters:
        fullyQualifiedMethodName - a fully qualified method name, never null or blank
        Returns:
        a 3-element array of strings containing the parsed values
      • getOutermostInstance

        public static Optional<Object> getOutermostInstance​(Object inner,
                                                            Class<?> requiredType)
        Get the outermost instance of the required type, searching recursively through enclosing instances.

        If the supplied inner object is of the required type, it will simply be returned.

        Parameters:
        inner - the inner object from which to begin the search; never null
        requiredType - the required type of the outermost instance; never null
        Returns:
        an Optional containing the outermost instance; never null but potentially empty
      • getAllClasspathRootDirectories

        public static Set<Path> getAllClasspathRootDirectories()
      • findAllClassesInClasspathRoot

        public static List<Class<?>> findAllClassesInClasspathRoot​(URI root,
                                                                   ClassFilter classFilter)
        Since:
        1.1
      • findAllClassesInPackage

        public static List<Class<?>> findAllClassesInPackage​(String basePackageName,
                                                             ClassFilter classFilter)
        Since:
        1.1
      • findAllClassesInModule

        public static List<Class<?>> findAllClassesInModule​(String moduleName,
                                                            ClassFilter classFilter)
        Since:
        1.1.1
      • findConstructors

        public static List<Constructor<?>> findConstructors​(Class<?> clazz,
                                                            Predicate<Constructor<?>> predicate)
        Find all constructors in the supplied class that match the supplied predicate.
        Parameters:
        clazz - the class in which to search for constructors; never null
        predicate - the predicate to use to test for a match; never null
        Returns:
        an immutable list of all such constructors found; never null but potentially empty
      • findFields

        public static List<Field> findFields​(Class<?> clazz,
                                             Predicate<Field> predicate,
                                             ReflectionUtils.HierarchyTraversalMode traversalMode)
        Find all fields of the supplied class or interface that match the specified predicate.

        The results will not contain fields that are hidden.

        Parameters:
        clazz - the class or interface in which to find the fields; never null
        predicate - the field filter; never null
        traversalMode - the hierarchy traversal mode; never null
        Returns:
        an immutable list of all such fields found; never null but potentially empty
      • isMethodPresent

        public static boolean isMethodPresent​(Class<?> clazz,
                                              Predicate<Method> predicate)
        Determine if a Method matching the supplied Predicate is present within the type hierarchy of the specified class, beginning with the specified class or interface and traversing up the type hierarchy until such a method is found or the type hierarchy is exhausted.
        Parameters:
        clazz - the class or interface in which to find the method; never null
        predicate - the predicate to use to test for a match; never null
        Returns:
        true if such a method is present
        See Also:
        findMethod(Class, String, String), findMethod(Class, String, Class...)
      • makeAccessible

        public static <T extends AccessibleObject> T makeAccessible​(T object)
      • getAllAssignmentCompatibleClasses

        public static Set<Class<?>> getAllAssignmentCompatibleClasses​(Class<?> clazz)
        Return all classes and interfaces that can be used as assignment types for instances of the specified Class, including itself.
        Parameters:
        clazz - the Class to look up
        See Also:
        Class.isAssignableFrom(java.lang.Class<?>)