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
相关推荐
tqs_123451 天前
MySQL RR隔离级别死锁|Gap间隙锁、临键锁,订单并发范围查询死锁根因方案80s7771 天前
动态代理和静态代理的区别,动态代理怎么提高网络安全性逆境不可逃1 天前
Pi Agent 学习笔记:多个工具怎样并行执行Flynt1 天前
Java 27 升级实测:默认值动得比新特性多,有个老参数会让 JVM 直接起不来Bs_MoneyMagnet1 天前
基于springboot+vue的个人健康管理系统的设计与实现 源码+文档星云API技术支持1 天前
企业微信二次开发:群权限设置、成员管理与群资料维护的接口组合实践niucloud-admin1 天前
JAVA V6 多商户商城 开发文档——job 计划任务开发小叶肥辉1 天前
LangChain链和LangGraph图的学习笔记【三】——分别用langchain_openai库和langchain_community库调用大模型2401_873479401 天前
IP属地为什么有时显示外省?用IP查询工具核查动态分配、运营商出口与GeoIP库海宇AI1 天前
零信任架构实战:基于海宇柠檬查出险-登记证构建自动化车抵贷核保网关