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

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