public class TypeReference<T> extends Object
Note the source code is copied from com.alibaba.fastjson.TypeReference
Represents a generic type 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.
For example, to create a type literal for List<String>, you can create an empty anonymous inner class:
TypeReference<List<String>> list = new TypeReference<List<String>>() {};
To create a generic list of type with given class:
Type type = TypeReference.listOf(clazz);
This syntax cannot be used to create type literals that have wildcard parameters, such as Class<?> or List<? extends CharSequence>.
| Modifier and Type | Field and Description |
|---|---|
static Type |
LIST_STRING |
protected Type |
type |
| Modifier | Constructor and Description |
|---|---|
protected |
TypeReference()
Constructs a new type literal.
|
| Modifier and Type | Method and Description |
|---|---|
static Type |
collectionOf(Class<?> elementType) |
Type |
getType()
Gets underlying
Type instance. |
static Type |
IterableOf(Class<?> elementType) |
static Type |
listOf(Class<?> elementType) |
static Type |
mapOf(Class<?> keyType,
Class<?> valType) |
static Type |
setOf(Class<?> elementType) |
protected TypeReference()
Constructs a new type literal. Derives represented class from type parameter.
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.
public Type getType()
Gets underlying Type instance.
Copyright © 2014–2021 OSGL (Open Source General Library). All rights reserved.