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
相关推荐
头发还在的女程序员3 小时前
Java充电桩平台源码解析:枪口状态、上报时间与页面刷新怎样协同timeDifference₇4 小时前
Python Vibe Coding实操实验报告geovindu5 小时前
python:DecisionTreeCosolar5 小时前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录怒放的生命19916 小时前
代码知识库 RAG 问答系统 —— 从 0 到 1 实现教程geovindu6 小时前
python: Decision Tree RegressorIT空门:门主7 小时前
LangChain4j 1.24.0 深度解读:新特性、版本对比与学习路线fengkai45457 小时前
Python运维Fancy.7617 小时前
:Python条件与循环语句学习总结用户3721574261358 小时前
Java 批量操作 Excel 超链接:添加、读取、修改和删除