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
相关推荐
xinxiyinhe1 小时前
如何设置Cursor中.cursorrules文件风与沙的较量丶1 小时前
Java中的局部变量和成员变量在内存中的位置m0_748251721 小时前
SpringBoot3 升级介绍诸神缄默不语1 小时前
如何用Python 3自动打开exe程序橘子师兄2 小时前
分页功能组件开发Logintern092 小时前
使用VS Code进行Python编程的一些快捷方式Multiple-ji2 小时前
想学python进来看看把极客先躯2 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?程序员侠客行2 小时前
Spring事务原理 二小猫猫猫◍˃ᵕ˂◍3 小时前
备忘录模式:快速恢复原始数据