Class ClassUtil
java.lang.Object
cloud.opencode.base.reflect.ClassUtil
Class Utility Class
类工具类
Provides low-level class operation utilities with caching.
提供带缓存的底层类操作工具。
Features | 主要功能:
- Class loading with caching - 带缓存的类加载
- Class hierarchy resolution with caching - 带缓存的类层次解析
- Interface resolution with caching - 带缓存的接口解析
- Primitive/wrapper conversion - 原始/包装类型转换
Usage Examples | 使用示例:
Class<?> clazz = ClassUtil.loadClass("com.example.User");
List<Class<?>> hierarchy = ClassUtil.getClassHierarchy(User.class);
Class<?> wrapper = ClassUtil.primitiveToWrapper(int.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(h) for first hierarchy resolution where h is the class hierarchy depth - 时间复杂度: 缓存命中时 O(1);首次层次解析为 O(h),h为类层次深度
- Space complexity: O(h) for the cached hierarchy per class - 空间复杂度: O(h),每类缓存层次
- Since:
- JDK 25, opencode-base-reflect V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidClears all caches 清除所有缓存static voidclearCache(Class<?> clazz) Clears cache for specific class 清除特定类的缓存static booleanChecks if class exists 检查类是否存在static Class<?> Loads class by name (cached) 按名称加载类(缓存)static Class<?> forName(String className, ClassLoader classLoader) Loads class by name with class loader 使用类加载器按名称加载类forNameSafe(String className) Loads class by name safely 安全按名称加载类getAllInterfaces(Class<?> clazz) Gets all interfaces including inherited (cached) 获取所有接口包含继承(缓存)getAllSuperclasses(Class<?> clazz) Gets all superclasses (cached) 获取所有父类(缓存)static Class<?> getArrayClass(Class<?> componentType) Gets array class for component type 获取组件类型的数组类static StringgetCanonicalNameOrName(Class<?> clazz) Gets canonical name or name 获取规范名称或名称getClassHierarchy(Class<?> clazz) Gets full class hierarchy (cached) 获取完整类层次结构(缓存)static Class<?> getCommonSuperclass(Class<?> class1, Class<?> class2) Gets common superclass 获取公共父类static Class<?> getComponentType(Class<?> arrayClass) Gets array component type 获取数组组件类型static StringgetPackageName(Class<?> clazz) Gets package name 获取包名static StringgetSimpleName(Class<?> clazz) Gets simple name 获取简单名称static booleanisAbstract(Class<?> clazz) Checks if abstract class 检查是否为抽象类static booleanisAnonymous(Class<?> clazz) Checks if anonymous class 检查是否为匿名类static booleanChecks if array type 检查是否为数组类型static booleanisAssignableFrom(Class<?> target, Class<?> source) Checks if assignable from 检查是否可从另一类赋值static booleanChecks if enum type 检查是否为枚举类型static booleanChecks if final class 检查是否为final类static booleanisFunctionalInterface(Class<?> clazz) Checks if functional interface 检查是否为函数式接口static booleanisInnerClass(Class<?> clazz) Checks if inner class 检查是否为内部类static booleanisInterface(Class<?> clazz) Checks if interface 检查是否为接口static booleanisPrimitive(Class<?> clazz) Checks if primitive type 检查是否为原始类型static booleanisPrimitiveOrWrapper(Class<?> clazz) Checks if primitive or wrapper 检查是否为原始类型或包装类型static booleanChecks if Record class 检查是否为Record类static booleanisSamePackage(Class<?> class1, Class<?> class2) Checks if same package 检查是否在同一包static booleanChecks if sealed class 检查是否为密封类static booleanChecks if wrapper type 检查是否为包装类型static Class<?> primitiveToWrapper(Class<?> primitiveType) Converts primitive to wrapper 原始类型转包装类型static Class<?> wrapperToPrimitive(Class<?> wrapperType) Converts wrapper to primitive 包装类型转原始类型
-
Method Details
-
forName
Loads class by name (cached) 按名称加载类(缓存)- Parameters:
className- the class name | 类名- Returns:
- the class | 类
- Throws:
OpenReflectException- if not found | 如果未找到
-
forNameSafe
-
forName
Loads class by name with class loader 使用类加载器按名称加载类- Parameters:
className- the class name | 类名classLoader- the class loader | 类加载器- Returns:
- the class | 类
-
exists
Checks if class exists 检查类是否存在- Parameters:
className- the class name | 类名- Returns:
- true if exists | 如果存在返回true
-
getAllSuperclasses
-
getAllInterfaces
-
getClassHierarchy
-
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 检查是否为原始类型或包装类型- Parameters:
clazz- the class | 类- Returns:
- true if primitive or wrapper | 如果是原始或包装类型返回true
-
isRecord
Checks if Record class 检查是否为Record类- Parameters:
clazz- the class | 类- Returns:
- true if record | 如果是Record返回true
-
isSealed
Checks if sealed class 检查是否为密封类- Parameters:
clazz- the class | 类- Returns:
- true if sealed | 如果是密封类返回true
-
isEnum
Checks if enum type 检查是否为枚举类型- Parameters:
clazz- the class | 类- Returns:
- true if enum | 如果是枚举返回true
-
isArray
Checks if array type 检查是否为数组类型- Parameters:
clazz- the class | 类- Returns:
- true if array | 如果是数组返回true
-
isInterface
Checks if interface 检查是否为接口- Parameters:
clazz- the class | 类- Returns:
- true if interface | 如果是接口返回true
-
isAbstract
Checks if abstract class 检查是否为抽象类- Parameters:
clazz- the class | 类- Returns:
- true if abstract | 如果是抽象类返回true
-
isFinal
Checks if final class 检查是否为final类- Parameters:
clazz- the class | 类- Returns:
- true if final | 如果是final返回true
-
isAnonymous
Checks if anonymous class 检查是否为匿名类- Parameters:
clazz- the class | 类- Returns:
- true if anonymous | 如果是匿名类返回true
-
isInnerClass
Checks if inner class 检查是否为内部类- Parameters:
clazz- the class | 类- Returns:
- true if inner | 如果是内部类返回true
-
isFunctionalInterface
Checks if functional interface 检查是否为函数式接口- Parameters:
clazz- the class | 类- Returns:
- true if functional interface | 如果是函数式接口返回true
-
primitiveToWrapper
-
wrapperToPrimitive
-
getComponentType
-
getArrayClass
-
getSimpleName
-
getCanonicalNameOrName
-
getPackageName
-
isSamePackage
-
isAssignableFrom
-
getCommonSuperclass
-
clearCache
public static void clearCache()Clears all caches 清除所有缓存 -
clearCache
Clears cache for specific class 清除特定类的缓存- Parameters:
clazz- the class | 类
-