Class ConstructorUtil
java.lang.Object
cloud.opencode.base.reflect.ConstructorUtil
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 Summary
Modifier and TypeMethodDescriptionstatic voidClears constructor cache 清除构造器缓存static voidclearCache(Class<?> clazz) Clears cache for specific class 清除特定类的缓存static <T> Constructor<T> findMatchingConstructor(Class<T> clazz, Object... args) Finds best matching constructor for arguments 查找最佳匹配的构造器static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes) Gets constructor by parameter types (cached) 按参数类型获取构造器(缓存)static <T> Constructor<T> getConstructorOrThrow(Class<T> clazz, Class<?>... parameterTypes) Gets constructor or throws exception 获取构造器或抛出异常static <T> List<Constructor<T>> getConstructors(Class<T> clazz, Predicate<Constructor<T>> predicate) Gets constructors matching predicate 获取匹配条件的构造器static <T> List<Constructor<T>> getConstructorsWithAnnotation(Class<T> clazz, Class<? extends Annotation> annotationClass) Gets constructors with annotation 获取带注解的构造器static <T> Constructor<T>[]getDeclaredConstructors(Class<T> clazz) Gets all declared constructors 获取所有声明的构造器static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) Gets default (no-arg) constructor 获取默认(无参)构造器static intgetParameterCount(Constructor<?> constructor) Gets constructor parameter count 获取构造器参数数量static Class<?>[]getParameterTypes(Constructor<?> constructor) Gets constructor parameter types 获取构造器参数类型static <T> List<Constructor<T>> getPublicConstructors(Class<T> clazz) Gets public constructors 获取公共构造器static booleanhasDefaultConstructor(Class<?> clazz) Checks if class has default constructor 检查类是否有默认构造器static booleanisInstantiable(Class<?> clazz) Checks if class is instantiable 检查类是否可实例化static booleanisPrivate(Constructor<?> constructor) Checks if constructor is private 检查构造器是否为私有static booleanisPublic(Constructor<?> constructor) Checks if constructor is public 检查构造器是否为公共static <T> TnewInstance(Class<T> clazz) Creates new instance using default constructor 使用默认构造器创建新实例static <T> TnewInstance(Class<T> clazz, Object... args) Creates new instance with arguments (auto-matching) 使用参数创建新实例(自动匹配)static <T> TnewInstance(Constructor<T> constructor, Object... args) Creates new instance using constructor 使用构造器创建新实例static <T> Optional<T> newInstanceSafe(Class<T> clazz) Creates new instance safely 安全创建新实例
-
Method Details
-
getDeclaredConstructors
Gets all declared constructors 获取所有声明的构造器- Type Parameters:
T- the type | 类型- Parameters:
clazz- the class | 类- Returns:
- array of constructors | 构造器数组
-
getConstructor
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
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
Gets default (no-arg) constructor 获取默认(无参)构造器- Type Parameters:
T- the type | 类型- Parameters:
clazz- the class | 类- Returns:
- the constructor or null | 构造器或null
-
findMatchingConstructor
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
Gets public constructors 获取公共构造器- Type Parameters:
T- the type | 类型- Parameters:
clazz- the class | 类- Returns:
- list of constructors | 构造器列表
-
newInstance
Creates new instance using constructor 使用构造器创建新实例- Type Parameters:
T- the type | 类型- Parameters:
constructor- the constructor | 构造器args- the arguments | 参数- Returns:
- the instance | 实例
-
newInstance
Creates new instance using default constructor 使用默认构造器创建新实例- Type Parameters:
T- the type | 类型- Parameters:
clazz- the class | 类- Returns:
- the instance | 实例
-
newInstance
-
newInstanceSafe
-
getParameterTypes
Gets constructor parameter types 获取构造器参数类型- Parameters:
constructor- the constructor | 构造器- Returns:
- the parameter types | 参数类型
-
getParameterCount
Gets constructor parameter count 获取构造器参数数量- Parameters:
constructor- the constructor | 构造器- Returns:
- the parameter count | 参数数量
-
isPublic
Checks if constructor is public 检查构造器是否为公共- Parameters:
constructor- the constructor | 构造器- Returns:
- true if public | 如果是公共返回true
-
isPrivate
Checks if constructor is private 检查构造器是否为私有- Parameters:
constructor- the constructor | 构造器- Returns:
- true if private | 如果是私有返回true
-
hasDefaultConstructor
Checks if class has default constructor 检查类是否有默认构造器- Parameters:
clazz- the class | 类- Returns:
- true if has default constructor | 如果有默认构造器返回true
-
isInstantiable
Checks if class is instantiable 检查类是否可实例化- Parameters:
clazz- the class | 类- Returns:
- true if instantiable | 如果可实例化返回true
-
clearCache
public static void clearCache()Clears constructor cache 清除构造器缓存 -
clearCache
Clears cache for specific class 清除特定类的缓存- Parameters:
clazz- the class | 类
-