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 小时前
Python能做嵌入式开发吗?一份写给动手派的生态与硬件全景图月光船幽幽7 小时前
检测用户意图复杂性的关键方法疯狂打码的少年7 小时前
【数据库技术】关系模型基本概念(关系/属性/元组/键)Mr数据杨9 小时前
【Codex】用学生入学评估模块建立新生能力画像2601_962056239 小时前
Spring Boot 入门 与 无法解析符号 springframework 的解决Thomas214310 小时前
Java scala 数组 链表威联通网络存储10 小时前
TS-h3087XU-RP 汽车零部件质检追溯部署亚历克斯神10 小时前
低代码与 AI 的融合趋势——从可视化搭建到自然语言生成应用你驴我10 小时前
WhatsApp 多账号场景下的会话归档与历史消息检索优化实践IT毕设实战小研10 小时前
基于安卓的空气质量查询系统