Class ParameterizedTypeView

    • Field Detail

      • type

        protected final T extends Type type
    • Method Detail

      • of

        public static ParameterizedTypeView of​(ParameterizedType parameterized)
        Creates view over provided type.
        Parameters:
        parameterized - construct to wrap
        Returns:
        view wrapping specified type
      • erasure

        public Class<?> erasure()
        Description copied from class: TypeView
        Finds exact class that would be erasure of wrapped type.

        This method is implemented according to JSL section 4.6 with following extensions:

        • Erasure of wildcard type is erasure of its leftmost upper bound, or Object if there isn't one

        These extensions are needed, because these types are cannot be used in declaration, but can be obtained by reflections.

        Specified by:
        erasure in class TypeView
        Returns:
        erasure of wrapped type
      • arguments

        public List<TypeView> arguments()
        Calculates types used as arguments for this parameterized type.

        In other words, this method returns list of types that were used in place of type variables in declaration of this type. For example, if wrapped type is Map<Integer, ? extends String[]> then this method will return a list with two elements containing type view wrapping of class and type view wrapping of wildcard type with array class as a upper bound.

        This method creates corrected arguments: if raw type that this parameterized type is based on declared one of its variable to have upper bound, and in type argument substitution there were additional bounds declared, corrected argument will have both bounds. Ex. if raw type was a class with declaration class Example<T extends Serializable> {...} and this was used as field private Example<? super String> field;, then this method will return wildcard with both bounds: ? extends Serializable super String.

        Returns:
        types used as arguments for this parameterized type.
      • interfaces

        public List<ParameterizedTypeView> interfaces()
        Extracts list of interfaces either extended or implemented by this type.
        Returns:
        interfaces wrapped in TypeView
      • asParameterized

        @Deprecated
        public ParameterizedTypeView asParameterized()
        Deprecated.
        the type system already knows this is a parameterized type, and this call does not change the type
        Try to cast or convert wrapped type to ParameterizedType.

        This method is useful if you know that underlying type is ParameterizedType, then this is just a casting.

        This can also be done for regular classes, ClassView, as non-generic class can be treated as generic class with zero type parameters. It also works for class-represented arrays that are currently converted to parameterized types.

        Specified by:
        asParameterized in class TypeView
        Returns:
        this, as this is already parameterized view
      • asClass

        public ClassView<?> asClass()
                             throws IllegalStateException
        Try to cast or convert wrapped type to Class.

        This method is useful if you know that underlying type is Class, then this is just a casting.

        This can also be done for parameterized types with no actual substitutions and array classes represented by ArrayTypeView

        This method only works if this type has no actual substitutions and its base type is a class.

        Specified by:
        asClass in class TypeView
        Returns:
        class view with class equivalent to this unparameterized type
        Throws:
        IllegalStateException - when this view cannot be converted to Class.
      • asArray

        public ArrayTypeView asArray()
                              throws IllegalStateException
        Try to cast/convert wrapped type to GenericArrayType.

        This method is useful if you know that underlying type is GenericArrayType, then this is just a casting.

        This can also be done for parameterized types with no actual substitutions that represetn array class, and class view that has array class.

        This method only works if this type has no actual substitutions and its base type is an array class.

        Specified by:
        asArray in class TypeView
        Returns:
        class view with array class equivalent to this unparameterized type
        Throws:
        IllegalStateException - when this view cannot be converted to view of GenericArrayType.
      • isSubTypeOf

        public boolean isSubTypeOf​(TypeView other)
        Description copied from class: TypeView
        Tests if type wrapped in this view is subtype of the provided one.

        This method tries to adhere to the spirit of subtyping: it answers the question: Having reference of type T, can value of type S be always assigned to it?

        Subtyping is precisely defined for types that can be declared in source, but there are more types that can be constructed or extracted via reflections. This method follows JLS section 4.10 for every aspect it declares, but also defines sensible rule for subtyping wildcards: wildcard is a supertype of its lower bounds, i.e. ? super CharStream is supertype of CharStream and

        Specified by:
        isSubTypeOf in class TypeView
        Parameters:
        other - type to test subtype relation against
        Returns:
        true if this type is subtype of provided argument
      • visit

        public <T> T visit​(TypeView.Visitor<T> visitor)
        Description copied from class: TypeView
        Visitor patter method dispatching method.

        This method calls specific method on provided visitor, depending on actual implementation of this type.

        It will return whatever the method returns.

        Specified by:
        visit in class TypeView
        Type Parameters:
        T - type returned by visitor method
        Parameters:
        visitor - method target
        Returns:
        same result as visitors method.
      • resolve

        public ParameterizedTypeView resolve​(Type other)
        Description copied from class: TypeView
        Create view that has variable substitutions copied from provided.

        See documentation on TypeView.resolve(TypeView) for details.

        Overrides:
        resolve in class TypeView
        Parameters:
        other - type to copy substitutions from
        Returns:
        TypeView that uses substitutions from provided type
      • resolve

        public ParameterizedTypeView resolve​(TypeView other)
        Description copied from class: TypeView
        Create view that has variable substitutions copied from provided.

        This method creates type that has any variable substituted by substitutions used in other.

        Substitutions are found recursively. They can be only directly found in parameterized types, as arguments, but these parameterized types can be found in components of some containing type. For example, wildcard might use parameterized type as a bound, and this parameterized type might have substitution for specific variable.

        This method can be used to find what the signature of field or method looks like in some specific type. For example: lets say that there's a generic class class Example<T> that declares field protected List<T> elements. It's descendant class, declared class ExtendedExample extends Example<String> inherits field elements. In this case no conventional method will extract actual field type, List<String>:

        But this can be resolved using this method: TypeView.ofTypeOf(Example.class.getDeclaredField("elements")).resolve(ExampleExtension.class) will wrap List<String>, because variable substitution T -> String is used.
        Overrides:
        resolve in class TypeView
        Parameters:
        other - type to copy substitutions from
        Returns:
        TypeView that uses substitutions from provided type
      • unwrap

        public final T unwrap()
        Description copied from class: TypeView
        Extracts native type from this view.

        This is inverse operation from TypeView.of(Type)

        Specified by:
        unwrap in class TypeView
        Returns:
        type that this view was for
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object