• SpringBeanUtils的部分方法类


    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/12060553.html

    SpringBeanUtils的部分方法类:

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    
    /**
     * TODO
     * SpringBeanUtils的部分方法类
     */
    public class SpringBeanUtils {
    
        /**
         * 实例化一个class
         * @param clazz
         * @param <T>
         * @return
         */
        public static <T> T instantiate(Class<T> clazz) {
            //Assert.notNull(clazz, "not null");
            if (clazz.isInterface()) {
                System.out.println("no interface");
            } else {
                try {
                    return clazz.newInstance();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    System.out.println("constructor not accesbile");
                } catch (InstantiationException e) {
                    e.printStackTrace();
                    System.out.println("not abstract class");
                }
            }
            return null;
        }
    
        /**
         * 实例化一个class
         * @param clazz
         * @param <T>
         * @return
         */
        public static <T> T stanciateClass(Class<T> clazz) {
            //Assert.notNull(clazz, "not null");
            if (clazz.isInterface()) {
                System.out.println("not interface");
            } else {
                try {
                    return instantiateClass(clazz.getDeclaredConstructor());
                } catch (NoSuchMethodException e) {
                    System.out.println("no default constructors");
                } catch (LinkageError e) {
                    System.out.println("unresolvable class definition");
                }
            }
    
            return null;
        }
    
        /**
         * 通过构造器和参数实例化类
         * @param ctor
         * @param args
         * @param <T>
         * @return
         */
        public static <T> T instantiateClass(Constructor<T> ctor, Object... args) {
            //Assert.notNull(ctor, "not null");
            try {
                makeAccessible(ctor);
                /**
                 * 未做kotin检测
                 */
                return ctor.newInstance(args);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
        /**
         * 设置构造器可访问
         * @param ctor
         */
        public static void makeAccessible(Constructor<?> ctor) {
            if ((!Modifier.isPublic(ctor.getModifiers())) || (!Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) {
                ctor.setAccessible(true);
            }
        }
    
        /**
         * 查找方法
         * @param clazz
         * @param methodName
         * @param paramTypes
         * @return
         */
        public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
            try {
                /**
                 * 获取该类和所有父类的public方法
                 */
                return clazz.getMethod(methodName, paramTypes);
            } catch (NoSuchMethodException e) {
                /**
                 * 获取指定的声明过的方法
                 */
                return findDeclaredMethod(clazz, methodName, paramTypes);
            }
        }
    
        /**
         * 获取指定的声明过的方法
         * @param clazz
         * @param methodName
         * @param paramTypes
         * @return
         */
        public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>... paramTypes){
            try {
                return clazz.getDeclaredMethod(methodName, paramTypes);
            } catch (NoSuchMethodException e) {
                return clazz.getSuperclass() != null ? findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes) : null;
            }
        }
    
        /**
         * findMethodWithMinimalParameters
         * 暂时不清楚这个方法干什么用的
         * @param methods
         * @param methodName
         * @return
         */
        public static Method findMethodWithMinimalParameters(Method[] methods, String methodName){
            /**
             * 目标方法
             */
            Method targetMethod = null;
            /**
             * 找到的最小参数方法的数量
             */
            int numMethodsFoundWithCurrentMinimumArgs = 0;
            /**
             * 方法数组
             */
            Method[] methodsArr = methods;
            int methodsSize = methods.length;
    
            for (int i = 0; i < methodsSize; ++i) {
                Method method = methodsArr[i];
                if (method.getName().equals(methodName)){
                    int numParams = method.getParameterCount();
                    if (targetMethod != null && numParams >= targetMethod.getParameterCount()) {
                        if (!method.isBridge() && targetMethod.getParameterCount() == numParams) {
                            if (targetMethod.isBridge()) {
                                targetMethod = method;
                            } else {
                                ++numMethodsFoundWithCurrentMinimumArgs;
                            }
                        }
                    } else {
                        targetMethod = method;
                        numMethodsFoundWithCurrentMinimumArgs = 1;
                    }
                }
            }
    
            if (numMethodsFoundWithCurrentMinimumArgs > 1) {
                throw new IllegalArgumentException("Cannot resolve method '" + methodName + "' to a unique method. Attempted to resolve to overloaded method with the least number of parameters but there were " + numMethodsFoundWithCurrentMinimumArgs + " candidates.");
            } else {
                return targetMethod;
            }
        }
    
    }
  • 相关阅读:
    Linux使用lrzsz上传下载文件
    开发Wordpress主题时没有特色图片的功能
    Windows10重启之后总是将默认浏览器设置为IE
    C#泛型类的类型约束
    CentOS给网站配置Https证书
    从微软官网下载VS离线安装包的方法
    Azure Sql Database为某个数据库创建单独的访问账户
    VS2017/2019 Product Key
    VMware Workstation/Fusion 14/15 密钥
    将DataTable进行分页并生成新的DataTable
  • 原文地址:https://www.cnblogs.com/fanerwei222/p/12060553.html
Copyright © 2020-2023  润新知