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
相关推荐
小趴蔡ha1 天前
02 Anaconda、Python 与机器学习开发环境入门2401_868534781 天前
RTOS之RT-Thread & Linux:线程/进程管理与调度核心差异分析mifengxing1 天前
LeetCode 41.缺失的第一个正数|Hard题O(n)+O(1)最优解法深度解析数字化转型分享点滴1 天前
富士康、蓝思科技为何选择四化信息?MES制造执行系统的实践wling03011 天前
vasp虚频计算-python脚本魔镜前的帅比1 天前
(开源项目)x-claw(总)xrandzj1 天前
Python面向对象编程入门:类、实例、初始化与封装实践markinmarkin1 天前
Spring 中Bean 的作用域有哪些?山荷枝1 天前
Java学习第十天matlabgoodboy1 天前
计算机毕设代做|Java Python Matlab APP 全套开发设计