private void test1() {
Method[] methods = TestObj.class.getMethods();
Method listMethod = null;
Method arrayMethod = null;
for (Method m : methods) {
if (m.getName().equals("ListPara")) {
listMethod = m;
} else if (m.getName().equals("ArrayPara")) {
arrayMethod = m;
}
}
Class<?> arrayParaType = arrayMethod.getParameterTypes()[0];
boolean isArray = arrayParaType.isArray(); //是 Array
/**
* 获取 Array 的元素类型
* */
Class<?> arrayEleType = arrayParaType.getComponentType();
if (MenuInfo.class == arrayEleType) {
System.out.println(arrayEleType.toString());
}
Class<?> listParaType = listMethod.getParameterTypes()[0];
boolean isList = List.class.isAssignableFrom(listParaType); //是 List
/**
* 获取 List 的元素类型
* */
Type listType = listMethod.getGenericParameterTypes()[0];
ParameterizedType parameterizedType = (ParameterizedType) listType;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Class<?> listEleType = (Class<?>) actualTypeArguments[0];
if (MenuInfo.class == listEleType) {
System.out.println(listEleType.toString());
}
}
class TestObj {
public void ListPara(List<MenuInfo> menuInfos) {
//
}
public void ArrayPara(MenuInfo[] menuInfos) {
//
}
}
Java 获取方法的参数是List 或 Array的元素类型
u0104654172024-06-09 17:46
相关推荐
小二·2 小时前
Python Web 开发进阶实战 :AI 原生数字孪生 —— 在 Flask + Three.js 中构建物理世界实时仿真与优化平台Whisper_Sy2 小时前
Flutter for OpenHarmony移动数据使用监管助手App实战 - 网络状态实现乂爻yiyao3 小时前
1.1 JVM 内存区域划分hmywillstronger3 小时前
【Rhino】【Python】 查询指定字段并cloud标注没有bug.的程序员4 小时前
Spring Cloud Eureka:注册中心高可用配置与故障转移实战dyyx1114 小时前
如何从Python初学者进阶为专家?二十雨辰4 小时前
[python]-函数CryptoRzz4 小时前
如何高效接入日本股市实时数据?StockTV API 对接实战指南码农水水4 小时前
中国邮政Java面试被问:容器镜像的多阶段构建和优化若鱼19194 小时前
SpringBoot4.0新特性-BeanRegistrar