ClassLoaderUtil
Introduction
Provides utility classes related to ClassLoader, such as class loading (Class.forName wrapper), etc.
Methods
Getting ClassLoader
getContextClassLoader
Gets the ClassLoader of the current thread, essentially calling Thread.currentThread().getContextClassLoader()
getClassLoader
Gets the ClassLoader according to the following order of rules:
- Get the ContextClassLoader of the current thread
- Get the ClassLoader corresponding to the ClassLoaderUtil class
- Get the system ClassLoader (ClassLoader.getSystemClassLoader())
Loading Class
loadClass
Loads a class by passing in the string of the class name and returns its corresponding class. It uses the default ClassLoader and initializes the class (calls static module content and optional initialization of static attributes).
Extends the Class.forName
method to support loading of the following types of class names:
- Primitive types, for example: int
- Array types, for example: int[], Long[], String[]
- Inner classes, for example: java.lang.Thread.State will be converted to java.lang.Thread$State for loading
It also provides the loadPrimitiveClass
method for quickly loading primitive type classes, including primitive types, primitive type arrays, and void.
isPresent
Checks if a specified class is available by attempting to load the class with the specified class name through the loadClass
method. If loading fails, it returns false.
The reason for loading failure may be that the class does not exist or its associated referenced classes do not exist.