java 遍歷指定包下所有類,返回完整類名。工具類,可以直接拷入使用
阿新 • • 發佈:2018-12-14
1、說明:
此類為本人開發的工具類,具體應用在什麼地方呢。本人在實際專案中,許可權管理這一塊有所應用,應該是許可權這一塊有所需求而開發的。
應用場景說明:許可權資源自動化生產時,使用者點選介面的一鍵生成資源時,介面中就會遍歷指定controller包下所有類,返回所有類名。在通過反射獲取每一個url地址,與地址上的解釋,生產一一對應的資源。
例如: 所用主要框架: spring boot、swagger2
程式碼段:
@ApiOperation("根據id刪除使用者") @DeleteMapping(value = "/byId/{id}") public RetEntity<Users> deleteUser(@PathVariable("id") String id) throws CommonException { RetEntity<Users> retEntity = usersService.deleteUser(id); return retEntity; }
生成的資源格式:“根據id刪除使用者”,“/users/byId/{id}”
2、類 ClassUtils.java
import java.io.File; import java.io.IOException; import java.net.JarURLConnection; import java.net.URL; import java.net.URLClassLoader; import java.util.Enumeration; import java.util.HashSet; import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class ClassUtils { // public static void main(String[] args) { // String packageName = ""; //填入完整包名,如com.org.String // Set<String> classNames = getClassName(packageName, false); // if (classNames != null) { // for (String className : classNames) { // System.out.println(className); // } // } // } /** * 獲取某包下所有類 * * @param packageName 包名 * @param isRecursion 是否遍歷子包 * @return 類的完整名稱 */ public static Set<String> getClassName(String packageName, boolean isRecursion) { Set<String> classNames = null; ClassLoader loader = Thread.currentThread().getContextClassLoader(); String packagePath = packageName.replace(".", "/"); URL url = loader.getResource(packagePath); if (url != null) { String protocol = url.getProtocol(); if (protocol.equals("file")) { classNames = getClassNameFromDir(url.getPath(), packageName, isRecursion); } else if (protocol.equals("jar")) { JarFile jarFile = null; try { jarFile = ((JarURLConnection) url.openConnection()).getJarFile(); } catch (Exception e) { e.printStackTrace(); } if (jarFile != null) { getClassNameFromJar(jarFile.entries(), packageName, isRecursion); } } } else { /*從所有的jar包中查詢包名*/ classNames = getClassNameFromJars(((URLClassLoader) loader).getURLs(), packageName, isRecursion); } return classNames; } /** * 從專案檔案獲取某包下�?有類 * * @param filePath 檔案路徑 * @param className 類名集合 * @param isRecursion 是否遍歷子包 * @return 類的完整名稱 */ private static Set<String> getClassNameFromDir(String filePath, String packageName, boolean isRecursion) { Set<String> className = new HashSet<String>(); File file = new File(filePath); File[] files = file.listFiles(); for (File childFile : files) { //�?查一個物件是否是檔案�? if (childFile.isDirectory()) { if (isRecursion) { className.addAll(getClassNameFromDir(childFile.getPath(), packageName + "." + childFile.getName(), isRecursion)); } } else { String fileName = childFile.getName(); //endsWith() 方法用於測試字串是否以指定的後�?結束�? !fileName.contains("$") 檔名中不包�? '$' if (fileName.endsWith(".class") && !fileName.contains("$")) { className.add(packageName + "." + fileName.replace(".class", "")); } } } return className; } /** * @param jarEntries * @param packageName * @param isRecursion * @return */ private static Set<String> getClassNameFromJar(Enumeration<JarEntry> jarEntries, String packageName, boolean isRecursion) { Set<String> classNames = new HashSet<String>(); while (jarEntries.hasMoreElements()) { JarEntry jarEntry = jarEntries.nextElement(); if (!jarEntry.isDirectory()) { /* * 這裡是為了方便,先把"/" 轉成 "." 再判�? ".class" 的做法可能會有bug * (FIXME: 先把"/" 轉成 "." 再判�? ".class" 的做法可能會有bug) */ String entryName = jarEntry.getName().replace("/", "."); if (entryName.endsWith(".class") && !entryName.contains("$") && entryName.startsWith(packageName)) { entryName = entryName.replace(".class", ""); if (isRecursion) { classNames.add(entryName); } else if (!entryName.replace(packageName + ".", "").contains(".")) { classNames.add(entryName); } } } } return classNames; } /** * 從所有jar中搜索該包,並獲取該包下�?有類 * * @param urls URL集合 * @param packageName 包路�? * @param isRecursion 是否遍歷子包 * @return 類的完整名稱 */ private static Set<String> getClassNameFromJars(URL[] urls, String packageName, boolean isRecursion) { Set<String> classNames = new HashSet<String>(); for (int i = 0; i < urls.length; i++) { String classPath = urls[i].getPath(); //不必搜尋classes檔案�? if (classPath.endsWith("classes/")) { continue; } JarFile jarFile = null; try { jarFile = new JarFile(classPath.substring(classPath.indexOf("/"))); } catch (IOException e) { e.printStackTrace(); } if (jarFile != null) { classNames.addAll(getClassNameFromJar(jarFile.entries(), packageName, isRecursion)); } } return classNames; } }
結語:本人所有文章都立志寫的簡單易懂,戳中問題點。 當然了,簡單的同時可能忽略了很多細節與詳細,如有不足的地方,還請諒解並指出。 如對文章或實現技術上有問題,可聯絡我:qq: 1226500260 郵箱:[email protected]