public List<String> getClassNamesByPackageName(String packageName) {
List<String> classNameList = new ArrayList<>();
try {
ClassLoader e = Thread.currentThread().getContextClassLoader();
String path = packageName.replace('.', '/');
Enumeration<URL> resources = e.getResources(path);
List<File> dirs = new ArrayList<>();
while (resources.hasMoreElements()) {
URL directory = resources.nextElement();
dirs.add(new File(directory.getFile()));
}
Iterator<File> arg6 = dirs.iterator();
while (arg6.hasNext()) {
File directory1 = arg6.next();
classNameList.addAll(findClasses(directory1, packageName));
}
} catch (Exception arg7) {
arg7.printStackTrace();
}
return classNameList;
}
private List<String> findClasses(File directory, String packageName) throws ClassNotFoundException {
List<String> className = new ArrayList();
if (!directory.exists()) {
return className;
} else {
File[] files = directory.listFiles();
File[] arg6 = files;
int arg5 = files.length;
for (int arg4 = 0; arg4 < arg5; ++arg4) {
File file = arg6[arg4];
if (file.isDirectory()) {
assert !file.getName().contains(".");
className.addAll(findClasses(file, packageName + '.' + file.getName()));
} else if (file.getName().endsWith(".class")) {
className.add(packageName + "." + file.getName().substring(0, file.getName().length() - 6));
}
}
return className;
}
}
获得包下所有类命
最新推荐文章于 2024-05-08 15:24:52 发布