Java遍歷包中全部類方法註解

  1 import java.io.File;
  2 import java.io.FileFilter;
  3 import java.io.IOException;
  4 import java.lang.annotation.Annotation;
  5 import java.lang.reflect.Method;
  6 import java.net.JarURLConnection;
  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 import java.util.jar.JarEntry;
 14 import java.util.jar.JarFile;
 15 
 16 public class ClassUtil {
 17 
 18     public static void main(String[] args) {
 19         
 20         // 包下面的類
 21         Set<Class<?>> clazzs = getClasses("cn.package.test");
 22         if (clazzs == null) {
 23             return;
 24         }
 25 
 26         System.out.printf(clazzs.size() + "");
 27         // 某類或者接口的子類
 28         Set<Class<?>> inInterface = getByInterface(Object.class, clazzs);
 29         System.out.printf(inInterface.size() + "");
 30 
 31         for (Class<?> clazz : clazzs) {
 32 
 33             // 獲取類上的註解
 34             Annotation[] annos = clazz.getAnnotations();
 35             for (Annotation anno : annos) {
 36                 System.out.println(clazz.getSimpleName().concat(".").concat(anno.annotationType().getSimpleName()));
 37             }
 38 
 39             // 獲取方法上的註解
 40             Method[] methods = clazz.getDeclaredMethods();
 41             for (Method method : methods) {
 42                 Annotation[] annotations = method.getDeclaredAnnotations();
 43                 for (Annotation annotation : annotations) {
 44                     System.out.println(clazz.getSimpleName().concat(".").concat(method.getName()).concat(".")
 45                             .concat(annotation.annotationType().getSimpleName()));
 46                 }
 47             }
 48         }
 49 
 50     }
 51 
 52     /**
 53      * 從包package中獲取全部的Class
 54      *
 55      * @param pack
 56      * @return
 57      */
 58     public static Set<Class<?>> getClasses(String pack) {
 59 
 60         // 第一個class類的集合
 61         Set<Class<?>> classes = new LinkedHashSet<>();
 62         // 是否循環迭代
 63         boolean recursive = true;
 64         // 獲取包的名字 並進行替換
 65         String packageName = pack;
 66         String packageDirName = packageName.replace('.', '/');
 67         // 定義一個枚舉的集合 並進行循環來處理這個目錄下的things
 68         Enumeration<URL> dirs;
 69         try {
 70             dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
 71             // 循環迭代下去
 72             while (dirs.hasMoreElements()) {
 73                 // 獲取下一個元素
 74                 URL url = dirs.nextElement();
 75                 // 獲得協議的名稱
 76                 String protocol = url.getProtocol();
 77                 // 若是是以文件的形式保存在服務器上
 78                 if ("file".equals(protocol)) {
 79                     System.err.println("file類型的掃描");
 80                     // 獲取包的物理路徑
 81                     String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
 82                     // 以文件的方式掃描整個包下的文件 並添加到集合中
 83                     findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes);
 84                 } else if ("jar".equals(protocol)) {
 85                     // 若是是jar包文件
 86                     // 定義一個JarFile
 87                     // System.err.println("jar類型的掃描");
 88                     JarFile jar;
 89                     try {
 90                         // 獲取jar
 91                         jar = ((JarURLConnection) url.openConnection()).getJarFile();
 92                         // 今後jar包 獲得一個枚舉類
 93                         Enumeration<JarEntry> entries = jar.entries();
 94                         // 一樣的進行循環迭代
 95                         while (entries.hasMoreElements()) {
 96                             // 獲取jar裏的一個實體 能夠是目錄 和一些jar包裏的其餘文件 如META-INF等文件
 97                             JarEntry entry = entries.nextElement();
 98                             String name = entry.getName();
 99                             // 若是是以/開頭的
100                             if (name.charAt(0) == '/') {
101                                 // 獲取後面的字符串
102                                 name = name.substring(1);
103                             }
104                             // 若是前半部分和定義的包名相同
105                             if (name.startsWith(packageDirName)) {
106                                 int idx = name.lastIndexOf('/');
107                                 // 若是以"/"結尾 是一個包
108                                 if (idx != -1) {
109                                     // 獲取包名 把"/"替換成"."
110                                     packageName = name.substring(0, idx).replace('/', '.');
111                                 }
112                                 // 若是能夠迭代下去 而且是一個包
113                                 if ((idx != -1) || recursive) {
114                                     // 若是是一個.class文件 並且不是目錄
115                                     if (name.endsWith(".class") && !entry.isDirectory()) {
116                                         // 去掉後面的".class" 獲取真正的類名
117                                         String className = name.substring(packageName.length() + 1, name.length() - 6);
118                                         try {
119                                             // 添加到classes
120                                             classes.add(Class.forName(packageName + '.' + className));
121                                         } catch (ClassNotFoundException e) {
122                                             e.printStackTrace();
123                                         }
124                                     }
125                                 }
126                             }
127                         }
128                     } catch (IOException e) {
129                         // log.error("在掃描用戶定義視圖時從jar包獲取文件出錯");
130                         e.printStackTrace();
131                     }
132                 }
133             }
134         } catch (IOException e) {
135             e.printStackTrace();
136         }
137 
138         return classes;
139     }
140 
141     /**
142      * 以文件的形式來獲取包下的全部Class
143      *
144      * @param packageName
145      * @param packagePath
146      * @param recursive
147      * @param classes
148      */
149     public static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive,
150             Set<Class<?>> classes) {
151         // 獲取此包的目錄 創建一個File
152         File dir = new File(packagePath);
153         // 若是不存在或者 也不是目錄就直接返回
154         if (!dir.exists() || !dir.isDirectory()) {
155             // log.warn("用戶定義包名 " + packageName + " 下沒有任何文件");
156             return;
157         }
158         // 若是存在 就獲取包下的全部文件 包括目錄
159         File[] dirfiles = dir.listFiles(new FileFilter() {
160             // 自定義過濾規則 若是能夠循環(包含子目錄) 或則是以.class結尾的文件(編譯好的java類文件)
161             public boolean accept(File file) {
162                 return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
163             }
164         });
165         // 循環全部文件
166         for (File file : dirfiles) {
167             // 若是是目錄 則繼續掃描
168             if (file.isDirectory()) {
169                 findAndAddClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive,
170                         classes);
171             } else {
172                 // 若是是java類文件 去掉後面的.class 只留下類名
173                 String className = file.getName().substring(0, file.getName().length() - 6);
174                 try {
175                     // 添加到集合中去
176                     // classes.add(Class.forName(packageName + '.' + className));
177                     // 通過回覆同窗的提醒,這裏用forName有一些很差,會觸發static方法,沒有使用classLoader的load乾淨
178                     classes.add(
179                             Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className));
180                 } catch (ClassNotFoundException e) {
181                     // log.error("添加用戶自定義視圖類錯誤 找不到此類的.class文件");
182                     e.printStackTrace();
183                 }
184             }
185         }
186     }
187 
188     // --------------------------------------------------------------------------------------------------------
189 
190     @SuppressWarnings({ "rawtypes", "unchecked" })
191     public static Set<Class<?>> getByInterface(Class clazz, Set<Class<?>> classesAll) {
192         Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
193         // 獲取指定接口的實現類
194         if (!clazz.isInterface()) {
195             try {
196                 /**
197                  * 循環判斷路徑下的全部類是否繼承了指定類 而且排除父類本身
198                  */
199                 Iterator<Class<?>> iterator = classesAll.iterator();
200                 while (iterator.hasNext()) {
201                     Class<?> cls = iterator.next();
202                     /**
203                      * isAssignableFrom該方法的解析,請參考博客:
204                      * http://blog.csdn.net/u010156024/article/details/44875195
205                      */
206                     if (clazz.isAssignableFrom(cls)) {
207                         if (!clazz.equals(cls)) {// 自身並不加進去
208                             classes.add(cls);
209                         } else {
210 
211                         }
212                     }
213                 }
214             } catch (Exception e) {
215                 System.out.println("出現異常");
216             }
217         }
218         return classes;
219     }
220 
221 }
相關文章
相關標籤/搜索