/**
*在父类使用泛型的情况下,怎样得到子类传进来的是什么类型呢。
*看下面一个例子
*/
/**
*父类dao.java
*/
package com.chj.dao;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class Dao<T>
{
public Dao()
{ //创建子类时调用父类的构造器,所以this指向子类
System.out.println(this);
System.out.println(this.getClass());
/*
*得到子类带参数类型的父类
*public Type getGenericSuperclass()
*返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。
*如果超类是参数化类型,则返回的 Type 对象必须准确反映源代码中所使用的实际类型参数。
*/
Type type = this.getClass().getGenericSuperclass();
System.out.println(type);
Class clazz = null;
//这个类型是否带参数
if(type instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType)type;
//Type[] getActualTypeArguments()
//返回表示此类型实际类型参数的 Type 对象的数组。
Type[] args = parameterizedType.getActualTypeArguments();
if(args != null && args.length > 0){
//得到子类传进来的这个参数类型
Type type1 = args[0];
if(type1 instanceof Class){
clazz = (Class<T>)type1;
}
}
}
System.out.println(clazz);
}
}
/**
*子类PersonDao 继承父类Dao 传进泛型参数Person
*/
package com.chj.dao;
public class PersonDao extends Dao<Person>{}
//Person类
package com.chj.dao;
public class Person{}
//测试类
package com.chj.dao;
import org.junit.Test;
public class TestDao
{
@Test
public void test(){
PersonDao personDao = new PersonDao();
}
}
输出:
com.chj.dao.PersonDao@14514713
class com.chj.dao.PersonDao
com.chj.dao.Dao<com.chj.dao.Person>
class com.chj.dao.Person
/**
* 根据以上原理写一个工具类
* 通过反射,获得定义Class时声明的父类的泛型参数的类型
* 如:public EmployeeDao extends BaseDao<Employee,String>
* @param clazz
* @param index
* @return
*/
public static Class getSuperClassGenricType(Class clazz,int index){
Type genType = clazz.getGenericSuperclass();
if(!(genType instanceof ParameterizedType)){
return Object.class;
}
Type [] params = ((ParameterizedType)genType).getActualTypeArguments();
if(index >= params.length || index < 0){
return Object.class;
}
if(!(params[index] instanceof Class)){
return Object.class;
}
return (Class)params[index];
}
@Test
public void testGetSuperClassGenricType(){
Class clazz = EmployeeDao.class;
Class argClazz = getSuperClassGenricType(clazz,0);
System.out.println(argClazz);
argClazz = getSuperClassGenricType(clazz,1);
System.out.println(argClazz);
}
输出:
class com.chj.dao.Employee
class java.lang.String
*在父类使用泛型的情况下,怎样得到子类传进来的是什么类型呢。
*看下面一个例子
*/
/**
*父类dao.java
*/
package com.chj.dao;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class Dao<T>
{
public Dao()
{ //创建子类时调用父类的构造器,所以this指向子类
System.out.println(this);
System.out.println(this.getClass());
/*
*得到子类带参数类型的父类
*public Type getGenericSuperclass()
*返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。
*如果超类是参数化类型,则返回的 Type 对象必须准确反映源代码中所使用的实际类型参数。
*/
Type type = this.getClass().getGenericSuperclass();
System.out.println(type);
Class clazz = null;
//这个类型是否带参数
if(type instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType)type;
//Type[] getActualTypeArguments()
//返回表示此类型实际类型参数的 Type 对象的数组。
Type[] args = parameterizedType.getActualTypeArguments();
if(args != null && args.length > 0){
//得到子类传进来的这个参数类型
Type type1 = args[0];
if(type1 instanceof Class){
clazz = (Class<T>)type1;
}
}
}
System.out.println(clazz);
}
}
/**
*子类PersonDao 继承父类Dao 传进泛型参数Person
*/
package com.chj.dao;
public class PersonDao extends Dao<Person>{}
//Person类
package com.chj.dao;
public class Person{}
//测试类
package com.chj.dao;
import org.junit.Test;
public class TestDao
{
@Test
public void test(){
PersonDao personDao = new PersonDao();
}
}
输出:
com.chj.dao.PersonDao@14514713
class com.chj.dao.PersonDao
com.chj.dao.Dao<com.chj.dao.Person>
class com.chj.dao.Person
/**
* 根据以上原理写一个工具类
* 通过反射,获得定义Class时声明的父类的泛型参数的类型
* 如:public EmployeeDao extends BaseDao<Employee,String>
* @param clazz
* @param index
* @return
*/
public static Class getSuperClassGenricType(Class clazz,int index){
Type genType = clazz.getGenericSuperclass();
if(!(genType instanceof ParameterizedType)){
return Object.class;
}
Type [] params = ((ParameterizedType)genType).getActualTypeArguments();
if(index >= params.length || index < 0){
return Object.class;
}
if(!(params[index] instanceof Class)){
return Object.class;
}
return (Class)params[index];
}
@Test
public void testGetSuperClassGenricType(){
Class clazz = EmployeeDao.class;
Class argClazz = getSuperClassGenricType(clazz,0);
System.out.println(argClazz);
argClazz = getSuperClassGenricType(clazz,1);
System.out.println(argClazz);
}
输出:
class com.chj.dao.Employee
class java.lang.String