反射时获取 method 里面 List<Object> 里面的具体类型 (正则获取括号内的内容)

反射时获取 method 里面 List<Object> 里面的具体类型

一、 介绍

我们正常反射 获取方法Method 里面的 参数类型, 但是 如果参数是 List , 只能拿到List, 获取不到对应的Object 是什么类型,这里是 泛型语法糖 ,思路如下:

class --->> .getMethod() --->> Method.getParameters() --->> parameter.getDeclaringExecutable().toGenericString(); --->> 获取 到接口的的字符串
//这里是获取全部 路径 ,这是我测试接口
 public abstract void com.test.common.dao.insertEntity(java.util.List<com.test.common.entity.TestEntity>)
--->>> 通过正则 过滤出括号里面的
java.util.List<com.test.common.entity.TestEntity> 
--->>com.test.common.entity.TestEntity    --->> class.forName()    --->> Clazz.newInstance()

 
 
 

接下来通过正则 获取 括号里面的 参数,具体方法就不放了.

给个获取的正则demo:

    public static void main(String[] args) {
        String str ="public abstract void com.test.common.dao.insertEntity(java.util.List<com.test.common.entity.TestEntity>)";
        Matcher matcher = Pattern.compile("(?<=\\()(\\S+)(?=\\))").matcher(str);
        if (matcher.find()) {
            String value = matcher.group();
            System.out.println(value);
        }
    }

输出: java.util.List<com.test.common.entity.TestEntity>
在这里插入图片描述

二、小结

记录一下,其实很简单,当时第一时刻没有拿到里面的具体类型.

支付宝微信
支付宝微信
如果有帮助记得打赏哦特别需要您的打赏哦
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直打铁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值