1
2
3
4
5 package gboat2.base.bridge.util;
6
7 import gboat2.base.bridge.exception.DefaultGboatNestedException;
8
9 import java.io.File;
10 import java.io.FileInputStream;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.net.URL;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.zip.ZipEntry;
17 import java.util.zip.ZipInputStream;
18
19 import org.apache.commons.io.IOUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23
24
25
26
27
28
29
30 public abstract class PackageClassUtil {
31
32 private static final Logger logger = LoggerFactory.getLogger(PackageClassUtil.class);
33
34
35
36
37
38
39 public static List<Class<?>> getClasses(String pkgname) {
40 return getClasses(pkgname, null);
41 }
42
43
44
45
46
47
48
49
50 public static List<Class<?>> getClasses(String pkgname, ClassLoader classLoader) {
51 ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
52
53 ClassLoader cld = Thread.currentThread().getContextClassLoader();
54 if (cld == null)
55 throw new DefaultGboatNestedException("Can't get class loader.");
56
57 String path = pkgname.replace('.', '/');
58 URL resource = cld.getResource(path);
59 if (resource == null)
60 throw new DefaultGboatNestedException("No resource for " + path);
61
62 File directory = new File(resource.getFile());
63 String dirPath = directory.getAbsolutePath();
64 logger.debug("包 [] 对应的物理路径为:{}", pkgname, dirPath);
65 if (directory.exists()) {
66
67 String[] filenames = directory.list();
68 String filename = null;
69 try {
70 for (int i = 0; i < filenames.length; i++) {
71 filename = filenames[i];
72
73 if (filename.endsWith(".class")) {
74
75 classes.add(Class.forName(pkgname + '.' + filename.substring(0, filename.length() - 6)));
76 }
77 }
78 }catch (ClassNotFoundException e) {
79 throw new DefaultGboatNestedException(filename + " does not exist in the package [" + pkgname + "]");
80 }
81 } else if (dirPath.indexOf(".jar!") != -1) {
82 String zipFileName = dirPath.substring(dirPath.indexOf("file") + 6, dirPath.indexOf("!"));
83 String prefixPath = dirPath.substring(dirPath.indexOf("!") + 2).replaceAll("\\\\", "/");
84 ZipInputStream in = null;
85 String name = null;
86 try {
87 in = new ZipInputStream(new FileInputStream(zipFileName));
88 ZipEntry entry = null;
89 while ((entry = in.getNextEntry()) != null) {
90 if (!entry.isDirectory()) {
91 name = entry.getName();
92 if (name.startsWith(prefixPath) && name.endsWith(".class")) {
93 classes.add(Class.forName(name.substring(0, name.indexOf(".class")).replaceAll("/", ".")));
94 }
95 }
96 }
97
98 } catch (FileNotFoundException e) {
99 throw new DefaultGboatNestedException(pkgname + " --> the jar file [" + zipFileName + "] can't find");
100 }catch (IOException e) {
101 throw new DefaultGboatNestedException(pkgname + " --> the jar file [" + zipFileName + "] can't read");
102 } catch (ClassNotFoundException e) {
103 throw new DefaultGboatNestedException(name + " does not exist in the jar file [" + zipFileName + "]");
104 } finally {
105 IOUtils.closeQuietly(in);
106 }
107 } else {
108 throw new DefaultGboatNestedException(pkgname + " does not appear to be a valid package");
109 }
110 return classes;
111 }
112
113
114
115
116
117 public static String getRealPath(Class<?> cls) {
118
119 if (cls == null)
120 throw new java.lang.IllegalArgumentException("参数不能为空!");
121
122 ClassLoader loader = cls.getClassLoader();
123
124 String clsName = cls.getName() + ".class";
125
126 Package pack = cls.getPackage();
127 String path = "";
128 StringBuilder pathSB = new StringBuilder();
129
130 if (pack != null) {
131 String packName = pack.getName();
132
133 if (packName.startsWith("java.") || packName.startsWith("javax."))
134 throw new java.lang.IllegalArgumentException("不要传送系统类!");
135
136
137 clsName = clsName.substring(packName.length() + 1);
138
139 if (packName.indexOf(".") < 0)
140
141 pathSB.append(packName + "/");
142 else {
143 int start = 0, end = 0;
144 end = packName.indexOf(".");
145 while (end != -1) {
146
147 pathSB.append(packName.substring(start, end) + "/");
148 start = end + 1;
149 end = packName.indexOf(".", start);
150 }
151
152 pathSB.append(packName.substring(start) + "/");
153 }
154 }
155 path = pathSB.toString();
156
157 java.net.URL url = loader.getResource(path + clsName);
158
159 String realPath = url.getPath();
160
161 int pos = realPath.indexOf("file:");
162 if (pos > -1)
163 realPath = realPath.substring(pos + 5);
164
165 pos = realPath.indexOf(path + clsName);
166 realPath = realPath.substring(0, pos - 1);
167
168 if (realPath.endsWith("!"))
169 realPath = realPath.substring(0, realPath.lastIndexOf("/"));
170
171
172
173
174
175
176 try {
177 realPath = java.net.URLDecoder.decode(realPath, "utf-8");
178 } catch (Exception e) {
179 throw new RuntimeException(e);
180 }
181 return realPath;
182 }
183 }