Class ConstructorUtil
java.lang.Object
cloud.opencode.base.core.reflect.ConstructorUtil
Constructor Utility Class - Constructor reflection operations
构造器工具类 - 构造器反射操作
Provides utilities for constructor discovery, filtering, and instance creation.
提供构造器发现、过滤和实例创建操作工具。
Features | 主要功能:
- Get all/public/default constructors - 获取全部/公共/默认构造器
- Get by parameter types or annotation - 按参数类型或注解获取
- Get min/max args constructors - 获取最少/最多参数构造器
- Instance creation with type-safe API - 类型安全的实例创建
Usage Examples | 使用示例:
// Get default constructor - 获取默认构造器
Optional<Constructor<User>> ctor = ConstructorUtil.getDefaultConstructor(User.class);
// Create instance - 创建实例
User user = ConstructorUtil.newInstance(User.class);
// Get constructor by param types - 按参数类型获取
Optional<Constructor<User>> ctor = ConstructorUtil.getConstructor(User.class, String.class);
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Returns Optional - 空值安全: 返回 Optional
Performance | 性能特性:
- Time complexity: O(n) where n = declared constructors - O(n), n为声明的构造器数
- Space complexity: O(1) per invocation - 每次调用 O(1)
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<Constructor<T>> getAllConstructors(Class<T> clazz) Gets all constructors 获取所有构造器static <T> Optional<Constructor<T>> getConstructor(Class<T> clazz, Class<?>... paramTypes) Gets the constructor with specified parameter types 获取指定参数类型的构造器static <T> List<Constructor<T>> getConstructorsWithAnnotation(Class<T> clazz, Class<? extends Annotation> annotation) Gets constructors with the specified annotation 获取带有指定注解的构造器static <T> Optional<Constructor<T>> getDefaultConstructor(Class<T> clazz) Gets the default constructor (no-arg) 获取默认构造器(无参)static <T> Optional<Constructor<T>> getMaxArgsConstructor(Class<T> clazz) Gets the constructor with the most parameters 获取参数数量最多的构造器static <T> Optional<Constructor<T>> getMinArgsConstructor(Class<T> clazz) Gets the constructor with the fewest parameters 获取参数数量最少的构造器static String[]getParameterNames(Constructor<?> constructor) Gets constructor parameter names (requires -parameters compile flag) 获取构造器参数名称(需要编译时 -parameters 参数)static <T> List<Constructor<T>> getPublicConstructors(Class<T> clazz) Gets public constructors 获取公共构造器static booleanhasDefaultConstructor(Class<?> clazz) Checks if a default constructor exists 检查是否有默认构造器static booleanisPrivate(Constructor<?> constructor) Checks if the constructor is private 检查构造器是否为 privatestatic booleanisPublic(Constructor<?> constructor) Checks if the constructor is public 检查构造器是否为 publicstatic <T> TnewInstance(Class<T> clazz) Creates an instance using the default constructor 使用默认构造器创建实例static <T> TnewInstance(Constructor<T> constructor, Object... args) Creates an instance 创建实例
-
Method Details
-
getAllConstructors
Gets all constructors 获取所有构造器 -
getPublicConstructors
Gets public constructors 获取公共构造器 -
getDefaultConstructor
Gets the default constructor (no-arg) 获取默认构造器(无参) -
getConstructor
Gets the constructor with specified parameter types 获取指定参数类型的构造器 -
getConstructorsWithAnnotation
public static <T> List<Constructor<T>> getConstructorsWithAnnotation(Class<T> clazz, Class<? extends Annotation> annotation) Gets constructors with the specified annotation 获取带有指定注解的构造器 -
getMinArgsConstructor
Gets the constructor with the fewest parameters 获取参数数量最少的构造器 -
getMaxArgsConstructor
Gets the constructor with the most parameters 获取参数数量最多的构造器 -
hasDefaultConstructor
Checks if a default constructor exists 检查是否有默认构造器 -
isPublic
Checks if the constructor is public 检查构造器是否为 public -
isPrivate
Checks if the constructor is private 检查构造器是否为 private -
newInstance
Creates an instance 创建实例 -
newInstance
Creates an instance using the default constructor 使用默认构造器创建实例 -
getParameterNames
Gets constructor parameter names (requires -parameters compile flag) 获取构造器参数名称(需要编译时 -parameters 参数)
-