ClassUtil

Class Processing Tool ClassUtil

This tool mainly encapsulates some reflection methods to make calling easier. The most useful method in this class is the scanPackage method, which scans all classes under the classpath. This is a feature in Spring and is the basis for class scanning in the Hulu framework. Below is an introduction to the methods in this class.

getShortClassName

Gets the short format of the full class name, for example, cn.hutool.core.util.StrUtil -> c.h.c.u.StrUtil

isAllAssignableFrom

Compares and judges two groups of classes, if all classes in types1 are the same as the corresponding classes in types2, or their superclasses or interfaces, then returns true.

isPrimitiveWrapper

Whether it is a wrapper type

isBasicType

Whether it is a basic type (including wrapper classes and primitive types)

getPackage

Gets the package name where the given class is located, for example, cn.hutool.util.ClassUtil -> cn.hutool.util

scanPackage Method

The only parameter of this method is the package name, and the return result is all classes under this package and its sub-packages. The method usage is simple, but the process is somewhat complicated. Package scanning first calls the getClassPaths method to get the ClassPath, then scans the ClassPath. If it is a directory, it scans the class files under the directory or jar files. If it is a jar package, it directly obtains the class name from the jar package. The purpose of this method is obvious, to find all classes. In Spring, it is used for dependency injection, while in Hulu, it is used to find Action classes. Of course, you can also pass in a ClassFilter object to filter out unwanted classes.

getClassPaths Method

This method gets the current thread’s ClassPath, with the core being the call to Thread.currentThread().getContextClassLoader().getResources.

getJavaClassPaths Method

This method is used to get the ClassPath defined by the java system variables.

getClassLoader and getContextClassLoader Methods

The latter only gets the current thread’s ClassLoader, while the former gets the ClassLoader of the ClassUtil class if获取 fails.

getDefaultValue

Gets the default value of the specified type, with the default rule being: if it is a primitive type, return 0; if not a primitive type, return null.

Others

For more detailed method descriptions, please refer to:

https://apidoc.gitee.com/loolly/hutool/cn/hutool/core/util/ClassUtil.html