Class TypeUtils


  • public class TypeUtils
    extends Object
    Utility methods focusing on type inspection, particularly with regard to generics. Code extracted from Apache Commons Lang 3 - TypeUtils.java and ArrayUtils.java
    • Method Detail

      • isAssignable

        public static boolean isAssignable​(Type type,
                                           Type toType)

        Checks if the subject type may be implicitly cast to the target type following the Java generics rules.

        Parameters:
        type - the subject type to be assigned to the target type
        toType - the target type
        Returns:
        true if type is assignable to toType.
      • normalizeUpperBounds

        public static Type[] normalizeUpperBounds​(Type[] bounds)

        This method strips out the redundant upper bound types in type variable types and wildcard types (or it would with wildcard types if multiple upper bounds were allowed).

        Example, with the variable type declaration:

         <K extends java.util.Collection<String> &
         java.util.List<String>>
         

        since List is a subinterface of Collection, this method will return the bounds as if the declaration had been:

         <K extends java.util.List<String>>
         
        Parameters:
        bounds - an array of types representing the upper bounds of either WildcardType or TypeVariable, not null.
        Returns:
        an array containing the values from bounds minus the redundant types.
      • equals

        public static boolean equals​(Type t1,
                                     Type t2)
        Check equality of types.
        Parameters:
        t1 - the first type
        t2 - the second type
        Returns:
        boolean
      • toString

        public static String toString​(Type type)
      • add

        public static int[] add​(int[] array,
                                int element)