反射解析jar包 并获取List集合的泛型类型与数组的对象类型(两种情况实体类属性为List和方法体参数为List)

上传MultipartFile类型jar包,反射解析jar包 获取方法详情,实体类属性或方法体参数为List(数组)时,获取List集合的泛型类型数组的对象类型

直接上代码:(方法体做了简化,其中的Class genericType为泛型类型),有问题或者有更简单的方法请指正...

 1.  方法体参数为集合*(数组)时,获取集合(数组)的泛型类型

    @RequestMapping(value = "/getDubboInfo", method = RequestMethod.POST)
    @ResponseBody
    public voidgetDubboInfo1(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam(value = "dubboFile", required = true) MultipartFile dubboFile,
                               @RequestParam(value = "interfaceName", required = true) String interfaceName,
                               @RequestParam(value = "methodName", required = true) String methodName) {
        String filepath = "D:/";
        try {
            String fileName = dubboFile.getOriginalFilename();
            filepath = filepath + "/" + fileName;
            File jarFile = new File(filepath);
            dubboFile.transferTo(jarFile);
            URL url = jarFile.toURI().toURL();
            URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url}, Thread.currentThread().getContextClassLoader());
            Class myclass = urlClassLoader.loadClass(interfaceName);
            //通过getMethod得到类中包含的方法
            Method[] m = myclass.getDeclaredMethods();
            for (int i = 0; i < m.length; i++) {
                // 得到方法的返回值类型的类类型
                Class returnType = m[i].getReturnType();
                String operation = m[i].getName();//方法名
                if (operation.toLowerCase().equals(methodName.toLowerCase())) {//匹配到对应方法,这里假定方法名唯一,没有方法重载,实际请客要做判断处理
                    Parameter[] params = m[i].getParameters();
                    Map param = new HashMap<>();
                    for (Parameter parameter : params) {
                        String typeName = parameter.getParameterizedType().getTypeName();
                        String name = parameter.getName();
                        param.put(name, typeName);
                        Class classType = parameter.getType();
                            //分别判断参数为数组或集合并获取泛型和数组类型
                            try {
                                if (classType.isArray()) {
                                    Class genericType = classType.getComponentType();
                                    //定义Array的类型
                                }
                                if (classType.isAssignableFrom(List.class)) {
                                    Type type = parameter.getParameterizedType();
                                    ParameterizedType parameterizedType = (ParameterizedType) type;
                                    Type[] types = parameterizedType.getActualTypeArguments();
                                    String genericName = types[0].getTypeName();
                                    //定义List<>中的泛型类
                                    Class genericType = null;
                                    try {
                                        genericType = Class.forName(genericName);
                                    } catch (ClassNotFoundException e) {
                                        genericType = urlClassLoader.loadClass(genericName);
                                    }
                                }
                            } catch (ClassNotFoundException e) {
                                logger.error("方法中参数类型为List和Array时获取泛型类型出错", e);
                            }

                    }
                }
            }
        } catch (MalformedURLException e) {
            logger.error(e.getMessage(), e);
        } catch (ClassNotFoundException e1) {
            logger.error("找不到对应的接口", e1);
        } catch (IOException e1) {
            logger.error(e.getMessage(), e1);
        }

    }

2. 实体类属性为List(数组)时

       try{
            Class cls = classLoader.loadClass(entityName);//实体类类名
            Class genericType = null;
            Field[] fields = cls.getDeclaredFields();
            for(Field field : fields){
                //field.getType()--属性的类型;field.getName()属性的名字
                if(field.getType().isAssignableFrom(List.class)){
                    Type type = field.getGenericType();
                    //得到泛型类型的类名
                    ParameterizedType parameterizedType = (ParameterizedType) type;
                    Type[] types = parameterizedType.getActualTypeArguments();
                    String genericName = types[0].getTypeName();
                    //String genericName = type.getTypeName();
                    try{
                        genericType = Class.forName(genericName);
                    }catch (ClassNotFoundException e){
                        genericType = classLoader.loadClass(genericName);
                    }
                }
            }
        }catch (ClassNotFoundException e){
            logger.config(e.getMessage());
        }


你可能感兴趣的:(反射)