1、分享一种获取指定包名下所有类旳工具类,代码如下 1. import java.io.File; 2. import java.io.FileFilter; 3. import java.lang.annotation.Annotation; 4. import .JarURLConnection; 5. import .URL; 6. import java.util.ArrayList; 7. import java.util.Enumeration; 8. import java.util.List; 9. import java.util.jar.JarEntry;
2、10. import java.util.jar.JarFile;
11.
12. public class ClassUtil {
13. // 获取指定包名下旳所有类
14. public static List
3、n
4、etProtocol(); 22. if (protocol.equals("file")) { 23. String packagePath = url.getPath(); 24. addClass(classList, packagePath, packageName, isRecursive); 25. } else if (protocol.equals("jar")) { 26.
5、 JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
27. JarFile jarFile = jarURLConnection.getJarFile();
28. Enumeration
6、hasMoreElements()) { 30. JarEntry jarEntry = jarEntries.nextElement(); 31. String jarEntryName = jarEntry.getName(); 32. if (jarEntryName.endsWith(".class")) { 33. String className
7、 = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", "."); 34. if (isRecursive || className.substring(0, className.lastIndexOf(".")).equals(packageName)) { 35. classList.add(Class.forName(className)); 36.
8、 } 37. } 38. } 39. } 40. } 41. } 42. } catch (Exception e) { 43. e.printStackTrace(); 44. } 45. return classList; 46. }
9、
47.
48. // 获取指定包名下旳所有类(可根据注解进行过滤)
49. public static List
10、ead.currentThread().getContextClassLoader().getResources(packageName.replaceAll("\\.", "/")); 53. while (urls.hasMoreElements()) { 54. URL url = urls.nextElement(); 55. if (url != null) { 56. String protocol = url.getProtocol(); 57
11、 if (protocol.equals("file")) { 58. String packagePath = url.getPath(); 59. addClassByAnnotation(classList, packagePath, packageName, annotationClass); 60. } else if (protocol.equals("jar")) { 61.
12、 JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
62. JarFile jarFile = jarURLConnection.getJarFile();
63. Enumeration
13、asMoreElements()) { 65. JarEntry jarEntry = jarEntries.nextElement(); 66. String jarEntryName = jarEntry.getName(); 67. if (jarEntryName.endsWith(".class")) { 68. String className
14、 jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", "."); 69. Class> cls = Class.forName(className); 70. if (cls.isAnnotationPresent(annotationClass)) { 71. classList.add(c
15、ls); 72. } 73. } 74. } 75. } 76. } 77. } 78. } catch (Exception e) { 79. e.printStackTrace(); 80. } 81. return classList;
16、
82. }
83.
84. private static void addClass(List
17、les) { 89. String fileName = file.getName(); 90. if (file.isFile()) { 91. String className = getClassName(packageName, fileName); 92. classList.add(Class.forName(className)); 93. } else {
18、 94. if (isRecursive) { 95. String subPackagePath = getSubPackagePath(packagePath, fileName); 96. String subPackageName = getSubPackageName(packageName, fileName); 97. addClass(classList,
19、subPackagePath, subPackageName, isRecursive); 98. } 99. } 100. } 101. } 102. } catch (Exception e) { 103. e.printStackTrace(); 104. } 105. } 106. 107. private static File[] getClassF
20、iles(String packagePath) { 108. return new File(packagePath).listFiles(new FileFilter() { 109. @Override 110. public boolean accept(File file) { 111. return (file.isFile() && file.getName().endsWith(".class")) || file.isDirectory(); 112.
21、 } 113. }); 114. } 115. 116. private static String getClassName(String packageName, String fileName) { 117. String className = fileName.substring(0, fileName.lastIndexOf(".")); 118. if (mons.lang.StringUtil.isNotEmpty(packageName)) { 119. classNam
22、e = packageName + "." + className; 120. } 121. return className; 122. } 123. 124. private static String getSubPackagePath(String packagePath, String filePath) { 125. String subPackagePath = filePath; 126. if (mons.lang.StringUtil.isNotEmpty(packagePath)
23、) { 127. subPackagePath = packagePath + "/" + subPackagePath; 128. } 129. return subPackagePath; 130. } 131. 132. private static String getSubPackageName(String packageName, String filePath) { 133. String subPackageName = filePath; 134. if
24、mons.lang.StringUtil.isNotEmpty(packageName)) {
135. subPackageName = packageName + "." + subPackageName;
136. }
137. return subPackageName;
138. }
139.
140. private static void addClassByAnnotation(List
25、geName, Class extends Annotation> annotationClass) { 141. try { 142. File[] files = getClassFiles(packagePath); 143. if (files != null) { 144. for (File file : files) { 145. String fileName = file.getName(); 146.
26、 if (file.isFile()) { 147. String className = getClassName(packageName, fileName); 148. Class> cls = Class.forName(className); 149. if (cls.isAnnotationPresent(annotationClass)) { 150. c
27、lassList.add(cls); 151. } 152. } else { 153. String subPackagePath = getSubPackagePath(packagePath, fileName); 154. String subPackageName = getSubPackageName(packageName, fileName); 155. addClassByAnnotation(classList, subPackagePath, subPackageName, annotationClass); 156. } 157. } 158. } 159. } catch (Exception e) { 160. e.printStackTrace(); 161. } 162. } 163. }






