掃描Java包class檔案,掃描javaclass

來源:互聯網
上載者:User

掃描Java包class檔案,掃描javaclass

  1 package javax.servlet.util;  2   3 import java.io.File;  4 import java.io.FileFilter;  5 import java.io.FileNotFoundException;  6 import java.io.IOException;  7 import java.net.URL;  8 import java.net.URLDecoder;  9 import java.util.Enumeration; 10 import java.util.Iterator; 11 import java.util.LinkedHashSet; 12 import java.util.Set; 13  14  15 public class PackageScanUtils 16 { 17      18         /** 19          * 擷取class集合 20          * @param packName         包名 21          * @param scanSubdirectory  掃描子目錄 22          * @return 23          */ 24     public static Set<Class<?>> getClasses(String packName,boolean scanSubdirectory) 25     { 26  27         Set<Class<?>> classes = new LinkedHashSet<Class<?>>(); // 裝載class集合 28         String packageName = packName; // 擷取包的名字 並進行替換 29         String packageUrlName = packageName.replace('.', '/'); 30         Enumeration<URL> urls; // 定義一個枚舉的集合 並進行迴圈來處理這個目錄下的class 31         try 32         { 33             urls = Thread.currentThread().getContextClassLoader().getResources(packageUrlName); 34  35             // 迴圈迭代下去 36             while (urls.hasMoreElements()) 37             { 38  39                 URL url = urls.nextElement(); // 擷取下一個元素 40  41                 String protocol = url.getProtocol(); // 得到協議的名稱,比如http、file 42  43                 if ("file".equals(protocol)) 44                 { // 如果是以檔案的形式儲存在伺服器上 45  46                     String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); // 擷取包的實體路徑 47                     // 以檔案的方式掃描整個包下的檔案 並添加到集合中 48                     findClassesInPackageByFile(packageName, filePath, scanSubdirectory, classes); 49                 } 50             } 51         } catch (IOException e) 52         { 53             e.printStackTrace(); 54          } 55  56         return classes; 57     } 58      59     /** 60      * 以檔案的方式掃描整個包下的檔案 並添加到集合中 61      * @param packageName   包名 62      * @param packagePath   包路徑 63      * @param scanSubdirectory     是否掃描子目錄 64      * @param classes        class集合 65      * @throws FileNotFoundException  66      */ 67     public static void findClassesInPackageByFile(String packageName, String packagePath, final boolean scanSubdirectory, Set<Class<?>> classes) throws FileNotFoundException 68     { 69  70         File dir = new File(packagePath); // 擷取此包的目錄 建立一個File 71         File[] dirfiles = dir.listFiles(new FileFilter() 72         { // 如果存在 就擷取包下的所有檔案 包括目錄  73  74             public boolean accept(File file) 75             { // 自訂過濾規則 如果可以迴圈(包含子目錄) 或則是以.class結尾的檔案(編譯好的java類檔案) 76                 return (scanSubdirectory && file.isDirectory()) || (file.getName().endsWith(".class")); 77             } 78         }); 79  80         for (File file : dirfiles) 81         { // 迴圈所有檔案 82              83             if (file.isDirectory()) 84             { // 如果是目錄 則繼續掃描 85                 findClassesInPackageByFile(packageName==""? file.getName():packageName + "." + file.getName(), file.getAbsolutePath(), scanSubdirectory, classes); 86             } else 87             { // 如果是java類檔案 去掉後面的.class 只留下類名 88  89                 String className = file.getName().substring(0, file.getName().length() - 6); 90                 try 91                 { 92                     if("".equals(packageName)){ 93                         classes.add(Thread.currentThread().getContextClassLoader().loadClass(className)); 94                     }else{ 95                         classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className)); 96                     } 97                 } catch (ClassNotFoundException e) 98                 { 99                     e.printStackTrace();100                 }101             }102         }103     }104     105     public static void main(String[] args)106     {107         Set<Class<?>> clas=PackageScanUtils.getClasses("cn.javax",true);108         Iterator<Class<?>>cla=clas.iterator();109         while(cla.hasNext()){110             /*Annotation[]ann=cla.next().getAnnotations();111             for(Annotation a:ann){112                 if(a instanceof WebServlet){113                     System.out.println(((WebServlet) a).value());114                 }115             }*/116             System.out.println(cla.next().getName());117         }118     }119 }
View Code

運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.