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
相关推荐
Li emily5 小时前
解决了加密货币api多币种订阅时的数据乱序问题Dicky-_-zhang5 小时前
消息队列Kafka/RocketMQ选型与高可用架构:从单体到100万TPS的演进晨曦中的暮雨5 小时前
4.15腾讯 CSIG云服务产线 一面2301_781571425 小时前
Golang格式化输出占位符都有什么_Golang fmt占位符教程【通俗】fake_ss1985 小时前
AI时代学习全栈项目开发的新范式asdzx675 小时前
使用 Python 为 PDF 添加页码 (详细教程)茉莉玫瑰花茶6 小时前
工作流的常见模式 [ 1 ]AI技术控6 小时前
《Transformers are Inherently Succinct》论文解读:从“能表达什么”到“多紧凑地表达”未若君雅裁6 小时前
Spring AOP、日志切面与声明式事务原理No8g攻城狮7 小时前
【人大金仓】wsl2+ubuntu22.04安装人大金仓数据库V9