Class OpenConstructor

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

public final class OpenConstructor extends Object
Constructor Facade Entry Class 构造器门面入口类

Provides common constructor operations API. Similar to Commons Lang ConstructorUtils.

提供常用构造器操作API。 对标Commons Lang ConstructorUtils。

Features | 主要功能:

  • Constructor retrieval - 构造器获取
  • Instance creation - 实例创建
  • Factory method discovery - 工厂方法发现

Usage Examples | 使用示例:

// Create instance with default constructor
MyClass obj = OpenConstructor.newInstance(MyClass.class);

// Create instance with arguments
MyClass obj = OpenConstructor.newInstance(MyClass.class, "name", 25);

// Find factory method
Optional<Method> factory = OpenConstructor.findFactoryMethod(MyClass.class, "of");

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getConstructor

      public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes)
      Gets a constructor (exact parameter types) 获取构造器(精确参数类型)
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      parameterTypes - the parameter types | 参数类型
      Returns:
      the constructor | 构造器
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getConstructor

      public static <T> Constructor<T> getConstructor(Class<T> clazz, boolean forceAccess, Class<?>... parameterTypes)
      Gets a constructor with optional force access 获取构造器(可选强制访问)
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      forceAccess - whether to force access | 是否强制访问
      parameterTypes - the parameter types | 参数类型
      Returns:
      the constructor | 构造器
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getMatchingConstructor

      public static <T> Constructor<T> getMatchingConstructor(Class<T> clazz, Class<?>... parameterTypes)
      Gets a matching constructor (compatible parameter types) 获取匹配构造器(兼容参数类型)
      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 the default (no-arg) constructor 获取无参构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      the constructor | 构造器
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getConstructors

      public static <T> List<Constructor<T>> getConstructors(Class<T> clazz)
      Gets all constructors 获取所有构造器
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      list of constructors | 构造器列表
    • getConstructorsWithAnnotation

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

      public static <T> T newInstance(Class<T> clazz)
      Creates an 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 an instance with auto-matching constructor 创建实例(自动匹配构造器)
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      args - the arguments | 参数
      Returns:
      the instance | 实例
    • newInstance

      public static <T> T newInstance(Class<T> clazz, Class<?>[] parameterTypes, Object... args)
      Creates an instance with specified parameter types 创建实例(指定参数类型)
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      parameterTypes - the parameter types | 参数类型
      args - the arguments | 参数
      Returns:
      the instance | 实例
    • newInstance

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

      public static <T> T newInstanceForced(Class<T> clazz, Object... args)
      Forces creation of an instance (bypasses private constructors) 强制创建实例(绕过私有构造器)
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      args - the arguments | 参数
      Returns:
      the instance | 实例
    • hasDefaultConstructor

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

      public static boolean hasConstructor(Class<?> clazz, Class<?>... parameterTypes)
      Checks if class has constructor with specified parameters 检查是否有指定参数构造器
      Parameters:
      clazz - the class | 类
      parameterTypes - the parameter types | 参数类型
      Returns:
      true if has constructor | 如果有构造器返回true
    • getParameterNames

      public static List<String> getParameterNames(Constructor<?> constructor)
      Gets constructor parameter names 获取构造器参数名称
      Parameters:
      constructor - the constructor | 构造器
      Returns:
      list of parameter names | 参数名列表
    • getParameterTypes

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

      public static <T> Optional<Method> findFactoryMethod(Class<T> clazz, String methodName)
      Finds a static factory method 查找静态工厂方法
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      methodName - the method name | 方法名
      Returns:
      Optional of method | 方法的Optional
    • findFactoryMethods

      public static <T> List<Method> findFactoryMethods(Class<T> clazz)
      Finds all static factory methods 查找所有静态工厂方法
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      list of methods | 方法列表
    • newInstanceByFactory

      public static <T> T newInstanceByFactory(Class<T> clazz, String factoryMethod, Object... args)
      Creates an instance via factory method 通过工厂方法创建实例
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      factoryMethod - the factory method name | 工厂方法名
      args - the arguments | 参数
      Returns:
      the instance | 实例
    • toInvokable

      public static <T> Invokable<T,T> toInvokable(Constructor<T> constructor)
      Converts to Invokable 转为Invokable
      Type Parameters:
      T - the type | 类型
      Parameters:
      constructor - the constructor | 构造器
      Returns:
      Invokable | Invokable