Class OpenClass
java.lang.Object
cloud.opencode.base.reflect.OpenClass
Class Facade Entry Class
类门面入口类
Provides common class operations API. Similar to Commons Lang ClassUtils.
提供常用类操作API。 对标Commons Lang ClassUtils。
Features | 主要功能:
- Class loading - 类加载
- Type checking - 类型检查
- Inheritance analysis - 继承分析
- Primitive/wrapper conversion - 原始/包装类型转换
Usage Examples | 使用示例:
// Load class by name
Class<?> clazz = OpenClass.forName("com.example.MyClass");
// Check type
boolean isRecord = OpenClass.isRecord(clazz);
// Get all interfaces
List<Class<?>> interfaces = OpenClass.getAllInterfaces(clazz);
Security | 安全性:
- Thread-safe: Yes (stateless utility class, immutable maps) - 线程安全: 是(无状态工具类,不可变映射)
- 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 TypeMethodDescriptionstatic booleanChecks if a class exists 检查类是否存在static Class<?> Loads a class using context class loader 加载类(使用当前线程类加载器)static Class<?> forName(String className, ClassLoader classLoader) Loads a class with specified class loader 加载类(指定类加载器)forNameSafe(String className) Safely loads a class returning Optional 安全加载类(返回Optional)static Class<?> forNameWithoutInit(String className) Loads a class without initialization 加载类(不初始化)getAllInterfaces(Class<?> clazz) Gets all interfaces 获取所有接口getAllSuperclasses(Class<?> clazz) Gets all superclasses (excluding Object) 获取所有父类(不含Object)static Class<?> getArrayClass(Class<?> componentType) Gets array class for component type 获取数组类型static StringgetCanonicalName(Class<?> clazz) Gets canonical name 获取规范名getClassHierarchy(Class<?> clazz) Gets class hierarchy (from current to Object) 获取继承层次(从当前类到Object)getClassLocation(Class<?> clazz) Gets the class location (JAR file) 获取类所在JAR文件static Class<?> getComponentType(Class<?> arrayClass) Gets array component type 获取数组组件类型static StringgetPackageName(Class<?> clazz) Gets package name 获取包名static Class<?> getPrimitiveClass(String name) Gets a primitive class by name 加载原始类型static StringgetShortClassName(Class<?> clazz) Gets short class name 获取类名(简短)static StringgetSimpleName(Class<?> clazz) Gets simple class name 获取类名(不含包名)static booleanhasDefaultConstructor(Class<?> clazz) Checks if class has default constructor 检查是否有默认构造器static booleanisAbstract(Class<?> clazz) Checks if abstract 检查是否为抽象类static booleanisAnnotation(Class<?> clazz) Checks if annotation 检查是否为注解static booleanisAnonymousClass(Class<?> clazz) Checks if anonymous class 检查是否为匿名类static booleanChecks if array 检查是否为数组static booleanisAssignable(Class<?> target, Class<?> source) Checks if target is assignable from source 检查是否可赋值static booleanisAssignable(Class<?> target, Class<?> source, boolean autoboxing) Checks if target is assignable from source with autoboxing option 检查是否可赋值(支持原始类型转换)static booleanChecks if enum 检查是否为枚举static booleanChecks if final 检查是否为final类static booleanisFunctionalInterface(Class<?> clazz) Checks if functional interface 检查是否为函数式接口static booleanisInnerClass(Class<?> clazz) Checks if inner class 检查是否为内部类static booleanisInstantiable(Class<?> clazz) Checks if class can be instantiated 检查是否可实例化static booleanisInterface(Class<?> clazz) Checks if interface 检查是否为接口static booleanisPrimitive(Class<?> clazz) Checks if primitive type 检查是否为原始类型static booleanisPrimitiveOrWrapper(Class<?> clazz) Checks if primitive or wrapper type 检查是否为原始类型或包装类型static booleanChecks if Record (JDK 16+) 检查是否为Record(JDK 16+)static booleanChecks if Sealed class (JDK 17+) 检查是否为Sealed类(JDK 17+)static booleanChecks if wrapper type 检查是否为包装类型static Class<?> primitiveToWrapper(Class<?> clazz) Converts primitive to wrapper 原始类型转包装类型static Class<?> wrapperToPrimitive(Class<?> clazz) Converts wrapper to primitive 包装类型转原始类型
-
Method Details
-
forName
Loads a class using context class loader 加载类(使用当前线程类加载器)- Parameters:
className- the class name | 类名- Returns:
- the class | 类
- Throws:
OpenReflectException- if not found | 如果未找到
-
forName
Loads a class with specified class loader 加载类(指定类加载器)- Parameters:
className- the class name | 类名classLoader- the class loader | 类加载器- Returns:
- the class | 类
- Throws:
OpenReflectException- if not found | 如果未找到
-
forNameWithoutInit
Loads a class without initialization 加载类(不初始化)- Parameters:
className- the class name | 类名- Returns:
- the class | 类
- Throws:
OpenReflectException- if not found | 如果未找到
-
forNameSafe
-
exists
Checks if a class exists 检查类是否存在- Parameters:
className- the class name | 类名- Returns:
- true if exists | 如果存在返回true
-
getPrimitiveClass
-
getShortClassName
-
getSimpleName
-
getPackageName
-
getCanonicalName
-
getClassLocation
-
isPrimitive
Checks if primitive type 检查是否为原始类型- Parameters:
clazz- the class | 类- Returns:
- true if primitive | 如果是原始类型返回true
-
isWrapper
Checks if wrapper type 检查是否为包装类型- Parameters:
clazz- the class | 类- Returns:
- true if wrapper | 如果是包装类型返回true
-
isPrimitiveOrWrapper
Checks if primitive or wrapper type 检查是否为原始类型或包装类型- Parameters:
clazz- the class | 类- Returns:
- true if primitive or wrapper | 如果是原始类型或包装类型返回true
-
isArray
Checks if array 检查是否为数组- Parameters:
clazz- the class | 类- Returns:
- true if array | 如果是数组返回true
-
isEnum
Checks if enum 检查是否为枚举- Parameters:
clazz- the class | 类- Returns:
- true if enum | 如果是枚举返回true
-
isAnnotation
Checks if annotation 检查是否为注解- Parameters:
clazz- the class | 类- Returns:
- true if annotation | 如果是注解返回true
-
isInterface
Checks if interface 检查是否为接口- Parameters:
clazz- the class | 类- Returns:
- true if interface | 如果是接口返回true
-
isAbstract
Checks if abstract 检查是否为抽象类- Parameters:
clazz- the class | 类- Returns:
- true if abstract | 如果是抽象类返回true
-
isFinal
Checks if final 检查是否为final类- Parameters:
clazz- the class | 类- Returns:
- true if final | 如果是final类返回true
-
isInnerClass
Checks if inner class 检查是否为内部类- Parameters:
clazz- the class | 类- Returns:
- true if inner class | 如果是内部类返回true
-
isAnonymousClass
Checks if anonymous class 检查是否为匿名类- Parameters:
clazz- the class | 类- Returns:
- true if anonymous | 如果是匿名类返回true
-
isRecord
Checks if Record (JDK 16+) 检查是否为Record(JDK 16+)- Parameters:
clazz- the class | 类- Returns:
- true if record | 如果是Record返回true
-
isSealed
Checks if Sealed class (JDK 17+) 检查是否为Sealed类(JDK 17+)- Parameters:
clazz- the class | 类- Returns:
- true if sealed | 如果是Sealed类返回true
-
isFunctionalInterface
Checks if functional interface 检查是否为函数式接口- Parameters:
clazz- the class | 类- Returns:
- true if functional interface | 如果是函数式接口返回true
-
isAssignable
-
isAssignable
Checks if target is assignable from source with autoboxing option 检查是否可赋值(支持原始类型转换)- Parameters:
target- the target class | 目标类source- the source class | 源类autoboxing- whether to consider autoboxing | 是否考虑自动装箱- Returns:
- true if assignable | 如果可赋值返回true
-
getAllSuperclasses
-
getAllInterfaces
-
getClassHierarchy
-
primitiveToWrapper
-
wrapperToPrimitive
-
getComponentType
-
getArrayClass
-
isInstantiable
Checks if class can be instantiated 检查是否可实例化- Parameters:
clazz- the class | 类- Returns:
- true if can be instantiated | 如果可实例化返回true
-
hasDefaultConstructor
Checks if class has default constructor 检查是否有默认构造器- Parameters:
clazz- the class | 类- Returns:
- true if has default constructor | 如果有默认构造器返回true
-