Class TypeToken<T>
java.lang.Object
cloud.opencode.base.reflect.type.TypeToken<T>
- Type Parameters:
T- the type to capture | 要捕获的类型
- All Implemented Interfaces:
Serializable
Generic Type Token (Similar to Guava TypeToken)
泛型类型令牌(对标 Guava TypeToken)
Captures and preserves generic type information at runtime, solving Java's type erasure problem.
在运行时捕获和保留泛型类型信息,解决Java的类型擦除问题。
Features | 主要功能:
- Runtime generic type capture - 运行时泛型类型捕获
- Type parameter extraction - 类型参数提取
- Type relationship checking - 类型关系检查
- Field/Method type resolution - 字段/方法类型解析
Usage Examples | 使用示例:
// Capture generic type via anonymous subclass
TypeToken<List<String>> listType = new TypeToken<List<String>>() {};
// Get raw type
Class<?> rawType = listType.getRawType(); // List.class
// Get type parameter
TypeToken<?> elementType = listType.getTypeParameter(0); // String
// Convenience methods
TypeToken<List<Integer>> intList = TypeToken.listOf(Integer.class);
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Since:
- JDK 25, opencode-base-reflect V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a TypeToken by capturing generic type from anonymous subclass 通过匿名子类捕获泛型类型创建TypeToken -
Method Summary
Modifier and TypeMethodDescriptionbooleanTypeToken<?> Gets the component type (for arrays) 获取组件类型(用于数组)Gets the raw type (without generic parameters) 获取原始类型(无泛型参数)getType()Gets the underlying Type 获取底层TypeTypeToken<?> getTypeParameter(int index) Gets the type parameter at the specified index 获取指定索引的类型参数Gets all type parameters 获取所有类型参数inthashCode()booleanisArray()Checks if this is an array type 检查是否为数组类型booleanisAssignableFrom(TypeToken<?> other) Checks if this type is assignable from the specified type 检查此类型是否可从指定类型赋值booleanChecks if this is a parameterized type 检查是否为参数化类型booleanChecks if this is a primitive type 检查是否为原始类型booleanisSubtypeOf(TypeToken<?> other) Checks if this type is a subtype of the specified type 检查此类型是否为指定类型的子类型booleanisSupertypeOf(TypeToken<?> other) Checks if this type is a supertype of the specified type 检查此类型是否为指定类型的父类型booleanChecks if this is a type variable 检查是否为类型变量booleanChecks if this is a wildcard type 检查是否为通配符类型Creates a List<E> TypeToken 创建 List<E> TypeTokenCreates a List<E> TypeToken 创建 List<E> TypeTokenCreates a Map<K,V> TypeToken 创建 Map<K,V> TypeTokenCreates a Map<K,V> TypeToken 创建 Map<K,V> TypeTokenstatic <T> TypeToken<T> Creates a TypeToken from Class 从Class创建TypeTokenstatic TypeToken<?> Creates a TypeToken from Type 从Type创建TypeTokenoptionalOf(TypeToken<T> valueType) Creates an Optional<T> TypeToken 创建 Optional<T> TypeTokenoptionalOf(Class<T> valueType) Creates an Optional<T> TypeToken 创建 Optional<T> TypeTokenTypeToken<?> resolveFieldType(Field field) Resolves field type in context of this type 在此类型上下文中解析字段类型resolveParameterTypes(Method method) Resolves method parameter types in context of this type 在此类型上下文中解析方法参数类型TypeToken<?> resolveReturnType(Method method) Resolves method return type in context of this type 在此类型上下文中解析方法返回类型TypeToken<?> resolveType(Type toResolve) Resolves a type in the context of this type 在此类型上下文中解析类型Creates a Set<E> TypeToken 创建 Set<E> TypeTokenCreates a Set<E> TypeToken 创建 Set<E> TypeTokentoString()unwrap()Gets the primitive type (for wrappers) 获取原始类型(对于包装类型)wrap()Gets the wrapper type (for primitives) 获取包装类型(对于原始类型)
-
Constructor Details
-
TypeToken
protected TypeToken()Creates a TypeToken by capturing generic type from anonymous subclass 通过匿名子类捕获泛型类型创建TypeToken
-
-
Method Details
-
of
-
of
-
getType
-
getRawType
-
getTypeParameter
Gets the type parameter at the specified index 获取指定索引的类型参数- Parameters:
index- the index | 索引- Returns:
- the type parameter | 类型参数
-
getTypeParameters
-
getComponentType
Gets the component type (for arrays) 获取组件类型(用于数组)- Returns:
- the component type or null | 组件类型或null
-
isPrimitive
public boolean isPrimitive()Checks if this is a primitive type 检查是否为原始类型- Returns:
- true if primitive | 如果是原始类型返回true
-
isArray
public boolean isArray()Checks if this is an array type 检查是否为数组类型- Returns:
- true if array | 如果是数组返回true
-
isParameterized
public boolean isParameterized()Checks if this is a parameterized type 检查是否为参数化类型- Returns:
- true if parameterized | 如果是参数化类型返回true
-
isWildcard
public boolean isWildcard()Checks if this is a wildcard type 检查是否为通配符类型- Returns:
- true if wildcard | 如果是通配符返回true
-
isTypeVariable
public boolean isTypeVariable()Checks if this is a type variable 检查是否为类型变量- Returns:
- true if type variable | 如果是类型变量返回true
-
isSupertypeOf
Checks if this type is a supertype of the specified type 检查此类型是否为指定类型的父类型- Parameters:
other- the other type | 另一类型- Returns:
- true if supertype | 如果是父类型返回true
-
isSubtypeOf
Checks if this type is a subtype of the specified type 检查此类型是否为指定类型的子类型- Parameters:
other- the other type | 另一类型- Returns:
- true if subtype | 如果是子类型返回true
-
isAssignableFrom
Checks if this type is assignable from the specified type 检查此类型是否可从指定类型赋值- Parameters:
other- the other type | 另一类型- Returns:
- true if assignable | 如果可赋值返回true
-
resolveType
-
wrap
-
unwrap
-
resolveFieldType
-
resolveReturnType
-
resolveParameterTypes
-
listOf
-
listOf
-
setOf
-
setOf
-
mapOf
Creates a Map<K,V> TypeToken 创建 Map<K,V> TypeToken- Type Parameters:
K- the key type | 键类型V- the value type | 值类型- Parameters:
keyType- the key type | 键类型valueType- the value type | 值类型- Returns:
- TypeToken for Map<K,V> | Map<K,V>的TypeToken
-
mapOf
Creates a Map<K,V> TypeToken 创建 Map<K,V> TypeToken- Type Parameters:
K- the key type | 键类型V- the value type | 值类型- Parameters:
keyType- the key class | 键类valueType- the value class | 值类- Returns:
- TypeToken for Map<K,V> | Map<K,V>的TypeToken
-
optionalOf
-
optionalOf
-
equals
-
hashCode
-
toString
-