Class Types

    • Constructor Detail

      • Types

        private Types​()
    • Method Detail

      • of

        public static Type of​(Type type,
                              Type... typeArguments)
        Creates a type with generic parameters.
      • toList

        private static <T> List<T> toList​(T[] ts)
        Returns a list backed by a copy of an array. The contents of the list will not change even if the contents of the array are subsequently modified.
      • toClass

        public static Class toClass​(Type type)
      • getComponentType

        public static Type getComponentType​(Type type)
        Returns the component type of an array.
      • getComponentTypeN

        static Type getComponentTypeN​(Type type)
      • boxClassName

        static String boxClassName​(Type type)
        Boxes a type, if it is primitive, and returns the type name. The type is abbreviated if it is in the "java.lang" package.

        For example, boxClassName(int) returns "Integer"; boxClassName(List<String>) returns "List<String>"

        Parameters:
        type - Type
        Returns:
        Class name
      • box

        public static Type box​(Type type)
      • unbox

        public static Type unbox​(Type type)
      • className

        static String className​(Type type)
      • isAssignableFrom

        public static boolean isAssignableFrom​(Type type0,
                                               Type type)
      • isArray

        public static boolean isArray​(Type type)
      • nthField

        public static Field nthField​(int ordinal,
                                     Class clazz)
      • nthField

        public static PseudoField nthField​(int ordinal,
                                           Type clazz)
      • allAssignable

        static boolean allAssignable​(boolean varArgs,
                                     Class[] parameterTypes,
                                     Class[] argumentTypes)
      • assignableFrom

        private static boolean assignableFrom​(Class parameter,
                                              Class argument)
        Returns whether a parameter is assignable from an argument by virtue of (a) sub-classing (e.g. Writer is assignable from PrintWriter) and (b) up-casting (e.g. int is assignable from short).
        Parameters:
        parameter - Parameter type
        argument - Argument type
        Returns:
        Whether parameter can be assigned from argument
      • lookupMethod

        public static Method lookupMethod​(Class clazz,
                                          String methodName,
                                          Class... argumentTypes)
        Finds a method of a given name that accepts a given set of arguments. Includes in its search inherited methods and methods with wider argument types.
        Parameters:
        clazz - Class against which method is invoked
        methodName - Name of method
        argumentTypes - Types of arguments
        Returns:
        A method with the given name that matches the arguments given
        Throws:
        RuntimeException - if method not found
      • lookupConstructor

        public static Constructor lookupConstructor​(Type type,
                                                    Class... argumentTypes)
        Finds a constructor of a given class that accepts a given set of arguments. Includes in its search methods with wider argument types.
        Parameters:
        type - Class against which method is invoked
        argumentTypes - Types of arguments
        Returns:
        A method with the given name that matches the arguments given
        Throws:
        RuntimeException - if method not found
      • lookupField

        public static Field lookupField​(Type type,
                                        String name)
      • discard

        public static void discard​(Object o)
      • gcd

        static Type gcd​(Type... types)
        Returns the most restrictive type that is assignable from all given types.
      • castIfNecessary

        public static Expression castIfNecessary​(Type returnType,
                                                 Expression expression)
        Wraps an expression in a cast if it is not already of the desired type, or cannot be implicitly converted to it.
        Parameters:
        returnType - Desired type
        expression - Expression
        Returns:
        Expression of desired type
      • arrayClass

        static Class arrayClass​(Type type)
      • arrayType

        static Type arrayType​(Type type,
                              int dimension)
      • arrayType

        static Type arrayType​(Type type)
      • stripGenerics

        public static Type stripGenerics​(Type type)