反射时获取 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>
在这里插入图片描述

二、小结

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

支付宝微信
支付宝微信
如果有帮助记得打赏哦特别需要您的打赏哦

版权声明:本文为mamamalululu00000000原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。