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
相关推荐
松仔log1 小时前
JetPack——Paging3+Roombiter down5 小时前
14:pytest-order 插件 顺序控制案例测试开发-学习笔记6 小时前
从0开始搭建自动化(一)-appium+python㳺三才人子6 小时前
初探 FlaskLei活在当下6 小时前
先用起来,再理解,关于协程Coroutine应该知道的事Java爱好狂.6 小时前
Java程序员体系化学习路线(2026最新版)AI算法沐枫7 小时前
机器学习到底是什么?tongluowan0077 小时前
以ReentrantLock为例解释AQS的工作流程小技与小术7 小时前
玩转FlaskSilentSamsara7 小时前
Python 性能优化:tracemalloc、profiling 与 C 扩展加速