2015年1月14日 星期三

[ Java 代碼範本 ] Get All Classes Within A Package

Source From Here 
Preface 
The code below gets all classes within a given package. Notice that it should only work for classes found locally, getting really ALL classes is impossible. 

Sample Code 
Consider the project has below package hierarchy: 
 

Try below sample code: 
- test.Test.java 
  1. package test;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.net.URL;  
  6. import java.util.ArrayList;  
  7. import java.util.Enumeration;  
  8. import java.util.List;  
  9.   
  10. public class Test {  
  11.     public static class A{  
  12.         String name;  
  13.     }  
  14.       
  15.     /** 
  16.      * Scans all classes accessible from the context class loader which belong to the given package and subpackages. 
  17.      * 
  18.      * @param packageName The base package 
  19.      * @return The classes 
  20.      * @throws ClassNotFoundException 
  21.      * @throws IOException 
  22.      */  
  23.     private static List GetClasses(String packageName)  
  24.             throws ClassNotFoundException, IOException {  
  25.         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();  
  26.         assert classLoader != null;  
  27.         String path = packageName.replace('.''/');  
  28.         Enumeration resources = classLoader.getResources(path);  
  29.         List dirs = new ArrayList();  
  30.         while (resources.hasMoreElements()) {  
  31.             URL resource = resources.nextElement();  
  32.             dirs.add(new File(resource.getFile()));  
  33.         }  
  34.         ArrayList classes = new ArrayList();  
  35.         for (File directory : dirs) {  
  36.             classes.addAll(FindClasses(directory, packageName));  
  37.         }  
  38.         return classes;  
  39.     }  
  40.   
  41.     /** 
  42.      * Recursive method used to find all classes in a given directory and subdirs. 
  43.      * 
  44.      * @param directory   The base directory 
  45.      * @param packageName The package name for classes found inside the base directory 
  46.      * @return The classes 
  47.      * @throws ClassNotFoundException 
  48.      */  
  49.     private static List FindClasses(File directory, String packageName) throws ClassNotFoundException {  
  50.         List classes = new ArrayList();  
  51.         if (!directory.exists()) {  
  52.             return classes;  
  53.         }  
  54.         File[] files = directory.listFiles();  
  55.         for (File file : files) {  
  56.             if (file.isDirectory()) {  
  57.                 assert !file.getName().contains(".");  
  58.                 classes.addAll(FindClasses(file, packageName + "." + file.getName()));  
  59.             } else if (file.getName().endsWith(".class")) {  
  60.                 classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));  
  61.             }  
  62.         }  
  63.         return classes;  
  64.     }  
  65.       
  66.     public static void main(String args[]) throws Exception  
  67.     {  
  68.         String pkg = "test";  
  69.         System.out.printf("\t[Info] Search package: %s\n", pkg);          
  70.         for(Class clz:GetClasses(pkg))  
  71.         {  
  72.             System.out.printf("\t%s\n", clz.getName());  
  73.         }  
  74.     }  
  75. }  
Execution result: 
[Info] Search package: test
test.abc.C
test.B
test.Test$A
test.Test

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...