一、前言
关于apache的commons-lang中org.apache.commons.lang3.reflect.ConstructorUtils容器工具类对基于类反射容器对象实例化invokeConstructor、获取可用容器getAccessibleConstructor及获取匹配容器getMatchingAccessibleConstructor等操作。
二、源码说明
源码中 org.apache.commons.lang3.ArrayUtils 、org.apache.commons.lang3.ClassUtils、org.apache.commons.lang3.Validate>分别参加其他更多文章
package org.apache.commons.lang3.reflect;@b@@b@import java.lang.reflect.Constructor;@b@import java.lang.reflect.InvocationTargetException;@b@import org.apache.commons.lang3.ArrayUtils;@b@import org.apache.commons.lang3.ClassUtils;@b@import org.apache.commons.lang3.Validate;@b@@b@public class ConstructorUtils@b@{@b@ public static <T> T invokeConstructor(Class<T> cls, Object[] args)@b@ throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException@b@ {@b@ args = ArrayUtils.nullToEmpty(args);@b@ Class[] parameterTypes = ClassUtils.toClass(args);@b@ return invokeConstructor(cls, args, parameterTypes);@b@ }@b@@b@ public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes)@b@ throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException@b@ {@b@ args = ArrayUtils.nullToEmpty(args);@b@ parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);@b@ Constructor ctor = getMatchingAccessibleConstructor(cls, parameterTypes);@b@ if (ctor == null) {@b@ throw new NoSuchMethodException("No such accessible constructor on object: " + cls.getName());@b@ }@b@@b@ return ctor.newInstance(args);@b@ }@b@@b@ public static <T> T invokeExactConstructor(Class<T> cls, Object[] args)@b@ throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException@b@ {@b@ args = ArrayUtils.nullToEmpty(args);@b@ Class[] parameterTypes = ClassUtils.toClass(args);@b@ return invokeExactConstructor(cls, args, parameterTypes);@b@ }@b@@b@ public static <T> T invokeExactConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes)@b@ throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException@b@ {@b@ args = ArrayUtils.nullToEmpty(args);@b@ parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);@b@ Constructor ctor = getAccessibleConstructor(cls, parameterTypes);@b@ if (ctor == null) {@b@ throw new NoSuchMethodException("No such accessible constructor on object: " + cls.getName());@b@ }@b@@b@ return ctor.newInstance(args);@b@ }@b@@b@ public static <T> Constructor<T> getAccessibleConstructor(Class<T> cls, Class<?>[] parameterTypes)@b@ {@b@ Validate.notNull(cls, "class cannot be null", new Object[0]);@b@ try {@b@ return getAccessibleConstructor(cls.getConstructor(parameterTypes)); } catch (NoSuchMethodException e) {@b@ }@b@ return null;@b@ }@b@@b@ public static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor)@b@ {@b@ Validate.notNull(ctor, "constructor cannot be null", new Object[0]);@b@ return (((MemberUtils.isAccessible(ctor)) && (java.lang.reflect.Modifier.isPublic(ctor.getDeclaringClass().getModifiers()))) ? ctor : null);@b@ }@b@@b@ public static <T> Constructor<T> getMatchingAccessibleConstructor(Class<T> cls, Class<?>[] parameterTypes)@b@ {@b@ Constructor result;@b@ Validate.notNull(cls, "class cannot be null", new Object[0]);@b@ try@b@ {@b@ Constructor ctor = cls.getConstructor(parameterTypes);@b@ MemberUtils.setAccessibleWorkaround(ctor);@b@ return ctor;@b@ }@b@ catch (NoSuchMethodException result) {@b@ result = null;@b@@b@ Constructor[] ctors = cls.getConstructors();@b@@b@ Constructor[] arr$ = ctors; int len$ = arr$.length; int i$ = 0; if (i$ >= len$) break label119; Constructor ctor = arr$[i$];@b@@b@ if (ClassUtils.isAssignable(parameterTypes, ctor.getParameterTypes(), true))@b@ {@b@ ctor = getAccessibleConstructor(ctor);@b@ if (ctor != null) {@b@ MemberUtils.setAccessibleWorkaround(ctor);@b@ if ((result == null) || (MemberUtils.compareParameterTypes(ctor.getParameterTypes(), result.getParameterTypes(), parameterTypes) < 0))@b@ {@b@ Constructor constructor = ctor;@b@ result = constructor;@b@ }@b@ }@b@ }@b@ ++i$;@b@ }@b@@b@ label119: return result;@b@ }@b@}