Class Types


  • public abstract class Types
    extends java.lang.Object
    Utilities for converting between Expression, Type and Class.
    See Also:
    Primitive
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Types.ArrayType
      Array type.
      static class  Types.MapType
      Map type.
      (package private) static class  Types.ParameterizedTypeImpl
      Implementation of ParameterizedType.
      static interface  Types.RecordField
      Field that belongs to a record.
      static interface  Types.RecordType
      Base class for record-like types that do not mapped to (currently loaded) Java Class objects.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Types()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static boolean allAssignable​(boolean varArgs, java.lang.Class[] parameterTypes, java.lang.Class[] argumentTypes)  
      (package private) static java.lang.Class arrayClass​(java.lang.reflect.Type type)  
      (package private) static java.lang.reflect.Type arrayType​(java.lang.reflect.Type type)  
      (package private) static java.lang.reflect.Type arrayType​(java.lang.reflect.Type type, int dimension)  
      private static boolean assignableFrom​(java.lang.Class parameter, java.lang.Class argument)
      Returns whether a parameter is assignable from an argument by virtue of (a) sub-classing (e.g.
      static java.lang.reflect.Type box​(java.lang.reflect.Type type)  
      (package private) static java.lang.String boxClassName​(java.lang.reflect.Type type)
      Boxes a type, if it is primitive, and returns the type name.
      static Expression castIfNecessary​(java.lang.reflect.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.
      (package private) static java.lang.String className​(java.lang.reflect.Type type)  
      static void discard​(java.lang.Object o)  
      static PseudoField field​(java.lang.reflect.Field field)  
      (package private) static java.lang.reflect.Type gcd​(java.lang.reflect.Type... types)
      Returns the most restrictive type that is assignable from all given types.
      static java.lang.reflect.Type getComponentType​(java.lang.reflect.Type type)
      Returns the component type of an array.
      (package private) static java.lang.reflect.Type getComponentTypeN​(java.lang.reflect.Type type)  
      static java.lang.reflect.Type getElementType​(java.lang.reflect.Type type)
      Returns the element type of a Collection, Iterable (including Queryable and Enumerable), Iterator, Enumerator, or an array.
      (package private) static java.lang.reflect.Field getField​(java.lang.String fieldName, java.lang.Class clazz)  
      (package private) static PseudoField getField​(java.lang.String fieldName, java.lang.reflect.Type type)  
      private static Types.RecordField getRecordField​(java.lang.String fieldName, Types.RecordType type)  
      private static Types.RecordField getSystemField​(java.lang.String fieldName, java.lang.Class clazz)  
      static boolean isArray​(java.lang.reflect.Type type)  
      static boolean isAssignableFrom​(java.lang.reflect.Type type0, java.lang.reflect.Type type)  
      static java.lang.reflect.Constructor lookupConstructor​(java.lang.reflect.Type type, java.lang.Class... argumentTypes)
      Finds a constructor of a given class that accepts a given set of arguments.
      static java.lang.reflect.Field lookupField​(java.lang.reflect.Type type, java.lang.String name)  
      static java.lang.reflect.Method lookupMethod​(java.lang.Class clazz, java.lang.String methodName, java.lang.Class... argumentTypes)
      Finds a method of a given name that accepts a given set of arguments.
      static java.lang.reflect.Field nthField​(int ordinal, java.lang.Class clazz)  
      static PseudoField nthField​(int ordinal, java.lang.reflect.Type clazz)  
      static java.lang.reflect.Type of​(java.lang.reflect.Type type, java.lang.reflect.Type... typeArguments)
      Creates a type with generic parameters.
      static java.lang.reflect.Type stripGenerics​(java.lang.reflect.Type type)  
      static java.lang.Class toClass​(java.lang.reflect.Type type)  
      (package private) static java.lang.Class[] toClassArray​(java.lang.Iterable<? extends Expression> arguments)  
      (package private) static java.lang.Class[] toClassArray​(java.util.Collection<java.lang.reflect.Type> types)  
      private static <T> java.util.List<T> toList​(T[] ts)
      Returns a list backed by a copy of an array.
      static java.lang.reflect.Type unbox​(java.lang.reflect.Type type)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Types

        private Types()
    • Method Detail

      • of

        public static java.lang.reflect.Type of​(java.lang.reflect.Type type,
                                                java.lang.reflect.Type... typeArguments)
        Creates a type with generic parameters.
      • getElementType

        public static java.lang.reflect.Type getElementType​(java.lang.reflect.Type type)
        Returns the element type of a Collection, Iterable (including Queryable and Enumerable), Iterator, Enumerator, or an array.

        Returns null if the type is not one of these.

      • toList

        private static <T> java.util.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.
      • getField

        static java.lang.reflect.Field getField​(java.lang.String fieldName,
                                                java.lang.Class clazz)
      • getField

        static PseudoField getField​(java.lang.String fieldName,
                                    java.lang.reflect.Type type)
      • getSystemField

        private static Types.RecordField getSystemField​(java.lang.String fieldName,
                                                        java.lang.Class clazz)
      • toClass

        public static java.lang.Class toClass​(java.lang.reflect.Type type)
      • toClassArray

        static java.lang.Class[] toClassArray​(java.util.Collection<java.lang.reflect.Type> types)
      • toClassArray

        static java.lang.Class[] toClassArray​(java.lang.Iterable<? extends Expression> arguments)
      • getComponentType

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

        static java.lang.reflect.Type getComponentTypeN​(java.lang.reflect.Type type)
      • boxClassName

        static java.lang.String boxClassName​(java.lang.reflect.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 java.lang.reflect.Type box​(java.lang.reflect.Type type)
      • unbox

        public static java.lang.reflect.Type unbox​(java.lang.reflect.Type type)
      • className

        static java.lang.String className​(java.lang.reflect.Type type)
      • isAssignableFrom

        public static boolean isAssignableFrom​(java.lang.reflect.Type type0,
                                               java.lang.reflect.Type type)
      • isArray

        public static boolean isArray​(java.lang.reflect.Type type)
      • nthField

        public static java.lang.reflect.Field nthField​(int ordinal,
                                                       java.lang.Class clazz)
      • nthField

        public static PseudoField nthField​(int ordinal,
                                           java.lang.reflect.Type clazz)
      • allAssignable

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

        private static boolean assignableFrom​(java.lang.Class parameter,
                                              java.lang.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 java.lang.reflect.Method lookupMethod​(java.lang.Class clazz,
                                                            java.lang.String methodName,
                                                            java.lang.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:
        java.lang.RuntimeException - if method not found
      • lookupConstructor

        public static java.lang.reflect.Constructor lookupConstructor​(java.lang.reflect.Type type,
                                                                      java.lang.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:
        java.lang.RuntimeException - if method not found
      • lookupField

        public static java.lang.reflect.Field lookupField​(java.lang.reflect.Type type,
                                                          java.lang.String name)
      • discard

        public static void discard​(java.lang.Object o)
      • gcd

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

        public static Expression castIfNecessary​(java.lang.reflect.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
      • field

        public static PseudoField field​(java.lang.reflect.Field field)
      • arrayClass

        static java.lang.Class arrayClass​(java.lang.reflect.Type type)
      • arrayType

        static java.lang.reflect.Type arrayType​(java.lang.reflect.Type type,
                                                int dimension)
      • arrayType

        static java.lang.reflect.Type arrayType​(java.lang.reflect.Type type)
      • stripGenerics

        public static java.lang.reflect.Type stripGenerics​(java.lang.reflect.Type type)