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:

  1. Get the ContextClassLoader of the current thread
  2. Get the ClassLoader corresponding to the ClassLoaderUtil class
  3. 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:

  1. Primitive types, for example: int
  2. Array types, for example: int[], Long[], String[]
  3. 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.