资源描述
分享一种获取指定包名下所有类旳工具类,代码如下
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;
10. import java.util.jar.JarFile;
11.
12. public class ClassUtil {
13. // 获取指定包名下旳所有类
14. public static List<Class<?>> getClassList(String packageName, boolean isRecursive) {
15. List<Class<?>> classList = new ArrayList<Class<?>>();
16. try {
17. Enumeration<URL> urls = Thread.currentThread().getContextClassLoader().getResources(packageName.replaceAll("\\.", "/"));
18. while (urls.hasMoreElements()) {
19. URL url = urls.nextElement();
20. if (url != null) {
21. String protocol = url.getProtocol();
22. if (protocol.equals("file")) {
23. String packagePath = url.getPath();
24. addClass(classList, packagePath, packageName, isRecursive);
25. } else if (protocol.equals("jar")) {
26. JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
27. JarFile jarFile = jarURLConnection.getJarFile();
28. Enumeration<JarEntry> jarEntries = jarFile.entries();
29. while (jarEntries.hasMoreElements()) {
30. JarEntry jarEntry = jarEntries.nextElement();
31. String jarEntryName = jarEntry.getName();
32. if (jarEntryName.endsWith(".class")) {
33. String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");
34. if (isRecursive || className.substring(0, className.lastIndexOf(".")).equals(packageName)) {
35. classList.add(Class.forName(className));
36. }
37. }
38. }
39. }
40. }
41. }
42. } catch (Exception e) {
43. e.printStackTrace();
44. }
45. return classList;
46. }
47.
48. // 获取指定包名下旳所有类(可根据注解进行过滤)
49. public static List<Class<?>> getClassListByAnnotation(String packageName, Class<? extends Annotation> annotationClass) {
50. List<Class<?>> classList = new ArrayList<Class<?>>();
51. try {
52. Enumeration<URL> urls = Thread.currentThread().getContextClassLoader().getResources(packageName.replaceAll("\\.", "/"));
53. while (urls.hasMoreElements()) {
54. URL url = urls.nextElement();
55. if (url != null) {
56. String protocol = url.getProtocol();
57. if (protocol.equals("file")) {
58. String packagePath = url.getPath();
59. addClassByAnnotation(classList, packagePath, packageName, annotationClass);
60. } else if (protocol.equals("jar")) {
61. JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
62. JarFile jarFile = jarURLConnection.getJarFile();
63. Enumeration<JarEntry> jarEntries = jarFile.entries();
64. while (jarEntries.hasMoreElements()) {
65. JarEntry jarEntry = jarEntries.nextElement();
66. String jarEntryName = jarEntry.getName();
67. if (jarEntryName.endsWith(".class")) {
68. String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");
69. Class<?> cls = Class.forName(className);
70. if (cls.isAnnotationPresent(annotationClass)) {
71. classList.add(cls);
72. }
73. }
74. }
75. }
76. }
77. }
78. } catch (Exception e) {
79. e.printStackTrace();
80. }
81. return classList;
82. }
83.
84. private static void addClass(List<Class<?>> classList, String packagePath, String packageName, boolean isRecursive) {
85. try {
86. File[] files = getClassFiles(packagePath);
87. if (files != null) {
88. for (File file : files) {
89. String fileName = file.getName();
90. if (file.isFile()) {
91. String className = getClassName(packageName, fileName);
92. classList.add(Class.forName(className));
93. } else {
94. if (isRecursive) {
95. String subPackagePath = getSubPackagePath(packagePath, fileName);
96. String subPackageName = getSubPackageName(packageName, fileName);
97. addClass(classList, subPackagePath, subPackageName, isRecursive);
98. }
99. }
100. }
101. }
102. } catch (Exception e) {
103. e.printStackTrace();
104. }
105. }
106.
107. private static File[] getClassFiles(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. }
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. className = 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)) {
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 (mons.lang.StringUtil.isNotEmpty(packageName)) {
135. subPackageName = packageName + "." + subPackageName;
136. }
137. return subPackageName;
138. }
139.
140. private static void addClassByAnnotation(List<Class<?>> classList, String packagePath, String packageName, 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. if (file.isFile()) {
147. String className = getClassName(packageName, fileName);
148. Class<?> cls = Class.forName(className);
149. if (cls.isAnnotationPresent(annotationClass)) {
150. classList.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. }
展开阅读全文