Finds the best equally-matching constructors for the given argument types.
@param <T>
@param clazz
@param argTypes
@return constructors
480793
public static <T> Constructor<T>[] candidateConstructors(Class<T> clazz, Class<?>[] argTypes) {
        return candidateConstructors(collectConstructors(clazz), argTypes);
    }
170194
public static <T> T callConstructor(Class<T> c, Class<?>[] argTypes, Object[] args) {
        try {
            Constructor<T> cons = c.getConstructor(argTypes);
            return cons.newInstance(args);
        } catch(InvocationTargetException e) {
            throw getCause(e);
        } catch(IllegalAccessException e) {
            throw new IllegalStateException(e);
        } catch(NoSuchMethodException e) {
            throw new IllegalArgumentException(e);
        } catch(InstantiationException e) {
            throw new IllegalArgumentException(e);
        }
    }
1300898
public static <T> T newInstanceWithArgs(Class<T> clazz, Class<?>[] argTypes, Object[] args)
        throws SofaRpcRuntimeException {
        if (CommonUtils.isEmpty(argTypes)) {
            return newInstance(clazz);
        }
        try {
            if (!(clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers()))) {
                Constructor<T> constructor = clazz.getDeclaredConstructor(argTypes);
                constructor.setAccessible(true);
                return constructor.newInstance(args);
            } else {
                Constructor<T>[] constructors = (Constructor<T>[]) clazz.getDeclaredConstructors();
                if (constructors == null || constructors.length == 0) {
                    throw new SofaRpcRuntimeException("The " + clazz.getCanonicalName()
                        + " has no constructor with argTypes :" + Arrays.toString(argTypes));
                }
                Constructor<T> constructor = null;
                for (Constructor<T> c : constructors) {
                    Class[] ps = c.getParameterTypes();
                    if (ps.length == argTypes.length + 1) { // 长度多一
                        boolean allMath = true;
                        for (int i = 1; i < ps.length; i++) { // 而且第二个开始的参数类型匹配
                            if (ps[i] != argTypes[i - 1]) {
                                allMath = false;
                                break;
                            }
                        }
                        if (allMath) {
                            constructor = c;
                            break;
                        }
                    }
                }
                if (constructor == null) {
                    throw new SofaRpcRuntimeException("The " + clazz.getCanonicalName()
                        + " has no constructor with argTypes :" + Arrays.toString(argTypes));
                } else {
                    constructor.setAccessible(true);
                    Object[] newArgs = new Object[args.length + 1];
                    System.arraycopy(args, 0, newArgs, 1, args.length);
                    return constructor.newInstance(newArgs);
                }
            }
        } catch (SofaRpcRuntimeException e) {
            throw e;
        } catch (Throwable e) {
            throw new SofaRpcRuntimeException(e.getMessage(), e);
        }
    }
521748
public static Class<?> getTypeArgument(Class<?> clazz, int index) {
		final Type argumentType = TypeUtil.getTypeArgument(clazz, index);
		if (null != argumentType && argumentType instanceof Class) {
			return (Class<?>) argumentType;
		}
		return null;
	}
1005841
static <T> T newInstance(final Class<?> clazz, final Class<?>[] argumentTypes, final Object[] arguments,
        final Class<T> expectedType) {
        // Precondition checks
        if (clazz == null) {
            throw new IllegalArgumentException("Class must be specified");
        }
        if (argumentTypes == null) {
            throw new IllegalArgumentException("ArgumentTypes must be specified. Use empty array if no arguments");
        }
        if (arguments == null) {
            throw new IllegalArgumentException("Arguments must be specified. Use empty array if no arguments");
        }
        if (expectedType == null) {
            throw new IllegalArgumentException("Expected type must be specified");
        }

        // Get the ctor
        final Constructor<?> constructor;
        try {
            constructor = getConstructor(clazz, argumentTypes);
        } catch (final NoSuchMethodException e) {
            throw new RuntimeException("Could not create new instance of " + clazz + " using ctor argument types "
                + Arrays.asList(argumentTypes), e);
        }

        // Create the instance
        final Object obj;
        try {
            obj = constructor.newInstance(arguments);
        } catch (final Exception e) {
            throw new RuntimeException("Could not create a new instance of " + clazz + " using arguments "
                + Arrays.asList(arguments), e);
        }

        // Cast
        try {
            return expectedType.cast(obj);
        } catch (final ClassCastException cce) {
            // Reconstruct so we get some useful information
            throw new ClassCastException("Incorrect expected type, " + expectedType.getName() + ", defined for "
                + obj.getClass().getName());
        }
    }
81369
public static Type getDeclaredBeanType(Class<?> clazz) {
        Type[] actualTypeArguments = Reflections.getActualTypeArguments(clazz);
        if (actualTypeArguments.length == 1) {
            return actualTypeArguments[0];
        } else {
            return null;
        }
    }
1230887
public static Method getPublicMethod(Class<?> clazz, String methodName, Class<?>[] argTypes) {
        assertObjectNotNull("clazz", clazz);
        assertStringNotNullAndNotTrimmedEmpty("methodName", methodName);
        return findMethod(clazz, methodName, argTypes, VisibilityType.PUBLIC, false);
    }
285394
public static Class<?>[] getParamTypes(Object... arguments)
    {
        Check.notNull(arguments);

        final Collection<Object> types = new ArrayList<>(arguments.length);
        for (final Object argument : arguments)
        {
            if (argument.getClass() == Class.class)
            {
                types.add(argument);
            }
            else
            {
                types.add(argument.getClass());
            }
        }
        final Class<?>[] typesArray = new Class<?>[types.size()];
        return types.toArray(typesArray);
    }
1241170
private static Class<?>[] getTypes(final Object... args) {
    final Class<?>[] argTypes = new Class<?>[args.length];
    for (int i = 0; i < argTypes.length; i++) {
      argTypes[i] = args[i].getClass();
    }
    return argTypes;
  }
487900
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) {
		if (clazz == null) throw new IllegalArgumentException("Clazz parameter can't be null");
		
		A annotation = clazz.getAnnotation(annotationType);
		if (annotation != null) return annotation;
		
		for (Class<?> ifc : clazz.getInterfaces()) {
			annotation = findAnnotation(ifc, annotationType);
			if (annotation != null) return annotation;
		}
		
		if (!Annotation.class.isAssignableFrom(clazz))
			for (Annotation ann : clazz.getAnnotations()) {
				annotation = findAnnotation(ann.annotationType(), annotationType);
				if (annotation != null) return annotation;
			}
		
		Class<?> superClass = clazz.getSuperclass();
		if (superClass == null || superClass == Object.class) return null;
		return findAnnotation(superClass, annotationType);
	}
202392
@SuppressWarnings("unchecked")
	public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes) {
		if (null == clazz) {
			return null;
		}

		final Constructor<?>[] constructors = getConstructors(clazz);
		Class<?>[] pts;
		for (Constructor<?> constructor : constructors) {
			pts = constructor.getParameterTypes();
			if (ClassUtil.isAllAssignableFrom(pts, parameterTypes)) {
				// 构造可访问
				constructor.setAccessible(true);
				return (Constructor<T>) constructor;
			}
		}
		return null;
	}
668889
public static List<Class<?>> getAllClasses(Class<?> clazz) {
        List<Class<?>> types = new ArrayList<>();
        while (clazz != null) {
            types.add(clazz);
            clazz = clazz.getSuperclass();
        }
        return types;
    }
79703
@SafeVarargs
    public static <T> Constructor<T> getDeclaredConstructor(final Class<T> cls, final Class<?>... parameterTypes) {
        Map<Class<?>[], Constructor<?>> constructorPool = classDeclaredConstructorPool.get(cls);
        Constructor<T> constructor = null;

        if (constructorPool != null) {
            constructor = (Constructor<T>) constructorPool.get(parameterTypes);
        }

        if (constructor == null) {
            try {
                constructor = cls.getDeclaredConstructor(parameterTypes);

                // SHOULD NOT set it true here.
                // constructor.setAccessible(true);
            } catch (NoSuchMethodException e) {
                // ignore.
            }

            if (constructor != null) {
                if (constructorPool == null) {
                    constructorPool = new ArrayHashMap<>(ConcurrentHashMap.class);
                    classDeclaredConstructorPool.put(cls, constructorPool);
                }

                constructorPool.put(parameterTypes.clone(), constructor);
            }
        }

        return constructor;
    }
536387
public static <T> Supplier<Constructor<T>> constructor(Class<T> clazz, Object... parameters) {
        List<Class<?>> parameterTypes = Lists.newLinkedList();
        for (Object parameter : parameters) {
            parameterTypes.add(parameter.getClass());
        }
        
        return constructor(clazz, parameterTypes.toArray(new Class<?>[parameterTypes.size()]));
    }
940189
public static Converter<?, ?> constructConverter(Class<? extends Converter<?, ?>> clazz) {
		Constructor<? extends Converter<?, ?>> constructor;
		try {
			constructor = clazz.getConstructor();
		} catch (Exception e) {
			throw new IllegalArgumentException("Could not find public no-arg constructor for CSV converter class: "
					+ clazz, e);
		}
		try {
			return constructor.newInstance();
		} catch (Exception e) {
			throw new IllegalArgumentException("Could not construct new CSV converter: " + clazz, e);
		}
	}
960485
private static Constructor<?> findConstructorForStaticClass(final Class<?> klazz) {
    final Constructor<?>[] constructors = klazz.getDeclaredConstructors();
    if (constructors.length == 1) {
      return constructors[0];
    }
    for (final Constructor<?> c : constructors) {
      final Class<?>[] params = c.getParameterTypes();
      if (params.length == 0) {
        return c;
      }
    }
    return constructors[0];
  }
1321248
@SuppressWarnings({ "unchecked", "TryWithIdenticalCatches" })
  public static <T> T init(
      @NonNull Class<T> type, @NonNull Class[] argClasses, @NonNull Object[] args) {
    try {
      Constructor<T> constructor = type.getDeclaredConstructor(argClasses);
      constructor.setAccessible(true);
      return constructor.newInstance(args);
    } catch (NoSuchMethodException e) {
      throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    } catch (InstantiationException e) {
      throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
      throw new RuntimeException(e);
    }
  }