获取泛型类的真实参数

获得某个类的泛型

package com;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


import org.springframework.core.ResolvableType;


interface Service2<N, M> {
}
class ABService2 implements Service2<A, B> {}


public class TestGenericClass2 {
	private Service<C, D> cdService;
	
	public static void main(String[] args) {
		//获取ABService2实现的第一个接口(从0开始计数) 即Service2<A, B>
		ParameterizedType parameterizedType = (ParameterizedType) ABService2.class.getGenericInterfaces()[0];
		System.out.println(parameterizedType);
		//从Service2<A, B> 获得第0个参数 即A
		Type genericType = parameterizedType.getActualTypeArguments()[0];
		System.out.println(genericType);
		System.out.println("*************");
		//下面的可以获得被aop代理之前的类型
		//ClassUtils.getUserClass(myClass);
		ResolvableType resolvableType1 = ResolvableType.forClass(ABService2.class);  
		
		Class c=resolvableType1.as(Service2.class).getGeneric(0).resolve();
		Class d=resolvableType1.getInterfaces()[0].getGeneric(0).resolve();
		System.out.println("c   "+c);
		System.out.println("d   "+d);
		//上面的c与d一样 说明resolvableType1.as(Service2.class)与resolvableType1.getInterfaces()[0]
		//是一样的 都是返回resolvableType1 实现的某个接口
		
		System.out.println("__________");		
	}
}
输出:
com.Service2<com.A, com.B>
class com.A
*************
c   class com.A
d   class com.A
---------------

上面代码里,*************之前的是使用jdk的原始api,之后是使用spring为我们通过的泛型api


使用jdk原始的api,似乎有个问题,那就是如果,如果目标类是继承( 而不是实现)了某个泛型类,那就(似乎)没法获得参数类型了
(这个,我也不确定,如果诸位谁知道,麻烦告诉小弟一声)
package com;


import org.springframework.core.ResolvableType;


interface Service3<N, M> {}
class ServiceImpl3<N,M> implements Service<N, M>{}

class C3 {}
class D3 {}

class CDService3 extends ServiceImpl<C3, D3> {}

public class TestGenericClass3 {
	
	public static void main(String[] args) {
		ResolvableType resolvableType1 = ResolvableType.forClass(CDService3.class);  
		Class c=resolvableType1.as(ServiceImpl.class).getGeneric(0).resolve();
		System.out.println("c   "+c);
	}
}
输出
c   class com.C3
***********

获得成员变量的泛型类型

package com;

import org.springframework.core.ResolvableType;
import org.springframework.util.ReflectionUtils;

interface Service4<N, M> {}
class ServiceImpl4<N,M> implements Service<N, M>{}

class C4 {}

class D4 {}

class CDService4 extends ServiceImpl4<C4, D4> {
}


public class TestGenericClass4 {
	private Service<C, D> cdService;
	
	public static void main(String[] args) {


		ResolvableType resolvableType2 =  
                ResolvableType.forField(ReflectionUtils.findField(TestGenericClass.class, "cdService")); 
		System.out.println(resolvableType2.getGeneric(0).resolve());
			
	}
}
输出:
class com.C


在含泛型的父类中,获得子类的参数

说的很模糊,大家看代码:
package com;

import org.springframework.core.ResolvableType;
import org.springframework.util.ClassUtils;

interface Service<N, M> {
}
class ServiceImpl<N,M> implements Service<N, M>{
	private Class<?> entityN;
	private Class<?> entityM;
	
	public ServiceImpl(){
		System.out.println("this is "+this.getClass());
		//用于避过aop 获得原始的类型
		Class<?> childClass=ClassUtils.getUserClass(this.getClass());
		if (childClass.equals(this.getClass())) {
			System.out.println("一样");
		}
		ResolvableType resolvableType1 = ResolvableType.forClass(childClass);  
		entityN=resolvableType1.as(ServiceImpl.class).getGeneric(0).resolve();
		entityM=resolvableType1.as(ServiceImpl.class).getGeneric(1).resolve();
		System.out.println(entityN+"  **  "+entityM);
	}
}
class A {}

class B {}


class ABService extends ServiceImpl<A, B> {


}


public class TestGenericClass {
	
	public static void main(String[] args) {
	
		Service<A, B> abService= (Service<A, B>) new ABService();
		System.out.println("************* "+abService);		
	}
}
输出
this is class com.ABService
一样
class com.A  **  class com.B
************* com.ABService@3358fd70


关于更多的泛型资料,请见参考资料
顺便多说一句,开涛哥真牛。
感谢glt。




参考资料

http://jinnianshilongnian.iteye.com/blog/1993608

你可能感兴趣的:(泛型,Class,getClass,getGeneric)