共计 2466 个字符,预计需要花费 7 分钟才能阅读完成。
1. 本节须要实现的工作
- 实现对指定包下以及其子包下所有类的获取的工具类的编写
2. 我的项目配置
2.1 进行 pom 文件的相干配置
2.2 装置 lombok 插件
2.3 指定 maven-compiler-plugin 插件的相干配置
3. ClassUtil 类中相干办法的编写
3.1 须要实现的代码如下
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 | |
* 岂曰无衣,与子同袍~ | |
*/ | |
@Slf4j | |
public class ClassUtil { | |
public static final String FILE_PROTOCOL = "file"; | |
/** | |
* 依据传入的包名,获取该包以及其子包下所有的类 | |
* @param packageName | |
* @return | |
*/ | |
public static Set<Class<?>> extractPackageClass(String packageName) { | |
// 获取以后线程的类加载器 | |
ClassLoader classLoader = getClassLoader(); | |
// 通过类加载器获取到加载的资源 | |
URL url = classLoader.getResource(packageName.replace(".", "/")); // URL 格局 相似于 file:/ 文件在本机上的绝对路径 | |
if (url == null) {log.warn("unable to retrieve anything from package:" + packageName); | |
return null; | |
} | |
// 根据不同的资源,采纳不同的形式获取资源的汇合 | |
Set<Class<?>> classSet = null; | |
// 过滤出文件类型的资源 | |
if (url.getProtocol().equalsIgnoreCase(FILE_PROTOCOL)) {classSet = new HashSet<>(); | |
File packageDirectory = new File(url.getPath()); | |
extractClassFile(classSet, packageDirectory, packageName); | |
} | |
return classSet; | |
} | |
/** | |
* 获取以后线程的上下文类加载器 | |
* @return 以后的类加载器 | |
*/ | |
public static ClassLoader getClassLoader() {return Thread.currentThread().getContextClassLoader();} | |
private static void extractClassFile(Set<Class<?>> emptyClassSet, File fileSource, String packageName) {if (!fileSource.isDirectory()) { // 如果不是目录,间接返回 | |
return; | |
} | |
// 如果是文件夹,那么将该文件夹下的.class 文件放入 emptyClassSet 中 | |
File[] files = fileSource.listFiles(new FileFilter() { // listFile 只会返回该文件夹下的文件及文件夹,不会返回子文件夹下的内容 | |
@Override | |
public boolean accept(File file) {if (file.isDirectory()) {return true;} else { | |
// 如果不是文件夹,就把.class 文件放入类汇合中 | |
String absoluteFilePath = file.getAbsolutePath(); | |
if (absoluteFilePath.endsWith(".class")) {addToClassSet(absoluteFilePath); | |
} | |
} | |
return false; | |
} | |
private void addToClassSet(String absoluteFilePath) { | |
// 从绝对路径下获取类名 | |
absoluteFilePath = absoluteFilePath.replace(File.separator, "."); | |
String className = absoluteFilePath.substring(absoluteFilePath.indexOf(packageName)); | |
className = className.substring(0, className.lastIndexOf("."));// [) | |
// 通过反射获取 Class 对象 | |
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 须要实现的相干代码的解说如下:
3.2.1 extractPackageClass 办法解读:
3.2.2 getClassLoader 办法解读
3.2.3 extractClassFile 办法解读
3.2.4 loadClass 办法解读
github 地址:https://github.com/wuyiccc/he…
正文完