java 反射 dao_java通过反射获取Dao接口泛型T所指的具体事例和泛型的实例

该代码片段展示了如何使用Java反射API获取类的泛型父类、方法返回类型以及字段类型的泛型参数。通过`getSuperClassGenericType()`获取父类泛型,`getMethodGenericReturnType()`获取方法的泛型返回类型,`getMethodGenericParameterTypes()`获取方法参数的泛型类型,`getFieldGenericType()`获取字段的泛型类型。这些方法检查并处理了泛型索引的有效性。
摘要由CSDN通过智能技术生成

package com.ranred.utils;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

import java.util.ArrayList;

import java.util.List;

public class GenericsUtils {

@SuppressWarnings("unchecked")

public static Class

getSuperClassGenericType(Class clazz,int index){

Type

genType=clazz.getGenericSuperclass();//得到泛型父类

//如果没有实现ParameterizedType接口,即不支持泛型,直接返回Object.class

if(!(genType instanceof

ParameterizedType)){

return Object.class;

}

//返回表示此类型实际类型参数的Type对象的数组,数组里放的都是对应类型的Class, 如BuyerServiceBean

extends DaoSupport就返回Buyer和Contact类型

Type[]

parameters=((ParameterizedType)genType).getActualTypeArguments();

if(index>parameters.length||index<0){

throw new

RuntimeException("你输入的索引号"+(index<0 ? "不能小于0" :

"超出了参数的总数"));

}

if(!(parameters[index] instanceof

Class)){

return Object.class;

}

return (Class)parameters[index];

}

@SuppressWarnings("unchecked")

public static Class

getSuperClassGenericType(Class clazz){{}

return

getSuperClassGenericType(clazz, 0);

}

@SuppressWarnings("unchecked")

public static Class getMethodGenericReturnType(Method

method,int index){

Type returnType=method.getGenericReturnType();

if(returnType instanceof ParameterizedType){

ParameterizedType type=(ParameterizedType)returnType;

Type []typeArguments=type.getActualTypeArguments();

if(index>=typeArguments.length||index<0){

throw new RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" :

"超出了参数的总数"));

}

return (Class)typeArguments[index];

}

return Object.class;

}

@SuppressWarnings("unchecked")

public static Class getMethodGenericReturnType(Method method)

{

return

getMethodGenericReturnType(method, 0);

}

@SuppressWarnings("unchecked")

public static List

getMethodGenericParameterTypes(Method method,int index){

List results=new

ArrayList();

Type[]

genericParameterTypes=method.getGenericParameterTypes();

if(index>genericParameterTypes.length||index<0){

throw new

RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" :

"超出了参数的总数"));

}

Type

genericParamenterType=genericParameterTypes[index];

if(genericParamenterType

instanceof ParameterizedType){

ParameterizedType

aType=(ParameterizedType)genericParamenterType;

Type[]

parameterArgTypes=aType.getActualTypeArguments();

for(Type

parameterArgType:parameterArgTypes){

Class

parameterArgClass=(Class)parameterArgType;

results.add(parameterArgClass);

}

return results;

}

return results;

}

@SuppressWarnings("unchecked")

public static List getMethodGenericParameterTypes(Method

method) {

return

getMethodGenericParameterTypes(method, 0);

}

@SuppressWarnings("unchecked")

public static Class

getFieldGenericType(Field field,int index){

Type

genericFileType=field.getGenericType();

if(genericFileType

instanceof ParameterizedType){

ParameterizedType

aType=(ParameterizedType)genericFileType;

Type[]

fieldArgTypes=aType.getActualTypeArguments();

if(index>fieldArgTypes.length||index<0){

throw new

RuntimeException("你输入的索引"+ (index<0 ? "不能小于0" :

"超出了参数的总数"));

}

return

(Class)fieldArgTypes[index];

}

return

Object.class;

}

@SuppressWarnings("unchecked")

public static Class getFieldGenericType(Field field) {

return

getFieldGenericType(field, 0);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值