You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassPoolTail.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FilenameFilter;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.net.MalformedURLException;
  25. import java.net.URL;
  26. import java.util.jar.JarEntry;
  27. import java.util.jar.JarFile;
  28. final class ClassPathList {
  29. ClassPathList next;
  30. ClassPath path;
  31. ClassPathList(ClassPath p, ClassPathList n) {
  32. next = n;
  33. path = p;
  34. }
  35. }
  36. final class DirClassPath implements ClassPath {
  37. String directory;
  38. DirClassPath(String dirName) {
  39. directory = dirName;
  40. }
  41. public InputStream openClassfile(String classname) {
  42. try {
  43. char sep = File.separatorChar;
  44. String filename = directory + sep
  45. + classname.replace('.', sep) + ".class";
  46. return new FileInputStream(filename.toString());
  47. }
  48. catch (FileNotFoundException e) {}
  49. catch (SecurityException e) {}
  50. return null;
  51. }
  52. public URL find(String classname) {
  53. char sep = File.separatorChar;
  54. String filename = directory + sep
  55. + classname.replace('.', sep) + ".class";
  56. File f = new File(filename);
  57. if (f.exists())
  58. try {
  59. return f.getCanonicalFile().toURI().toURL();
  60. }
  61. catch (MalformedURLException e) {}
  62. catch (IOException e) {}
  63. return null;
  64. }
  65. public void close() {}
  66. public String toString() {
  67. return directory;
  68. }
  69. }
  70. final class JarDirClassPath implements ClassPath {
  71. JarClassPath[] jars;
  72. JarDirClassPath(String dirName) throws NotFoundException {
  73. File[] files = new File(dirName).listFiles(new FilenameFilter() {
  74. public boolean accept(File dir, String name) {
  75. name = name.toLowerCase();
  76. return name.endsWith(".jar") || name.endsWith(".zip");
  77. }
  78. });
  79. if (files != null) {
  80. jars = new JarClassPath[files.length];
  81. for (int i = 0; i < files.length; i++)
  82. jars[i] = new JarClassPath(files[i].getPath());
  83. }
  84. }
  85. public InputStream openClassfile(String classname) throws NotFoundException {
  86. if (jars != null)
  87. for (int i = 0; i < jars.length; i++) {
  88. InputStream is = jars[i].openClassfile(classname);
  89. if (is != null)
  90. return is;
  91. }
  92. return null; // not found
  93. }
  94. public URL find(String classname) {
  95. if (jars != null)
  96. for (int i = 0; i < jars.length; i++) {
  97. URL url = jars[i].find(classname);
  98. if (url != null)
  99. return url;
  100. }
  101. return null; // not found
  102. }
  103. public void close() {
  104. if (jars != null)
  105. for (int i = 0; i < jars.length; i++)
  106. jars[i].close();
  107. }
  108. }
  109. final class JarClassPath implements ClassPath {
  110. JarFile jarfile;
  111. String jarfileURL;
  112. JarClassPath(String pathname) throws NotFoundException {
  113. try {
  114. jarfile = new JarFile(pathname);
  115. jarfileURL = new File(pathname).getCanonicalFile()
  116. .toURI().toURL().toString();
  117. return;
  118. }
  119. catch (IOException e) {}
  120. throw new NotFoundException(pathname);
  121. }
  122. public InputStream openClassfile(String classname)
  123. throws NotFoundException
  124. {
  125. try {
  126. String jarname = classname.replace('.', '/') + ".class";
  127. JarEntry je = jarfile.getJarEntry(jarname);
  128. if (je != null)
  129. return jarfile.getInputStream(je);
  130. else
  131. return null; // not found
  132. }
  133. catch (IOException e) {}
  134. throw new NotFoundException("broken jar file?: "
  135. + jarfile.getName());
  136. }
  137. public URL find(String classname) {
  138. String jarname = classname.replace('.', '/') + ".class";
  139. JarEntry je = jarfile.getJarEntry(jarname);
  140. if (je != null)
  141. try {
  142. return new URL("jar:" + jarfileURL + "!/" + jarname);
  143. }
  144. catch (MalformedURLException e) {}
  145. return null; // not found
  146. }
  147. public void close() {
  148. try {
  149. jarfile.close();
  150. jarfile = null;
  151. }
  152. catch (IOException e) {}
  153. }
  154. public String toString() {
  155. return jarfile == null ? "<null>" : jarfile.toString();
  156. }
  157. }
  158. final class ClassPoolTail {
  159. protected ClassPathList pathList;
  160. public ClassPoolTail() {
  161. pathList = null;
  162. }
  163. public String toString() {
  164. StringBuffer buf = new StringBuffer();
  165. buf.append("[class path: ");
  166. ClassPathList list = pathList;
  167. while (list != null) {
  168. buf.append(list.path.toString());
  169. buf.append(File.pathSeparatorChar);
  170. list = list.next;
  171. }
  172. buf.append(']');
  173. return buf.toString();
  174. }
  175. public synchronized ClassPath insertClassPath(ClassPath cp) {
  176. pathList = new ClassPathList(cp, pathList);
  177. return cp;
  178. }
  179. public synchronized ClassPath appendClassPath(ClassPath cp) {
  180. ClassPathList tail = new ClassPathList(cp, null);
  181. ClassPathList list = pathList;
  182. if (list == null)
  183. pathList = tail;
  184. else {
  185. while (list.next != null)
  186. list = list.next;
  187. list.next = tail;
  188. }
  189. return cp;
  190. }
  191. public synchronized void removeClassPath(ClassPath cp) {
  192. ClassPathList list = pathList;
  193. if (list != null)
  194. if (list.path == cp)
  195. pathList = list.next;
  196. else {
  197. while (list.next != null)
  198. if (list.next.path == cp)
  199. list.next = list.next.next;
  200. else
  201. list = list.next;
  202. }
  203. cp.close();
  204. }
  205. public ClassPath appendSystemPath() {
  206. if (javassist.bytecode.ClassFile.MAJOR_VERSION < javassist.bytecode.ClassFile.JAVA_9) {
  207. return appendClassPath(new ClassClassPath());
  208. }
  209. else {
  210. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  211. return appendClassPath(new LoaderClassPath(cl, true));
  212. }
  213. }
  214. public ClassPath insertClassPath(String pathname)
  215. throws NotFoundException
  216. {
  217. return insertClassPath(makePathObject(pathname));
  218. }
  219. public ClassPath appendClassPath(String pathname)
  220. throws NotFoundException
  221. {
  222. return appendClassPath(makePathObject(pathname));
  223. }
  224. private static ClassPath makePathObject(String pathname)
  225. throws NotFoundException
  226. {
  227. String lower = pathname.toLowerCase();
  228. if (lower.endsWith(".jar") || lower.endsWith(".zip"))
  229. return new JarClassPath(pathname);
  230. int len = pathname.length();
  231. if (len > 2 && pathname.charAt(len - 1) == '*'
  232. && (pathname.charAt(len - 2) == '/'
  233. || pathname.charAt(len - 2) == File.separatorChar)) {
  234. String dir = pathname.substring(0, len - 2);
  235. return new JarDirClassPath(dir);
  236. }
  237. return new DirClassPath(pathname);
  238. }
  239. /**
  240. * This method does not close the output stream.
  241. */
  242. void writeClassfile(String classname, OutputStream out)
  243. throws NotFoundException, IOException, CannotCompileException
  244. {
  245. InputStream fin = openClassfile(classname);
  246. if (fin == null)
  247. throw new NotFoundException(classname);
  248. try {
  249. copyStream(fin, out);
  250. }
  251. finally {
  252. fin.close();
  253. }
  254. }
  255. /*
  256. -- faster version --
  257. void checkClassName(String classname) throws NotFoundException {
  258. if (find(classname) == null)
  259. throw new NotFoundException(classname);
  260. }
  261. -- slower version --
  262. void checkClassName(String classname) throws NotFoundException {
  263. InputStream fin = openClassfile(classname);
  264. try {
  265. fin.close();
  266. }
  267. catch (IOException e) {}
  268. }
  269. */
  270. /**
  271. * Opens the class file for the class specified by
  272. * <code>classname</code>.
  273. *
  274. * @param classname a fully-qualified class name
  275. * @return null if the file has not been found.
  276. * @throws NotFoundException if any error is reported by ClassPath.
  277. */
  278. InputStream openClassfile(String classname)
  279. throws NotFoundException
  280. {
  281. ClassPathList list = pathList;
  282. InputStream ins = null;
  283. NotFoundException error = null;
  284. while (list != null) {
  285. try {
  286. ins = list.path.openClassfile(classname);
  287. }
  288. catch (NotFoundException e) {
  289. if (error == null)
  290. error = e;
  291. }
  292. if (ins == null)
  293. list = list.next;
  294. else
  295. return ins;
  296. }
  297. if (error != null)
  298. throw error;
  299. else
  300. return null; // not found
  301. }
  302. /**
  303. * Searches the class path to obtain the URL of the class file
  304. * specified by classname. It is also used to determine whether
  305. * the class file exists.
  306. *
  307. * @param classname a fully-qualified class name.
  308. * @return null if the class file could not be found.
  309. */
  310. public URL find(String classname) {
  311. ClassPathList list = pathList;
  312. URL url = null;
  313. while (list != null) {
  314. url = list.path.find(classname);
  315. if (url == null)
  316. list = list.next;
  317. else
  318. return url;
  319. }
  320. return null;
  321. }
  322. /**
  323. * Reads from an input stream until it reaches the end.
  324. *
  325. * @return the contents of that input stream
  326. */
  327. public static byte[] readStream(InputStream fin) throws IOException {
  328. byte[][] bufs = new byte[8][];
  329. int bufsize = 4096;
  330. for (int i = 0; i < 8; ++i) {
  331. bufs[i] = new byte[bufsize];
  332. int size = 0;
  333. int len = 0;
  334. do {
  335. len = fin.read(bufs[i], size, bufsize - size);
  336. if (len >= 0)
  337. size += len;
  338. else {
  339. byte[] result = new byte[bufsize - 4096 + size];
  340. int s = 0;
  341. for (int j = 0; j < i; ++j) {
  342. System.arraycopy(bufs[j], 0, result, s, s + 4096);
  343. s = s + s + 4096;
  344. }
  345. System.arraycopy(bufs[i], 0, result, s, size);
  346. return result;
  347. }
  348. } while (size < bufsize);
  349. bufsize *= 2;
  350. }
  351. throw new IOException("too much data");
  352. }
  353. /**
  354. * Reads from an input stream and write to an output stream
  355. * until it reaches the end. This method does not close the
  356. * streams.
  357. */
  358. public static void copyStream(InputStream fin, OutputStream fout)
  359. throws IOException
  360. {
  361. int bufsize = 4096;
  362. byte[] buf = null;
  363. for (int i = 0; i < 64; ++i) {
  364. if (i < 8) {
  365. bufsize *= 2;
  366. buf = new byte[bufsize];
  367. }
  368. int size = 0;
  369. int len = 0;
  370. do {
  371. len = fin.read(buf, size, bufsize - size);
  372. if (len >= 0)
  373. size += len;
  374. else {
  375. fout.write(buf, 0, size);
  376. return;
  377. }
  378. } while (size < bufsize);
  379. fout.write(buf);
  380. }
  381. throw new IOException("too much data");
  382. }
  383. }