二、根据方法参数下标索引,获取该方法指定下标参数的名字

    /**
     * 根据方法参数下标索引,获取该方法指定下标参数的名字
     * @param method 方法
     * @param paramIndex 参数下标
     * @return 参数名
     */
    public static String getMethodParamNameByParamIndex(Method method, int paramIndex) {
        try {
            Method getNameMethod = Parameter.class.getMethod("getName");
            Method getParametersMethod = Method.class.getMethod("getParameters");
            Object[] params = (Object[]) getParametersMethod.invoke(method);
            return (String) getNameMethod.invoke(params[paramIndex]);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
发布了444 篇原创文章 · 获赞 113 · 访问量 40万+

猜你喜欢

转载自blog.csdn.net/panchang199266/article/details/103252890