zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    
    }
  • 相关阅读:
    java web 里的JSP 对象的简单了解
    PLSQL 语言 异常 函数 存储过程
    PLSQL语言 分支循环
    高级查询
    oracle 杂项
    sql 里面的常用函数
    java所有内容练习
    线程
    oracle基础 知识点

  • 原文地址:https://www.cnblogs.com/fanerwei222/p/12060553.html
Copyright © 2011-2022 走看看