Class OpenClass
java.lang.Object
cloud.opencode.base.core.OpenClass
Class Utility Class - Class loading, type checking, generics handling and classpath operations
Class 工具类 - 类加载、类型判断、泛型处理和类路径操作
Provides comprehensive class operations including loading, type checking, generics and classpath operations.
提供全面的类操作,包括类加载、类型判断、泛型处理、包扫描和类路径操作。
Features | 主要功能:
- Class loading (loadClass, loadClassSafely, forName) - 类加载
- Type checking (isPrimitive, isWrapper, isArray, isEnum) - 类型判断
- Primitive/Wrapper conversion (wrap, unwrap, getDefaultValue) - 原始/包装类型转换
- Generics handling (getTypeArguments, getSuperclassTypeArguments) - 泛型处理
- Hierarchy operations (getAllSuperclasses, getAllInterfaces, getCommonSuperClass) - 类层次操作
- Classpath operations (getResource, getResourceAsStream, getJarPath) - 类路径操作
- Instantiation (newInstance) - 实例化
Usage Examples | 使用示例:
// Class loading - 类加载
Class<?> clazz = OpenClass.loadClass("com.example.MyClass");
Optional<Class<?>> opt = OpenClass.loadClassSafely("Unknown");
// Type checking - 类型判断
boolean isPrimitive = OpenClass.isPrimitive(int.class); // true
boolean isWrapper = OpenClass.isWrapper(Integer.class); // true
// Primitive/Wrapper conversion - 类型转换
Class<?> wrapped = OpenClass.wrap(int.class); // Integer.class
Object defaultVal = OpenClass.getDefaultValue(int.class); // 0
// Generics - 泛型
Type[] args = OpenClass.getTypeArguments(MyClass.class);
Security | 安全性:
- Thread-safe: Yes (stateless with immutable caches) - 线程安全: 是 (无状态, 不可变缓存)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringclassNameToPath(String className) Converts a class name to a resource path by replacing '.' with '/'.getAllInterfaces(Class<?> clazz) Returns all interfaces implemented by the class, including inherited interfaces.getAllSuperTypes(Class<?> clazz) Returns all superclasses and interfaces of the given class.static Class<?> getArrayClass(Class<?> componentType) Returns the array type for the given component type.static ClassLoaderReturns the default ClassLoader for the current thread or this class.static ClassLoadergetClassLoader(Class<?> clazz) Returns the ClassLoader for the specified class.static URLgetCodeSourceLocation(Class<?> clazz) Returns the code source location URL for the given class.static Class<?> getCommonSuperClass(Class<?> class1, Class<?> class2) Returns the closest common superclass of two classes, or Object.class if none.static Class<?> getComponentType(Class<?> arrayClass) Returns the component type of the given array class.static <T> TgetDefaultValue(Class<T> clazz) Returns the default value for the given type (null for non-primitive types).static StringgetFullName(Class<?> clazz) Returns the fully-qualified name of the class.static Type[]getInterfaceTypeArguments(Class<?> clazz, Class<?> interfaceClass) Returns the actual type arguments for the specified interface on the given class.static StringgetJarPath(Class<?> clazz) Returns the JAR file path for the given class, or null if not in a JAR.static StringgetPackageName(Class<?> clazz) Returns the package name of the class.static StringgetPackageName(String className) Returns the package name extracted from the fully-qualified class name string.static Class<?> getPrimitiveClass(Class<?> wrapperType) Returns the primitive type corresponding to the given wrapper class.static ObjectgetPrimitiveDefaultValue(Class<?> primitiveType) Returns the default value for the given primitive type.static URLgetResource(String resourceName) Returns the URL of the resource with the given name from the classpath.static InputStreamgetResourceAsStream(String resourceName) Returns an InputStream for reading the named resource from the classpath.static StringgetShortName(Class<?> clazz) Returns the short name of the class (last segment of the fully-qualified name).static StringgetSimpleName(Class<?> clazz) Returns the simple name of the class.getSuperClasses(Class<?> clazz) Returns all superclasses of the given class, excluding Object.static Type[]getSuperclassTypeArguments(Class<?> clazz) Returns the actual type arguments of the generic superclass of the given class.static Type[]getTypeArguments(Class<?> clazz) Returns the type parameters declared on the given class.static Class<?> getWrapperClass(Class<?> primitiveType) Returns the wrapper class corresponding to the given primitive type.static booleanisAbstract(Class<?> clazz) Returns true if the class is abstract.static booleanisAnonymousClass(Class<?> clazz) Returns true if the class is an anonymous class.static booleanReturns true if the class represents an array type.static booleanisAssignable(Class<?> superType, Class<?> subType) Returns true if subType is assignable to superType (considering primitive/wrapper conversions).static booleanisCollection(Class<?> clazz) Returns true if the class is a Collection or Map type.static booleanReturns true if the class is an enum type.static booleanisInnerClass(Class<?> clazz) Returns true if the class is a non-static inner class.static booleanisInterface(Class<?> clazz) Returns true if the class is an interface.static booleanisLambdaClass(Class<?> clazz) Returns true if the class was generated by a lambda expression.static booleanisLocalClass(Class<?> clazz) Returns true if the class is a local class.static booleanReturns true if the class with the given name is present on the classpath.static booleanisPresent(String className, ClassLoader classLoader) Returns true if the class is present using the specified ClassLoader.static booleanisPrimitive(Class<?> clazz) Returns true if the class represents a primitive type.static booleanisPrimitiveOrWrapper(Class<?> clazz) Returns true if the class is a primitive type or its wrapper.static booleanisPrimitiveWrapper(Class<?> clazz) Returns true if the class is a primitive wrapper type.static booleanReturns true if the class is a record type.static booleanReturns true if the class is a sealed type.static Class<?> Loads and initializes the class with the given name.static Class<?> Loads the class with the given name, optionally initializing it.static Class<?> loadClass(String className, ClassLoader classLoader) Loads and initializes the class using the specified ClassLoader.loadClassOptional(String className) Safely loads the class, returning an Optional.static Class<?> loadClassSafely(String className) Safely loads the class, returning null if it does not exist.static <T> TnewInstance(Class<T> clazz) Creates a new instance of the given class using its no-arg constructor.static StringpathToClassName(String path) Converts a resource path to a class name by replacing '/' with '.'.static Class<?> resolveTypeArgument(Class<?> clazz, Class<?> genericClass) Resolves the first type argument for the given generic superclass or interface.
-
Method Details
-
getClassLoader
Returns the default ClassLoader for the current thread or this class. 获取默认 ClassLoader -
getClassLoader
Returns the ClassLoader for the specified class. 获取指定类的 ClassLoader -
loadClass
-
loadClass
-
loadClass
Loads and initializes the class using the specified ClassLoader. 加载类(指定 ClassLoader) -
loadClassSafely
-
loadClassOptional
-
isPresent
Returns true if the class with the given name is present on the classpath. 检查类是否存在 -
isPresent
Returns true if the class is present using the specified ClassLoader. 检查类是否存在(指定 ClassLoader) -
isPrimitive
Returns true if the class represents a primitive type. 检查是否为原始类型 -
isPrimitiveWrapper
Returns true if the class is a primitive wrapper type. 检查是否为原始类型包装类 -
isPrimitiveOrWrapper
Returns true if the class is a primitive type or its wrapper. 检查是否为原始类型或包装类 -
isArray
Returns true if the class represents an array type. 检查是否为数组类型 -
isCollection
Returns true if the class is a Collection or Map type. 检查是否为集合类型 -
isInterface
Returns true if the class is an interface. 检查是否为接口 -
isAbstract
Returns true if the class is abstract. 检查是否为抽象类 -
isEnum
Returns true if the class is an enum type. 检查是否为枚举类型 -
isRecord
Returns true if the class is a record type. 检查是否为 Record 类型 -
isSealed
Returns true if the class is a sealed type. 检查是否为 sealed 类型 -
isInnerClass
Returns true if the class is a non-static inner class. 检查是否为内部类 -
isAnonymousClass
Returns true if the class is an anonymous class. 检查是否为匿名类 -
isLocalClass
Returns true if the class is a local class. 检查是否为本地类 -
isLambdaClass
Returns true if the class was generated by a lambda expression. 检查是否为 Lambda 表达式生成的类 -
isAssignable
-
getWrapperClass
-
getPrimitiveClass
-
getComponentType
-
getArrayClass
-
getSimpleName
-
getShortName
-
getFullName
-
getPackageName
-
getPackageName
-
classNameToPath
-
pathToClassName
-
getSuperClasses
-
getAllInterfaces
-
getAllSuperTypes
-
getCommonSuperClass
Returns the closest common superclass of two classes, or Object.class if none. 获取公共父类- Parameters:
class1- first class | 第一个类class2- second class | 第二个类- Returns:
- closest common superclass, or Object.class if none | 最近公共父类,如果没有则返回 Object.class
-
getTypeArguments
-
getSuperclassTypeArguments
-
getInterfaceTypeArguments
Returns the actual type arguments for the specified interface on the given class. 获取接口的泛型参数- Parameters:
clazz- implementing class | 实现类interfaceClass- interface class | 接口类- Returns:
- array of interface type arguments, or empty array if none | 接口泛型参数数组,如果没有则返回空数组
-
resolveTypeArgument
Resolves the first type argument for the given generic superclass or interface. 解析泛型类型为实际类型- Parameters:
clazz- subclass | 子类genericClass- generic superclass or interface | 泛型父类或接口- Returns:
- actual type of the first type argument, or null if unresolvable | 第一个泛型参数的实际类型,如果无法解析则返回 null
-
getDefaultValue
Returns the default value for the given type (null for non-primitive types). 获取类型的默认值 -
getPrimitiveDefaultValue
-
getResource
-
getResourceAsStream
Returns an InputStream for reading the named resource from the classpath. 获取类路径下的资源流 -
getCodeSourceLocation
-
getJarPath
-
newInstance
Creates a new instance of the given class using its no-arg constructor. 创建类的新实例
-