1. Tasks to be completed in this section
- Complete the preparation of the tool class for obtaining all classes under the specified package and its sub package
2. Project configuration
2.1 configure the POM file
2.2 install Lombok plug-in
2.3 specify the configuration of Maven compiler plugin
3. The writing of related methods in classutil class
3.1 the code to be completed is as follows
package com.wuyiccc.helloframework.util;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
/**
* @author wuyiccc
* @date 2020/7/13 7:26
*Do you say that you have no clothes, and you are in the same robe with your son~
*/
@Slf4j
public class ClassUtil {
public static final String FILE_PROTOCOL = "file";
/**
*According to the package name, get all the classes under the package and its subpackages
* @param packageName
* @return
*/
public static Set<Class<?>> extractPackageClass(String packageName) {
//Gets the class loader of the current thread
ClassLoader classLoader = getClassLoader();
//Get the loaded resources through the class loader
URL url = classLoader.getResource ( packageName.replace (".," / "); // the URL format is similar to the absolute path of the file on the local machine
if (url == null) {
log.warn("unable to retrieve anything from package: " + packageName);
return null;
}
//According to different resources, different ways are used to obtain the collection of resources
Set<Class<?>> classSet = null;
//Filter out resources of file type
if (url.getProtocol().equalsIgnoreCase(FILE_PROTOCOL)) {
classSet = new HashSet<>();
File packageDirectory = new File(url.getPath());
extractClassFile(classSet, packageDirectory, packageName);
}
return classSet;
}
/**
*Gets the context class loader of the current thread
*@ return the current class loader
*/
public static ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
private static void extractClassFile(Set<Class<?>> emptyClassSet, File fileSource, String packageName) {
if (! fileSource.isDirectory ()) {// if it is not a directory, return it directly
return;
}
//If it is a folder, put the. Class file under the folder into emptyclassset
File[] files = fileSource.listFiles (New filefilter () {// LISTFILE will only return the files and folders in the folder, not the contents in the subfolder
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
//If it is not a folder, put the. Class file into the class collection
String absoluteFilePath = file.getAbsolutePath();
if (absoluteFilePath.endsWith(".class")) {
addToClassSet(absoluteFilePath);
}
}
return false;
}
private void addToClassSet(String absoluteFilePath) {
//Get class name from absolute path
absoluteFilePath = absoluteFilePath.replace(File.separator, ".");
String className = absoluteFilePath.substring(absoluteFilePath.indexOf(packageName));
className = className.substring(0, className.lastIndexOf("."));// [)
//Get class object by reflection
Class targetClass = loadClass(className);
emptyClassSet.add(targetClass);
}
});
if (files != null) {
for (File f : files) {
extractClassFile(emptyClassSet, f, packageName);
}
}
}
public static Class<?> loadClass(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
log.error("load class error");
throw new RuntimeException(e);
}
}
}
3.2 the explanation of relevant codes to be completed is as follows:
3.2.1 interpretation of extractpackageclass method:
3.2.2 interpretation of getclassloader method
3.2.3 interpretation of extractclassfile method
3.2.4 interpretation of loadclass method
GitHub address: https://github.com/wuyiccc/he…