T - the type refered topublic abstract class TypeReference<T>
extends java.lang.Object
T.
Java doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.
This syntax cannot be used to create type literals that have wildcard
parameters, such as Class<T> or List<? extends CharSequence>.
For example, to create a type literal for List<String>, you can
create an empty anonymous inner class:
TypeReference<List<String>> typeReference = new TypeReference<List<String>>(){};
For example, use it quickly:
String text = "{\"id\":1,\"name\":\"kraity\"}";
User user = new TypeReference<User>(){}.parseObject(text);
| Modifier and Type | Field and Description |
|---|---|
protected java.lang.Class<? super T> |
rawType |
protected java.lang.reflect.Type |
type |
| Modifier | Constructor and Description |
|---|---|
|
TypeReference()
Constructs a new type literal.
|
|
TypeReference(java.lang.reflect.Type... actualTypeArguments)
Constructs a new type literal with the specified actual type arguments.
|
private |
TypeReference(java.lang.reflect.Type type)
Constructs a new type literal with the specified type.
|
| Modifier and Type | Method and Description |
|---|---|
static java.lang.reflect.Type |
arrayType(java.lang.Class<?> elementType)
Creates an array type with the specified element type.
|
private static java.lang.reflect.Type |
canonicalize(java.lang.Class<?> thisClass,
java.lang.reflect.ParameterizedType type,
java.lang.reflect.Type[] actualTypeArguments,
int actualIndex)
Canonicalize the type.
|
static java.lang.reflect.Type |
collectionType(java.lang.Class<? extends java.util.Collection> collectionClass,
java.lang.Class<?> elementClass)
Creates a collection type with the specified collection class and element class.
|
static TypeReference<?> |
get(java.lang.reflect.Type type)
Gets a type reference for the specified type.
|
java.lang.Class<? super T> |
getRawType()
Gets the raw type.
|
java.lang.reflect.Type |
getType()
Gets the type literal.
|
static java.lang.reflect.Type |
mapType(java.lang.Class<?> keyClass,
java.lang.reflect.Type valueType)
Creates a map type with the specified key class and value type.
|
static java.lang.reflect.Type |
mapType(java.lang.Class<? extends java.util.Map> mapClass,
java.lang.Class<?> keyClass,
java.lang.Class<?> valueClass)
Creates a map type with the specified map class, key class and value class.
|
static java.lang.reflect.Type |
of(java.lang.reflect.Type... types)
Creates a multi-type from the specified types.
|
static java.lang.reflect.Type |
parametricType(java.lang.Class<?> parametrized,
java.lang.Class<?>... parameterClasses)
Creates a parameterized type with the specified parametrized class and parameter classes.
|
static java.lang.reflect.Type |
parametricType(java.lang.Class<?> parametrized,
java.lang.reflect.Type... parameterTypes)
Creates a parameterized type with the specified parametrized class and parameter types.
|
java.util.List<T> |
parseArray(byte[] utf8Bytes,
JSONReader.Feature... features)
See
JSON.parseArray(byte[], Type, JSONReader.Feature...) for details |
java.util.List<T> |
parseArray(java.lang.String text,
JSONReader.Feature... features)
See
JSON.parseArray(String, JSONReader.Feature...) for details |
T |
parseObject(byte[] utf8Bytes)
See
JSON.parseObject(byte[], Type) for details |
T |
parseObject(java.lang.String text)
See
JSON.parseObject(String, Type) for details |
T |
to(JSONArray array)
See
JSONArray.to(Type) for details |
T |
to(JSONObject object,
JSONReader.Feature... features)
See
JSONObject.to(Type, JSONReader.Feature...) for details |
T |
toJavaObject(JSONArray array)
Deprecated.
since 2.0.4, please use
to(JSONArray) |
T |
toJavaObject(JSONObject object,
JSONReader.Feature... features)
Deprecated.
since 2.0.4, please use
to(JSONObject, JSONReader.Feature...) |
protected final java.lang.reflect.Type type
protected final java.lang.Class<? super T> rawType
public TypeReference()
Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy, so we can reconstitute it at runtime despite erasure.
private TypeReference(java.lang.reflect.Type type)
type - specify the Type to be convertedjava.lang.NullPointerException - If the Type is nullpublic TypeReference(java.lang.reflect.Type... actualTypeArguments)
For example:
Class<T> klass = ...;
TypeReference<Response<T>> ref = new TypeReference<Response<T>>(new Type[]{klass}){};
actualTypeArguments - an array of Type objects representing the actual type arguments to this typejava.lang.NullPointerException - If the actualTypeArguments is null or emptypublic final java.lang.reflect.Type getType()
Typepublic final java.lang.Class<? super T> getRawType()
Classpublic T parseObject(java.lang.String text)
JSON.parseObject(String, Type) for details
String text = "{\"id\":1,\"name\":\"kraity\"}";
User user = new TypeReference<User>(){}.parseObject(text);
text - the JSON String to be parsedpublic T parseObject(byte[] utf8Bytes)
JSON.parseObject(byte[], Type) for details
String utf8Bytes = "{\"id\":1,\"name\":\"kraity\"}".getBytes(StandardCharsets.UTF_8);
User user = new TypeReference<User>(){}.parseObject(utf8Bytes);
utf8Bytes - UTF8 encoded JSON byte array to parsepublic java.util.List<T> parseArray(java.lang.String text, JSONReader.Feature... features)
JSON.parseArray(String, JSONReader.Feature...) for details
String text = "[{\"id\":1,\"name\":\"kraity\"}]";
List<User> users = new TypeReference<User>(){}.parseArray(text);
text - the JSON String to be parsedfeatures - features to be enabled in parsingpublic java.util.List<T> parseArray(byte[] utf8Bytes, JSONReader.Feature... features)
JSON.parseArray(byte[], Type, JSONReader.Feature...) for details
String utf8Bytes = "[{\"id\":1,\"name\":\"kraity\"}]".getBytes(StandardCharsets.UTF_8);
List<User> users = new TypeReference<User>(){}.parseArray(utf8Bytes);
utf8Bytes - UTF8 encoded JSON byte array to parsefeatures - features to be enabled in parsingpublic T to(JSONArray array)
JSONArray.to(Type) for details
JSONArray array = ...
List<User> users = new TypeReference<ArrayList<User>>(){}.to(array);
array - specify the JSONArray to convertpublic T to(JSONObject object, JSONReader.Feature... features)
JSONObject.to(Type, JSONReader.Feature...) for details
JSONObject object = ...
Map<String, User> users = new TypeReference<HashMap<String, User>>(){}.to(object);
object - specify the JSONObject to convertfeatures - features to be enabled in parsing@Deprecated public T toJavaObject(JSONArray array)
to(JSONArray)JSONArray.toJavaObject(Type) for detailsarray - specify the JSONArray to convert@Deprecated public T toJavaObject(JSONObject object, JSONReader.Feature... features)
to(JSONObject, JSONReader.Feature...)JSONObject.to(Type, JSONReader.Feature...) for detailsobject - specify the JSONObject to convertfeatures - features to be enabled in parsingpublic static TypeReference<?> get(java.lang.reflect.Type type)
type - specify the Type to be convertedprivate static java.lang.reflect.Type canonicalize(java.lang.Class<?> thisClass,
java.lang.reflect.ParameterizedType type,
java.lang.reflect.Type[] actualTypeArguments,
int actualIndex)
thisClass - this classtype - the parameterizedTypeactualTypeArguments - an array of Type objects representing the actual type arguments to this typeactualIndex - the actual indexpublic static java.lang.reflect.Type of(java.lang.reflect.Type... types)
types - the typespublic static java.lang.reflect.Type collectionType(java.lang.Class<? extends java.util.Collection> collectionClass,
java.lang.Class<?> elementClass)
collectionClass - the collection classelementClass - the element classpublic static java.lang.reflect.Type arrayType(java.lang.Class<?> elementType)
elementType - the element typepublic static java.lang.reflect.Type mapType(java.lang.Class<? extends java.util.Map> mapClass,
java.lang.Class<?> keyClass,
java.lang.Class<?> valueClass)
mapClass - the map classkeyClass - the key classvalueClass - the value classpublic static java.lang.reflect.Type mapType(java.lang.Class<?> keyClass,
java.lang.reflect.Type valueType)
keyClass - the key classvalueType - the value typepublic static java.lang.reflect.Type parametricType(java.lang.Class<?> parametrized,
java.lang.Class<?>... parameterClasses)
parametrized - the parametrized classparameterClasses - the parameter classespublic static java.lang.reflect.Type parametricType(java.lang.Class<?> parametrized,
java.lang.reflect.Type... parameterTypes)
parametrized - the parametrized classparameterTypes - the parameter types