获得包下所有类命

导读:本篇文章讲解 获得包下所有类命,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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;
		}
	}

Java通过编译文件获取项目包下的所有类 – 47号Gamer丶 – 博客园 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/107514.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!