Package org.kiwiproject.beta.reflect
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>orMap<String, Integer>).
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)List<Type>getGenericTypes()If a simple type, this will be empty.TypegetOnlyGenericType()Assumes this represents a Collection, and returns the single generic type, e.g.TypegetRawType()If a simple type, this is the entirety of the type information.booleanhasExactRawType(@NonNull Class<?> testType)Check if the raw type is exactly the same astestType.inthashCode()booleanhasRawTypeAssignableTo(@NonNull Class<?> testType)Check if the raw type is assignable to thetestType, for example a raw type of ArrayList is assignable to List, and a String is assignable to a CharSequence.booleanisCollection()Check if this is a collection (Collection, Set, List).booleanisCollectionOf(@NonNull Class<?> genericType)Check if this is a collection (Collection, Set, List) that contains elements with the given generic type.booleanisListOf(@NonNull Class<?> genericType)Check if this is a List that contains elements with the given generic type.booleanisMap()Check if this is a map.booleanisMapOf(@NonNull Class<?> keyGenericType, @NonNull Class<?> valueGenericType)Check if this is a Map that contains entries with the given key and value generic types.booleanisSetOf(@NonNull Class<?> genericType)Check if this is a Set that contains elements with the given generic type.static TypeInfoofParameterizedType(@NonNull ParameterizedType parameterizedType)Create a new instance representing a parameterized type such asList<String>orMap<String, Integer>.static TypeInfoofSimpleType(@NonNull Type simpleType)Create a new instance representing a simple type such as Integer or String, or raw collections.static TypeInfoofType(@NonNull Type type)Create a new instance representing either a simple type or a parameterized type.StringtoString()
-
-
-
Method Detail
-
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, orMap<String, Integer- Returns:
- a new instance
- See Also:
ofSimpleType(Type),ofParameterizedType(ParameterizedType)
-
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 asList<String>orMap<String, Integer>.- Parameters:
parameterizedType- the parameterized type, e.g.List<Integer>,Map<String, Object>, orSet<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 keysvalueGenericType- 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 astestType.- 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 thetestType, 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 aSet<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, aList<String>has one generic type, String, while aMap<String, Integer>contains two generic types, String and Integer.
-
-