Package org.kiwiproject.beta.reflect
Class TypeInfo
java.lang.Object
org.kiwiproject.beta.reflect.TypeInfo
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 TypeMethodDescriptionbooleanIf a simple type, this will be empty.Assumes this represents a Collection, and returns the single generic type, e.g., for aSet<Integer>then Integer is returned.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.booleanCheck 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.booleanCheck if this is a List that contains elements with the given generic type.booleanisMap()Check if this is a map.booleanCheck if this is a Map that contains entries with the given key and value generic types.booleanCheck 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 TypeInfoCreate a new instance representing either a simple type or a parameterized type.toString()
-
Method Details
-
ofType
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
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
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
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
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
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
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
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
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
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
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
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. -
equals
-
hashCode
public int hashCode() -
toString
-