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>{
	}

}
相关推荐
一灯架构5 小时前
90%的人答错!一文带你彻底搞懂ArrayList
java·后端
Y4090016 小时前
【多线程】线程安全(1)
java·开发语言·jvm
布局呆星6 小时前
SpringBoot 基础入门
java·spring boot·spring
风吹迎面入袖凉7 小时前
【Redis】Redisson的可重入锁原理
java·redis
w6100104667 小时前
cka-2026-ConfigMap
java·linux·cka·configmap
语戚7 小时前
力扣 968. 监控二叉树 —— 贪心 & 树形 DP 双解法递归 + 非递归全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·
quxuexi8 小时前
网络通信安全与可靠传输:从加密到认证,从状态码到可靠传输
java·安全·web
hrhcode8 小时前
【java工程师快速上手go】二.Go进阶特性
java·golang·go
小碗羊肉10 小时前
【从零开始学Java | 第三十一篇下】Stream流
java·开发语言