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 13KB

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