原創:轉載需注明原創地址 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; } } }