Class TypeInfo

java.lang.Object
org.kiwiproject.beta.reflect.TypeInfo

@Beta public final class TypeInfo extends Object
Represents either a simple type (e.g., Boolean or String) or a parameterized type (e.g. List<String> or Map<String, Integer>).
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    If a simple type, this will be empty.
    Assumes this represents a Collection, and returns the single generic type, e.g., for a Set<Integer> then Integer is returned.
    If a simple type, this is the entirety of the type information.
    boolean
    hasExactRawType(@NonNull Class<?> testType)
    Check if the raw type is exactly the same as testType.
    int
     
    boolean
    hasRawTypeAssignableTo(@NonNull Class<?> testType)
    Check if the raw type is assignable to the testType, for example, a raw type of ArrayList is assignable to List, and a String is assignable to a CharSequence.
    boolean
    Check if this is a collection (Collection, Set, List).
    boolean
    isCollectionOf(@NonNull Class<?> genericType)
    Check if this is a collection (Collection, Set, List) that contains elements with the given generic type.
    boolean
    isListOf(@NonNull Class<?> genericType)
    Check if this is a List that contains elements with the given generic type.
    boolean
    Check if this is a map.
    boolean
    isMapOf(@NonNull Class<?> keyGenericType, @NonNull Class<?> valueGenericType)
    Check if this is a Map that contains entries with the given key and value generic types.
    boolean
    isSetOf(@NonNull Class<?> genericType)
    Check if this is a Set that contains elements with the given generic type.
    static TypeInfo
    ofParameterizedType(@NonNull ParameterizedType parameterizedType)
    Create a new instance representing a parameterized type such as List<String> or Map<String, Integer>.
    static TypeInfo
    ofSimpleType(@NonNull Type simpleType)
    Create a new instance representing a simple type such as Integer or String, or raw collections.
    static TypeInfo
    ofType(@NonNull Type type)
    Create a new instance representing either a simple type or a parameterized type.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • ofType

      public static TypeInfo ofType(@NonNull Type type)
      Create a new instance representing either a simple type or a parameterized type.
      Parameters:
      type - the type, e.g., String, List<Integer, or Map<String, Integer
      Returns:
      a new instance
      See Also:
    • ofSimpleType

      public static TypeInfo ofSimpleType(@NonNull Type simpleType)
      Create a new instance representing a simple type such as Integer or String, or raw collections.
      Parameters:
      simpleType - the type, e.g., String, but also can represent a raw Collection, Map, etc.
      Returns:
      a new instance with the given raw type and an empty list of generic types
    • ofParameterizedType

      public static TypeInfo ofParameterizedType(@NonNull ParameterizedType parameterizedType)
      Create a new instance representing a parameterized type such as List<String> or Map<String, Integer>.
      Parameters:
      parameterizedType - the parameterized type, e.g. List<Integer>, Map<String, Object>, or Set<String>
      Returns:
      a new instance
    • isCollection

      public boolean isCollection()
      Check if this is a collection (Collection, Set, List).
      Returns:
      true if assignable to Collection, otherwise false
    • isCollectionOf

      public boolean isCollectionOf(@NonNull Class<?> genericType)
      Check if this is a collection (Collection, Set, List) that contains elements with the given generic type.
      Parameters:
      genericType - the exact generic type of the collection
      Returns:
      true if assignable to Collection, and its elements have the exact generic type, otherwise false
    • isListOf

      public boolean isListOf(@NonNull Class<?> genericType)
      Check if this is a List that contains elements with the given generic type.
      Parameters:
      genericType - the exact generic type of the list
      Returns:
      true if assignable to List and its elements have the exact generic type, otherwise false
    • isSetOf

      public boolean isSetOf(@NonNull Class<?> genericType)
      Check if this is a Set that contains elements with the given generic type.
      Parameters:
      genericType - the exact generic type of the set
      Returns:
      true if assignable to Set and its elements have the exact generic type, otherwise false
    • isMap

      public boolean isMap()
      Check if this is a map.
      Returns:
      true if assignable to Map, otherwise false
    • isMapOf

      public boolean isMapOf(@NonNull Class<?> keyGenericType, @NonNull Class<?> valueGenericType)
      Check if this is a Map that contains entries with the given key and value generic types.
      Parameters:
      keyGenericType - the exact generic type of the map keys
      valueGenericType - the exact generic type of the map values
      Returns:
      true if assignable to Map, and its elements have the exact key/value generic types, otherwise false
    • hasExactRawType

      public boolean hasExactRawType(@NonNull Class<?> testType)
      Check if the raw type is exactly the same as testType.
      Parameters:
      testType - the type to test against, e.g., Boolean or String
      Returns:
      true if this has the exact raw type given, otherwise false
    • hasRawTypeAssignableTo

      public boolean hasRawTypeAssignableTo(@NonNull Class<?> testType)
      Check if the raw type is assignable to the testType, for example, a raw type of ArrayList is assignable to List, and a String is assignable to a CharSequence.
      Parameters:
      testType - the type to test against, e.g. Collection, Map, CharSequence
      Returns:
      true if assignable to the given test type, otherwise false
    • getOnlyGenericType

      public Type getOnlyGenericType()
      Assumes this represents a Collection, and returns the single generic type, e.g., for a Set<Integer> then Integer is returned. Throws an exception if this does not represent a Collection with one generic type.
      Returns:
      the generic type of the Collection
      Throws:
      IllegalStateException - if this does not have exactly one generic type
    • getRawType

      public Type getRawType()
      If a simple type, this is the entirety of the type information. If a parameterized type, this is the top-level type, e.g., List or Map.
    • getGenericTypes

      public List<Type> getGenericTypes()
      If a simple type, this will be empty. If a parameterized type, then this list contains the generic types. For example, a List<String> has one generic type, String, while a Map<String, Integer> contains two generic types, String and Integer.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object