Class ConstructorUtil

java.lang.Object
cloud.opencode.base.reflect.ConstructorUtil

public final class ConstructorUtil extends Object
Constructor Utility Class 构造器工具类

Provides low-level constructor operation utilities with caching.

提供带缓存的底层构造器操作工具。

Features | 主要功能:

  • Constructor discovery with caching - 带缓存的构造器发现
  • Parameter type matching - 参数类型匹配
  • Constructor filtering by annotation - 按注解过滤构造器

Usage Examples | 使用示例:

Constructor<?> ctor = ConstructorUtil.getConstructor(User.class, String.class, int.class);
boolean hasDefault = ConstructorUtil.hasDefaultConstructor(User.class);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap缓存)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)

Performance | 性能特性:

  • Time complexity: O(1) for cached lookups; O(c) for first access where c is the number of constructors - 时间复杂度: 缓存命中时 O(1);首次访问为 O(c),c为构造器数量
  • Space complexity: O(c) for the cached constructors per class - 空间复杂度: O(c),每类缓存构造器
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getDeclaredConstructors

      public static <T> Constructor<T>[] getDeclaredConstructors(Class<T> clazz)
      Gets all declared constructors 获取所有声明的构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      array of constructors | 构造器数组
    • getConstructor

      public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes)
      Gets constructor by parameter types (cached) 按参数类型获取构造器(缓存)
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      parameterTypes - the parameter types | 参数类型
      Returns:
      the constructor or null | 构造器或null
    • getConstructorOrThrow

      public static <T> Constructor<T> getConstructorOrThrow(Class<T> clazz, Class<?>... parameterTypes)
      Gets constructor or throws exception 获取构造器或抛出异常
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      parameterTypes - the parameter types | 参数类型
      Returns:
      the constructor | 构造器
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getDefaultConstructor

      public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz)
      Gets default (no-arg) constructor 获取默认(无参)构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      the constructor or null | 构造器或null
    • findMatchingConstructor

      public static <T> Constructor<T> findMatchingConstructor(Class<T> clazz, Object... args)
      Finds best matching constructor for arguments 查找最佳匹配的构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      args - the arguments | 参数
      Returns:
      the constructor or null | 构造器或null
    • getConstructors

      public static <T> List<Constructor<T>> getConstructors(Class<T> clazz, Predicate<Constructor<T>> predicate)
      Gets constructors matching predicate 获取匹配条件的构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      predicate - the predicate | 谓词
      Returns:
      list of matching constructors | 匹配的构造器列表
    • getConstructorsWithAnnotation

      public static <T> List<Constructor<T>> getConstructorsWithAnnotation(Class<T> clazz, Class<? extends Annotation> annotationClass)
      Gets constructors with annotation 获取带注解的构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      annotationClass - the annotation class | 注解类
      Returns:
      list of constructors | 构造器列表
    • getPublicConstructors

      public static <T> List<Constructor<T>> getPublicConstructors(Class<T> clazz)
      Gets public constructors 获取公共构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      list of constructors | 构造器列表
    • newInstance

      public static <T> T newInstance(Constructor<T> constructor, Object... args)
      Creates new instance using constructor 使用构造器创建新实例
      Type Parameters:
      T - the type | 类型
      Parameters:
      constructor - the constructor | 构造器
      args - the arguments | 参数
      Returns:
      the instance | 实例
    • newInstance

      public static <T> T newInstance(Class<T> clazz)
      Creates new instance using default constructor 使用默认构造器创建新实例
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      the instance | 实例
    • newInstance

      public static <T> T newInstance(Class<T> clazz, Object... args)
      Creates new instance with arguments (auto-matching) 使用参数创建新实例(自动匹配)
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      args - the arguments | 参数
      Returns:
      the instance | 实例
    • newInstanceSafe

      public static <T> Optional<T> newInstanceSafe(Class<T> clazz)
      Creates new instance safely 安全创建新实例
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      Optional of instance | 实例的Optional
    • getParameterTypes

      public static Class<?>[] getParameterTypes(Constructor<?> constructor)
      Gets constructor parameter types 获取构造器参数类型
      Parameters:
      constructor - the constructor | 构造器
      Returns:
      the parameter types | 参数类型
    • getParameterCount

      public static int getParameterCount(Constructor<?> constructor)
      Gets constructor parameter count 获取构造器参数数量
      Parameters:
      constructor - the constructor | 构造器
      Returns:
      the parameter count | 参数数量
    • isPublic

      public static boolean isPublic(Constructor<?> constructor)
      Checks if constructor is public 检查构造器是否为公共
      Parameters:
      constructor - the constructor | 构造器
      Returns:
      true if public | 如果是公共返回true
    • isPrivate

      public static boolean isPrivate(Constructor<?> constructor)
      Checks if constructor is private 检查构造器是否为私有
      Parameters:
      constructor - the constructor | 构造器
      Returns:
      true if private | 如果是私有返回true
    • hasDefaultConstructor

      public static boolean hasDefaultConstructor(Class<?> clazz)
      Checks if class has default constructor 检查类是否有默认构造器
      Parameters:
      clazz - the class | 类
      Returns:
      true if has default constructor | 如果有默认构造器返回true
    • isInstantiable

      public static boolean isInstantiable(Class<?> clazz)
      Checks if class is instantiable 检查类是否可实例化
      Parameters:
      clazz - the class | 类
      Returns:
      true if instantiable | 如果可实例化返回true
    • clearCache

      public static void clearCache()
      Clears constructor cache 清除构造器缓存
    • clearCache

      public static void clearCache(Class<?> clazz)
      Clears cache for specific class 清除特定类的缓存
      Parameters:
      clazz - the class | 类