Class OpenConstructor
java.lang.Object
cloud.opencode.base.reflect.OpenConstructor
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 Summary
Modifier and TypeMethodDescriptionfindFactoryMethod(Class<T> clazz, String methodName) Finds a static factory method 查找静态工厂方法findFactoryMethods(Class<T> clazz) Finds all static factory methods 查找所有静态工厂方法static <T> Constructor<T> getConstructor(Class<T> clazz, boolean forceAccess, Class<?>... parameterTypes) Gets a constructor with optional force access 获取构造器(可选强制访问)static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes) Gets a constructor (exact parameter types) 获取构造器(精确参数类型)static <T> List<Constructor<T>> getConstructors(Class<T> clazz) Gets all constructors 获取所有构造器static <T> List<Constructor<T>> getConstructorsWithAnnotation(Class<T> clazz, Class<? extends Annotation> annotationClass) Gets constructors with specific annotation 按注解获取构造器static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) Gets the default (no-arg) constructor 获取无参构造器static <T> Constructor<T> getMatchingConstructor(Class<T> clazz, Class<?>... parameterTypes) Gets a matching constructor (compatible parameter types) 获取匹配构造器(兼容参数类型)getParameterNames(Constructor<?> constructor) Gets constructor parameter names 获取构造器参数名称static Class<?>[]getParameterTypes(Constructor<?> constructor) Gets constructor parameter types 获取构造器参数类型static booleanhasConstructor(Class<?> clazz, Class<?>... parameterTypes) Checks if class has constructor with specified parameters 检查是否有指定参数构造器static booleanhasDefaultConstructor(Class<?> clazz) Checks if class has default constructor 检查是否有无参构造器static <T> TnewInstance(Class<T> clazz) Creates an instance using default constructor 创建实例(使用无参构造器)static <T> TnewInstance(Class<T> clazz, Class<?>[] parameterTypes, Object... args) Creates an instance with specified parameter types 创建实例(指定参数类型)static <T> TnewInstance(Class<T> clazz, Object... args) Creates an instance with auto-matching constructor 创建实例(自动匹配构造器)static <T> TnewInstance(Constructor<T> constructor, Object... args) Creates an instance using Constructor object 创建实例(Constructor对象)static <T> TnewInstanceByFactory(Class<T> clazz, String factoryMethod, Object... args) Creates an instance via factory method 通过工厂方法创建实例static <T> TnewInstanceForced(Class<T> clazz, Object... args) Forces creation of an instance (bypasses private constructors) 强制创建实例(绕过私有构造器)static <T> Invokable<T, T> toInvokable(Constructor<T> constructor) Converts to Invokable 转为Invokable
-
Method Details
-
getConstructor
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
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
Gets the default (no-arg) constructor 获取无参构造器- Type Parameters:
T- the type | 类型- Parameters:
clazz- the class | 类- Returns:
- the constructor | 构造器
- Throws:
OpenReflectException- if not found | 如果未找到
-
getConstructors
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
Creates an instance using default constructor 创建实例(使用无参构造器)- Type Parameters:
T- the type | 类型- Parameters:
clazz- the class | 类- Returns:
- the instance | 实例
-
newInstance
-
newInstance
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
Creates an instance using Constructor object 创建实例(Constructor对象)- Type Parameters:
T- the type | 类型- Parameters:
constructor- the constructor | 构造器args- the arguments | 参数- Returns:
- the instance | 实例
-
newInstanceForced
-
hasDefaultConstructor
Checks if class has default constructor 检查是否有无参构造器- Parameters:
clazz- the class | 类- Returns:
- true if has default constructor | 如果有无参构造器返回true
-
hasConstructor
-
getParameterNames
Gets constructor parameter names 获取构造器参数名称- Parameters:
constructor- the constructor | 构造器- Returns:
- list of parameter names | 参数名列表
-
getParameterTypes
Gets constructor parameter types 获取构造器参数类型- Parameters:
constructor- the constructor | 构造器- Returns:
- array of parameter types | 参数类型数组
-
findFactoryMethod
-
findFactoryMethods
-
newInstanceByFactory
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
Converts to Invokable 转为Invokable- Type Parameters:
T- the type | 类型- Parameters:
constructor- the constructor | 构造器- Returns:
- Invokable | Invokable
-