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
相关推荐
代码or搬砖10 分钟前
String字符串Darkershadow1 小时前
python学习之串口通信3824278272 小时前
python:输出JSONAM越.2 小时前
Java设计模式详解--装饰器设计模式(含uml图)5980354152 小时前
【java工具类】小数、整数转中文大写JIngJaneIL2 小时前
基于java + vue个人博客系统(源码+数据库+文档)吃喝不愁霸王餐APP开发者2 小时前
Java后端服务在对接全国性霸王餐API时的多数据中心部署与就近调用策略也许是_2 小时前
大模型应用技术之 详解 MCP 原理从心归零2 小时前
springboot-jpa的批量更新方法这周也會开心3 小时前
128陷阱,==与equals区别