guava:基于TypeToken解析泛型类的类型变量(TypeVariable)的具体类型

对于 Base<F,T>这样的带泛型参数的抽象类,如何在类初始化时得到F,T对应的类型?

Guava TypeToken是一个功能强大的泛型解析工具。可以很简单的实际这个需求。

如下一行代码就可以完成解析:

java 复制代码
Type resolved = new TypeToken<F>(getClass()) {}.getType();

在执行 TypeToken(Class<?> declaringClass)构造方法时已经完成了对F类型参数的解析,保存到runtimeType字段。如下为 TypeToken(Class<?> declaringClass)构造方法的代码:

java 复制代码
  /**
   * 这里declaringClass必须为声明类型变量的类
   */
  protected TypeToken(Class<?> declaringClass) {
    Type captured = super.capture();
    if (captured instanceof Class) {
      this.runtimeType = captured;
    } else {
      /**
       * 非Class的泛型变量,则调用 resolveType 方法解析 T 为具体参数 
       * 由declaringClass创建的TypeToken对象完整保存了类型变量到实际类型的对应表,
       * 所以resolveType方法就是通过查表得到对应具体类型
       */
      this.runtimeType = of(declaringClass).resolveType(captured).runtimeType;
    }
  }

调用示例

java 复制代码
import static org.junit.Assert.*;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import com.google.common.reflect.TypeToken;

/**
 * Guava TypeToken 解析类型变量(TypeVariable)测试
 * @author l0km
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ResolveTypeTest {

	@SuppressWarnings("serial")
	@Test
	public void test() {
		Base<String,Date> base = new Base<String,Date>() {};
		assertEquals(String.class, base.inputType);
		assertEquals(Date.class, base.outputType);

		Sub1<Date> sub1 = new Sub1<Date>() {};
		assertEquals(String.class, sub1.inputType);
		assertEquals(Date.class, sub1.outputType);
		
		Sub2<Map<Integer,Type>> sub2 = new Sub2<Map<Integer,Type>>() {};
		assertEquals(new TypeToken<Map<Integer,Type>>() {}.getType(), sub2.inputType);
		assertEquals(String.class, sub2.outputType);
		
		Sub3<List<Date>> sub3 = new Sub3<List<Date>>() {};
		assertEquals(String.class, sub3.inputType);
		assertEquals(new TypeToken<List<Date>>() {}.getType(), sub3.outputType);

		Sub4<int[]> sub4 = new Sub4<int[]>() {};
		assertEquals(int[].class, sub4.inputType);
		assertEquals(String.class, sub4.outputType);
	}

	public static abstract class Base<F,T>{
		final Type inputType;
		final Type outputType;
		@SuppressWarnings("serial")
		protected Base(){
			inputType = new TypeToken<F>(getClass()) {}.getType();
			System.out.printf("inputType: %s\n",inputType);
			outputType = new TypeToken<T>(getClass()) {}.getType();
			System.out.printf("outputType: %s\n",outputType);
		}
	}
	public static abstract class Sub1<T> extends Base<String,T>{
	}
	public static abstract class Sub2<F> extends Base<F,String>{
	}
	public static abstract class Sub3<T> extends Sub1<T>{
	}
	public static abstract class Sub4<F> extends Sub2<F>{
	}

}
相关推荐
zzh___zzh17 分钟前
SQL 窗口函数 ROW_NUMBER、RANK、DENSE_RANK 常用方法笔记
java
计科土狗26 分钟前
GESP六级专题之类与对象
java·前端·数据库
Slow菜鸟1 小时前
第3篇:实操落地篇 · 3套企业标准工作流(IDEA可视化版)
java·ide·intellij-idea
weixin_440784111 小时前
Java基础常见题
java·开发语言·python·java基础
掉鱼的猫1 小时前
Hot-Plug 实战:运行期管理 Solon 插件
java·hotfix
markinmarkin1 小时前
Spring 中有哪些方式可以把Bean 注入到IOC 容器?
java·spring
带刺的坐椅2 小时前
Hot-Plug 实战:运行期管理 Solon 插件
java·solon·插件·热插拔
大黄说说2 小时前
SQL Server 执行计划怎么看?快速定位 SQL 慢的根源
java·linux·数据库
雪之下雪乃的代码日记2 小时前
Python快速入门(Java开发者版)
java·开发语言·笔记·python
敲个大西瓜3 小时前
Springboot核心面试题
java·spring boot·后端