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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2007 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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist;
  16. import java.io.*;
  17. import java.util.jar.*;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20. import java.util.Hashtable;
  21. final class ClassPathList {
  22. ClassPathList next;
  23. ClassPath path;
  24. ClassPathList(ClassPath p, ClassPathList n) {
  25. next = n;
  26. path = p;
  27. }
  28. }
  29. final class DirClassPath implements ClassPath {
  30. String directory;
  31. DirClassPath(String dirName) {
  32. directory = dirName;
  33. }
  34. public InputStream openClassfile(String classname) {
  35. try {
  36. char sep = File.separatorChar;
  37. String filename = directory + sep
  38. + classname.replace('.', sep) + ".class";
  39. return new FileInputStream(filename.toString());
  40. }
  41. catch (FileNotFoundException e) {}
  42. catch (SecurityException e) {}
  43. return null;
  44. }
  45. public URL find(String classname) {
  46. char sep = File.separatorChar;
  47. String filename = directory + sep
  48. + classname.replace('.', sep) + ".class";
  49. File f = new File(filename);
  50. if (f.exists())
  51. try {
  52. return f.getCanonicalFile().toURI().toURL();
  53. }
  54. catch (MalformedURLException e) {}
  55. catch (IOException e) {}
  56. return null;
  57. }
  58. public void close() {}
  59. public String toString() {
  60. return directory;
  61. }
  62. }
  63. final class JarDirClassPath implements ClassPath {
  64. JarClassPath[] jars;
  65. JarDirClassPath(String dirName) throws NotFoundException {
  66. File[] files = new File(dirName).listFiles(new FilenameFilter() {
  67. public boolean accept(File dir, String name) {
  68. name = name.toLowerCase();
  69. return name.endsWith(".jar") || name.endsWith(".zip");
  70. }
  71. });
  72. if (files != null) {
  73. jars = new JarClassPath[files.length];
  74. for (int i = 0; i < files.length; i++)
  75. jars[i] = new JarClassPath(files[i].getPath());
  76. }
  77. }
  78. public InputStream openClassfile(String classname) throws NotFoundException {
  79. if (jars != null)
  80. for (int i = 0; i < jars.length; i++) {
  81. InputStream is = jars[i].openClassfile(classname);
  82. if (is != null)
  83. return is;
  84. }
  85. return null; // not found
  86. }
  87. public URL find(String classname) {
  88. if (jars != null)
  89. for (int i = 0; i < jars.length; i++) {
  90. URL url = jars[i].find(classname);
  91. if (url != null)
  92. return url;
  93. }
  94. return null; // not found
  95. }
  96. public void close() {
  97. if (jars != null)
  98. for (int i = 0; i < jars.length; i++)
  99. jars[i].close();
  100. }
  101. }
  102. final class JarClassPath implements ClassPath {
  103. JarFile jarfile;
  104. String jarfileURL;
  105. JarClassPath(String pathname) throws NotFoundException {
  106. try {
  107. jarfile = new JarFile(pathname);
  108. jarfileURL = new File(pathname).getCanonicalFile()
  109. .toURI().toURL().toString();
  110. return;
  111. }
  112. catch (IOException e) {}
  113. throw new NotFoundException(pathname);
  114. }
  115. public InputStream openClassfile(String classname)
  116. throws NotFoundException
  117. {
  118. try {
  119. String jarname = classname.replace('.', '/') + ".class";
  120. JarEntry je = jarfile.getJarEntry(jarname);
  121. if (je != null)
  122. return jarfile.getInputStream(je);
  123. else
  124. return null; // not found
  125. }
  126. catch (IOException e) {}
  127. throw new NotFoundException("broken jar file?: "
  128. + jarfile.getName());
  129. }
  130. public URL find(String classname) {
  131. String jarname = classname.replace('.', '/') + ".class";
  132. JarEntry je = jarfile.getJarEntry(jarname);
  133. if (je != null)
  134. try {
  135. return new URL("jar:" + jarfileURL + "!/" + jarname);
  136. }
  137. catch (MalformedURLException e) {}
  138. return null; // not found
  139. }
  140. public void close() {
  141. try {
  142. jarfile.close();
  143. jarfile = null;
  144. }
  145. catch (IOException e) {}
  146. }
  147. public String toString() {
  148. return jarfile == null ? "<null>" : jarfile.toString();
  149. }
  150. }
  151. final class ClassPoolTail {
  152. protected ClassPathList pathList;
  153. private Hashtable packages; // should be synchronized.
  154. public ClassPoolTail() {
  155. pathList = null;
  156. packages = new Hashtable();
  157. }
  158. public String toString() {
  159. StringBuffer buf = new StringBuffer();
  160. buf.append("[class path: ");
  161. ClassPathList list = pathList;
  162. while (list != null) {
  163. buf.append(list.path.toString());
  164. buf.append(File.pathSeparatorChar);
  165. list = list.next;
  166. }
  167. buf.append(']');
  168. return buf.toString();
  169. }
  170. public synchronized ClassPath insertClassPath(ClassPath cp) {
  171. pathList = new ClassPathList(cp, pathList);
  172. return cp;
  173. }
  174. public synchronized ClassPath appendClassPath(ClassPath cp) {
  175. ClassPathList tail = new ClassPathList(cp, null);
  176. ClassPathList list = pathList;
  177. if (list == null)
  178. pathList = tail;
  179. else {
  180. while (list.next != null)
  181. list = list.next;
  182. list.next = tail;
  183. }
  184. return cp;
  185. }
  186. public synchronized void removeClassPath(ClassPath cp) {
  187. ClassPathList list = pathList;
  188. if (list != null)
  189. if (list.path == cp)
  190. pathList = list.next;
  191. else {
  192. while (list.next != null)
  193. if (list.next.path == cp)
  194. list.next = list.next.next;
  195. else
  196. list = list.next;
  197. }
  198. cp.close();
  199. }
  200. public ClassPath appendSystemPath() {
  201. return appendClassPath(new ClassClassPath());
  202. }
  203. public ClassPath insertClassPath(String pathname)
  204. throws NotFoundException
  205. {
  206. return insertClassPath(makePathObject(pathname));
  207. }
  208. public ClassPath appendClassPath(String pathname)
  209. throws NotFoundException
  210. {
  211. return appendClassPath(makePathObject(pathname));
  212. }
  213. private static ClassPath makePathObject(String pathname)
  214. throws NotFoundException
  215. {
  216. String lower = pathname.toLowerCase();
  217. if (lower.endsWith(".jar") || lower.endsWith(".zip"))
  218. return new JarClassPath(pathname);
  219. int len = pathname.length();
  220. if (len > 2 && pathname.charAt(len - 1) == '*'
  221. && (pathname.charAt(len - 2) == '/'
  222. || pathname.charAt(len - 2) == File.separatorChar)) {
  223. String dir = pathname.substring(0, len - 2);
  224. return new JarDirClassPath(dir);
  225. }
  226. return new DirClassPath(pathname);
  227. }
  228. /**
  229. * You can record "System" so that java.lang.System can be quickly
  230. * found although "System" is not a package name.
  231. */
  232. public void recordInvalidClassName(String name) {
  233. packages.put(name, name);
  234. }
  235. /**
  236. * This method does not close the output stream.
  237. */
  238. void writeClassfile(String classname, OutputStream out)
  239. throws NotFoundException, IOException, CannotCompileException
  240. {
  241. InputStream fin = openClassfile(classname);
  242. if (fin == null)
  243. throw new NotFoundException(classname);
  244. try {
  245. copyStream(fin, out);
  246. }
  247. finally {
  248. fin.close();
  249. }
  250. }
  251. /*
  252. -- faster version --
  253. void checkClassName(String classname) throws NotFoundException {
  254. if (find(classname) == null)
  255. throw new NotFoundException(classname);
  256. }
  257. -- slower version --
  258. void checkClassName(String classname) throws NotFoundException {
  259. InputStream fin = openClassfile(classname);
  260. try {
  261. fin.close();
  262. }
  263. catch (IOException e) {}
  264. }
  265. */
  266. /**
  267. * Opens the class file for the class specified by
  268. * <code>classname</code>.
  269. *
  270. * @param classname a fully-qualified class name
  271. * @return null if the file has not been found.
  272. * @throws NotFoundException if any error is reported by ClassPath.
  273. */
  274. InputStream openClassfile(String classname)
  275. throws NotFoundException
  276. {
  277. if (packages.get(classname) != null)
  278. return null; // not found
  279. ClassPathList list = pathList;
  280. InputStream ins = null;
  281. NotFoundException error = null;
  282. while (list != null) {
  283. try {
  284. ins = list.path.openClassfile(classname);
  285. }
  286. catch (NotFoundException e) {
  287. if (error == null)
  288. error = e;
  289. }
  290. if (ins == null)
  291. list = list.next;
  292. else
  293. return ins;
  294. }
  295. if (error != null)
  296. throw error;
  297. else
  298. return null; // not found
  299. }
  300. /**
  301. * Searches the class path to obtain the URL of the class file
  302. * specified by classname. It is also used to determine whether
  303. * the class file exists.
  304. *
  305. * @param classname a fully-qualified class name.
  306. * @return null if the class file could not be found.
  307. */
  308. public URL find(String classname) {
  309. if (packages.get(classname) != null)
  310. return null;
  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. }