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.

ClassPool.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 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.Hashtable;
  18. /**
  19. * A driver class for controlling bytecode editing with Javassist.
  20. * It manages where a class file is obtained and how it is modified.
  21. *
  22. * <p>A <code>ClassPool</code> object can be regarded as a container
  23. * of <code>CtClass</code> objects. It reads class files on demand
  24. * from various
  25. * sources represented by <code>ClassPath</code> and create
  26. * <code>CtClass</code> objects representing those class files.
  27. * The source may be another <code>ClassPool</code>. If so,
  28. * <code>write()</code> is called on the source <code>ClassPool</code>
  29. * for obtaining a class file.
  30. *
  31. * <p>A <code>CtClass</code>
  32. * object contained in a <code>ClassPool</code> is written to an
  33. * output stream (or a file) if <code>write()</code>
  34. * (or <code>writeFile()</code>) is called on the
  35. * <code>ClassPool</code>.
  36. * <code>write()</code> is typically called by a class loader,
  37. * which obtains the bytecode image to be loaded.
  38. *
  39. * <p>The users can modify <code>CtClass</code> objects
  40. * before those objects are written out.
  41. * To obtain a reference
  42. * to a <code>CtClass</code> object contained in a
  43. * <code>ClassPool</code>, <code>get()</code> should be
  44. * called on the <code>ClassPool</code>. If a <code>CtClass</code>
  45. * object is modified, then the modification is reflected on the resulting
  46. * class file returned by <code>write()</code> in <code>ClassPool</code>.
  47. *
  48. * <p>In summary,
  49. *
  50. * <ul>
  51. * <li><code>get()</code> returns a reference to a <code>CtClass</code>
  52. * object contained in a <code>ClassPool</code>.
  53. *
  54. * <li><code>write()</code> translates a <code>CtClass</code>
  55. * object contained in a <code>ClassPool</code> into a class file
  56. * and writes it to an output stream.
  57. * </ul>
  58. *
  59. * <p>The users can add a listener object receiving an event from a
  60. * <code>ClassPool</code>. An event occurs when a listener is
  61. * added to a <code>ClassPool</code> and when <code>write()</code>
  62. * is called on a <code>ClassPool</code>. The listener class
  63. * must implement <code>Translator</code>. A typical listener object
  64. * is used for modifying a <code>CtClass</code> object <i>on demand</i>
  65. * when it is written to an output stream.
  66. *
  67. * <p>The implementation of this class is thread-safe.
  68. *
  69. * @see javassist.CtClass
  70. * @see javassist.ClassPath
  71. * @see javassist.Translator
  72. */
  73. public class ClassPool {
  74. /* If this field is null, then the object must be an instance of
  75. * ClassPoolTail.
  76. */
  77. protected ClassPool source;
  78. protected Translator translator;
  79. protected Hashtable classes; // should be synchronous
  80. /**
  81. * Creates a class pool.
  82. *
  83. * @param src the source of class files. If it is null,
  84. * the class search path is initially null.
  85. * @see javassist.ClassPool#getDefault()
  86. */
  87. public ClassPool(ClassPool src) {
  88. this(src, null);
  89. }
  90. /**
  91. * Creates a class pool.
  92. *
  93. * @param src the source of class files. If it is null,
  94. * the class search path is initially null.
  95. * @param trans the translator linked to this class pool.
  96. * It may be null.
  97. * @see javassist.ClassPool#getDefault()
  98. */
  99. public ClassPool(ClassPool src, Translator trans)
  100. throws RuntimeException
  101. {
  102. classes = new Hashtable();
  103. CtClass[] pt = CtClass.primitiveTypes;
  104. for (int i = 0; i < pt.length; ++i)
  105. classes.put(pt[i].getName(), pt[i]);
  106. if (src != null)
  107. source = src;
  108. else
  109. source = new ClassPoolTail();
  110. translator = trans;
  111. if (trans != null)
  112. try {
  113. trans.start(this);
  114. }
  115. catch (Exception e) {
  116. throw new RuntimeException(
  117. "Translator.start() throws an exception: "
  118. + e.toString());
  119. }
  120. }
  121. protected ClassPool() {
  122. source = null;
  123. classes = null;
  124. translator = null;
  125. }
  126. /**
  127. * Returns the default class pool.
  128. * The returned object is always identical.
  129. *
  130. * <p>The default class pool searches the system search path,
  131. * which usually includes the platform library, extension
  132. * libraries, and the search path specified by the
  133. * <code>-classpath</code> option or the <code>CLASSPATH</code>
  134. * environment variable.
  135. *
  136. * @param t null or the translator linked to the class pool.
  137. */
  138. public static synchronized ClassPool getDefault(Translator t) {
  139. if (defaultPool == null) {
  140. ClassPoolTail tail = new ClassPoolTail();
  141. tail.appendSystemPath();
  142. defaultPool = new ClassPool(tail, t);
  143. }
  144. return defaultPool;
  145. }
  146. private static ClassPool defaultPool = null;
  147. /**
  148. * Returns the default class pool.
  149. * The returned object is always identical.
  150. *
  151. * <p>This returns the result of <code>getDefault(null)</code>.
  152. *
  153. * @see #getDefault(Translator)
  154. */
  155. public static ClassPool getDefault() {
  156. return getDefault(null);
  157. }
  158. /**
  159. * Returns the class search path.
  160. */
  161. public String toString() {
  162. return source.toString();
  163. }
  164. /**
  165. * Returns the <code>Translator</code> object associated with
  166. * this <code>ClassPool</code>.
  167. */
  168. public Translator getTranslator() { return translator; }
  169. /**
  170. * Table of registered cflow variables.
  171. */
  172. private Hashtable cflow = null; // should be synchronous.
  173. /**
  174. * Records the <code>$cflow</code> variable for the field specified
  175. * by <code>cname</code> and <code>fname</code>.
  176. *
  177. * @param name variable name
  178. * @param cname class name
  179. * @param fname field name
  180. */
  181. void recordCflow(String name, String cname, String fname) {
  182. if (cflow == null)
  183. cflow = new Hashtable();
  184. cflow.put(name, new Object[] { cname, fname });
  185. }
  186. /**
  187. * Undocumented method. Do not use; internal-use only.
  188. *
  189. * @param name the name of <code>$cflow</code> variable
  190. */
  191. public Object[] lookupCflow(String name) {
  192. if (cflow == null)
  193. cflow = new Hashtable();
  194. return (Object[])cflow.get(name);
  195. }
  196. /**
  197. * Writes a class file specified with <code>classname</code>
  198. * in the current directory.
  199. * It never calls <code>onWrite()</code> on a translator.
  200. * It is provided for debugging.
  201. *
  202. * @param classname the name of the class written on a local disk.
  203. */
  204. public void debugWriteFile(String classname)
  205. throws NotFoundException, CannotCompileException, IOException
  206. {
  207. debugWriteFile(classname, ".");
  208. }
  209. /**
  210. * Writes a class file specified with <code>classname</code>.
  211. * It never calls <code>onWrite()</code> on a translator.
  212. * It is provided for debugging.
  213. *
  214. * @param classname the name of the class written on a local disk.
  215. * @param directoryName it must end without a directory separator.
  216. */
  217. public void debugWriteFile(String classname, String directoryName)
  218. throws NotFoundException, CannotCompileException, IOException
  219. {
  220. writeFile(classname, directoryName, false);
  221. }
  222. /* void writeFile(CtClass) should not be defined since writeFile()
  223. * may be called on the class pool that does not contain the given
  224. * CtClass object.
  225. */
  226. /**
  227. * Writes a class file specified with <code>classname</code>
  228. * in the current directory.
  229. * It calls <code>onWrite()</code> on a translator.
  230. *
  231. * @param classname the name of the class written on a local disk.
  232. */
  233. public void writeFile(String classname)
  234. throws NotFoundException, CannotCompileException, IOException
  235. {
  236. writeFile(classname, ".");
  237. }
  238. /**
  239. * Writes a class file specified with <code>classname</code>
  240. * on a local disk.
  241. * It calls <code>onWrite()</code> on a translator.
  242. *
  243. * @param classname the name of the class written on a local disk.
  244. * @param directoryName it must end without a directory separator.
  245. */
  246. public void writeFile(String classname, String directoryName)
  247. throws NotFoundException, CannotCompileException, IOException
  248. {
  249. writeFile(classname, directoryName, true);
  250. }
  251. private void writeFile(String classname, String directoryName,
  252. boolean callback)
  253. throws NotFoundException, CannotCompileException, IOException
  254. {
  255. String filename = directoryName + File.separatorChar
  256. + classname.replace('.', File.separatorChar) + ".class";
  257. int pos = filename.lastIndexOf(File.separatorChar);
  258. if (pos > 0) {
  259. String dir = filename.substring(0, pos);
  260. if (!dir.equals("."))
  261. new File(dir).mkdirs();
  262. }
  263. DataOutputStream out
  264. = new DataOutputStream(new BufferedOutputStream(
  265. new DelayedFileOutputStream(filename)));
  266. write(classname, out, callback);
  267. out.close();
  268. }
  269. static class DelayedFileOutputStream extends OutputStream {
  270. private FileOutputStream file;
  271. private String filename;
  272. DelayedFileOutputStream(String name) {
  273. file = null;
  274. filename = name;
  275. }
  276. private void init() throws IOException {
  277. if (file == null)
  278. file = new FileOutputStream(filename);
  279. }
  280. public void write(int b) throws IOException {
  281. init();
  282. file.write(b);
  283. }
  284. public void write(byte[] b) throws IOException {
  285. init();
  286. file.write(b);
  287. }
  288. public void write(byte[] b, int off, int len) throws IOException {
  289. init();
  290. file.write(b, off, len);
  291. }
  292. public void flush() throws IOException {
  293. init();
  294. file.flush();
  295. }
  296. public void close() throws IOException {
  297. init();
  298. file.close();
  299. }
  300. }
  301. static class LocalClassLoader extends ClassLoader {
  302. public Class loadClass(String name, byte[] classfile)
  303. throws ClassFormatError
  304. {
  305. Class c = defineClass(name, classfile, 0, classfile.length);
  306. resolveClass(c);
  307. return c;
  308. }
  309. };
  310. private static LocalClassLoader classLoader = new LocalClassLoader();
  311. /**
  312. * Returns a <code>java.lang.Class</code> object that has been loaded
  313. * by <code>writeAsClass()</code>. Note that such a class cannot be
  314. * obtained by <code>java.lang.Class.forName()</code> because it has
  315. * been loaded by an internal class loader.
  316. *
  317. * @see #writeAsClass(String)
  318. * @see javassist.CtClass#toClass()
  319. */
  320. public static Class forName(String name) throws ClassNotFoundException {
  321. return classLoader.loadClass(name);
  322. }
  323. /**
  324. * Returns a <code>java.lang.Class</code> object.
  325. * It calls <code>write()</code> to obtain a class file and then
  326. * loads the obtained class file into the JVM. The returned
  327. * <code>Class</code> object represents the loaded class.
  328. *
  329. * <p>This method is provided for convenience. If you need more
  330. * complex functionality, you should write your own class loader.
  331. *
  332. * <p>To load a class file, this method uses an internal class loader.
  333. * Thus, that class file is not loaded by the system class loader,
  334. * which should have loaded this <code>ClassPool</code> class.
  335. * The internal class loader
  336. * loads only the classes explicitly specified by this method
  337. * <code>writeAsClass()</code>. The other classes are loaded
  338. * by the parent class loader (the sytem class loader) by delegation.
  339. * Thus, if a class <code>X</code> loaded by the internal class
  340. * loader refers to a class <code>Y</code>, then the class
  341. * <code>Y</code> is loaded by the parent class loader.
  342. *
  343. * @param classname a fully-qualified class name.
  344. *
  345. * @see #forName(String)
  346. * @see javassist.CtClass#toClass()
  347. * @see javassist.Loader
  348. */
  349. public Class writeAsClass(String classname)
  350. throws NotFoundException, IOException, CannotCompileException
  351. {
  352. try {
  353. return classLoader.loadClass(classname, write(classname));
  354. }
  355. catch (ClassFormatError e) {
  356. throw new CannotCompileException(e, classname);
  357. }
  358. }
  359. /**
  360. * Returns a byte array representing the class file.
  361. * It calls <code>onWrite()</code> on a translator.
  362. *
  363. * @param classname a fully-qualified class name.
  364. */
  365. public byte[] write(String classname)
  366. throws NotFoundException, IOException, CannotCompileException
  367. {
  368. ByteArrayOutputStream barray = new ByteArrayOutputStream();
  369. DataOutputStream out = new DataOutputStream(barray);
  370. write(classname, out, true);
  371. out.close();
  372. return barray.toByteArray();
  373. }
  374. /**
  375. * Writes a class file specified by <code>classname</code>
  376. * to a given output stream.
  377. * It calls <code>onWrite()</code> on a translator.
  378. *
  379. * <p>This method does not close the output stream in the end.
  380. *
  381. * @param classname a fully-qualified class name.
  382. * @param out an output stream
  383. */
  384. public void write(String classname, DataOutputStream out)
  385. throws NotFoundException, CannotCompileException, IOException
  386. {
  387. write(classname, out, true);
  388. }
  389. private void write(String classname, DataOutputStream out,
  390. boolean callback)
  391. throws NotFoundException, CannotCompileException, IOException
  392. {
  393. CtClass clazz = (CtClass)classes.get(classname);
  394. if (callback && translator != null
  395. && (clazz == null || !clazz.isFrozen())) {
  396. translator.onWrite(this, classname);
  397. // The CtClass object might be overwritten.
  398. clazz = (CtClass)classes.get(classname);
  399. }
  400. if (clazz == null || !clazz.isModified()) {
  401. if (clazz != null)
  402. clazz.freeze();
  403. source.write(classname, out);
  404. }
  405. else
  406. clazz.toBytecode(out);
  407. }
  408. /* for CtClassType.getClassFile2()
  409. */
  410. byte[] readSource(String classname)
  411. throws NotFoundException, IOException, CannotCompileException
  412. {
  413. return source.write(classname);
  414. }
  415. /*
  416. * Is invoked by CtClassType.setName().
  417. */
  418. synchronized void classNameChanged(String oldname, CtClass clazz) {
  419. CtClass c = (CtClass)classes.get(oldname);
  420. if (c == clazz) // must check this equation
  421. classes.remove(c);
  422. String newName = clazz.getName();
  423. checkNotFrozen(newName, "the class with the new name is frozen.");
  424. classes.put(newName, clazz);
  425. }
  426. /*
  427. * Is invoked by CtClassType.setName() and methods in this class.
  428. */
  429. void checkNotFrozen(String classname, String errmsg)
  430. throws RuntimeException
  431. {
  432. CtClass c = (CtClass)classes.get(classname);
  433. if (c != null && c.isFrozen())
  434. throw new RuntimeException(errmsg);
  435. }
  436. /**
  437. * Reads a class file and constructs a <code>CtClass</code>
  438. * object with a new name.
  439. * This method is useful if that class file has been already
  440. * loaded and the resulting class is frozen.
  441. *
  442. * @param orgName the original (fully-qualified) class name
  443. * @param newName the new class name
  444. */
  445. public CtClass getAndRename(String orgName, String newName)
  446. throws NotFoundException
  447. {
  448. CtClass clazz = get0(orgName);
  449. clazz.setName(newName); // indirectly calls
  450. // classNameChanged() in this class
  451. return clazz;
  452. }
  453. /**
  454. * Reads a class file from the source and returns a reference
  455. * to the <code>CtClass</code>
  456. * object representing that class file. If that class file has been
  457. * already read, this method returns a reference to the
  458. * <code>CtClass</code> created when that class file was read at the
  459. * first time.
  460. *
  461. * <p>If <code>classname</code> ends with "[]", then this method
  462. * returns a <code>CtClass</code> object for that array type.
  463. *
  464. * @param classname a fully-qualified class name.
  465. */
  466. public synchronized CtClass get(String classname)
  467. throws NotFoundException
  468. {
  469. CtClass clazz = (CtClass)classes.get(classname);
  470. if (clazz == null) {
  471. clazz = get0(classname);
  472. classes.put(classname, clazz);
  473. }
  474. return clazz;
  475. }
  476. private CtClass get0(String classname) throws NotFoundException {
  477. if (classname.endsWith("[]"))
  478. return new CtArray(classname, this);
  479. else {
  480. checkClassName(classname);
  481. return new CtClassType(classname, this);
  482. }
  483. }
  484. /**
  485. * Reads class files from the source and returns an array of
  486. * <code>CtClass</code>
  487. * objects representing those class files.
  488. *
  489. * <p>If an element of <code>classnames</code> ends with "[]",
  490. * then this method
  491. * returns a <code>CtClass</code> object for that array type.
  492. *
  493. * @param classnames an array of fully-qualified class name.
  494. */
  495. public CtClass[] get(String[] classnames) throws NotFoundException {
  496. if (classnames == null)
  497. return new CtClass[0];
  498. int num = classnames.length;
  499. CtClass[] result = new CtClass[num];
  500. for (int i = 0; i < num; ++i)
  501. result[i] = get(classnames[i]);
  502. return result;
  503. }
  504. /**
  505. * Reads a class file and obtains a compile-time method.
  506. *
  507. * @param classname the class name
  508. * @param methodname the method name
  509. *
  510. * @see CtClass#getDeclaredMethod(String)
  511. */
  512. public CtMethod getMethod(String classname, String methodname)
  513. throws NotFoundException
  514. {
  515. CtClass c = get(classname);
  516. return c.getDeclaredMethod(methodname);
  517. }
  518. /**
  519. * Creates a new class from the given class file.
  520. * If there already exists a class with the same name, the new class
  521. * overwrites that previous class.
  522. *
  523. * <p>This method is used for creating a <code>CtClass</code> object
  524. * directly from a class file. The qualified class name is obtained
  525. * from the class file; you do not have to explicitly give the name.
  526. *
  527. * @param classfile class file.
  528. * @exception RuntimeException if there is a frozen class with the
  529. * the same name.
  530. */
  531. public CtClass makeClass(InputStream classfile)
  532. throws IOException, RuntimeException
  533. {
  534. CtClass clazz = new CtClassType(classfile, this);
  535. clazz.checkModify();
  536. String classname = clazz.getName();
  537. checkNotFrozen(classname,
  538. "there is a frozen class with the same name.");
  539. classes.put(classname, clazz);
  540. return clazz;
  541. }
  542. /**
  543. * Creates a new public class.
  544. * If there already exists a class with the same name, the new class
  545. * overwrites that previous class.
  546. *
  547. * @param classname a fully-qualified class name.
  548. * @exception RuntimeException if the existing class is frozen.
  549. */
  550. public CtClass makeClass(String classname) throws RuntimeException {
  551. return makeClass(classname, null);
  552. }
  553. /**
  554. * Creates a new public class.
  555. * If there already exists a class/interface with the same name,
  556. * the new class overwrites that previous class.
  557. *
  558. * @param classname a fully-qualified class name.
  559. * @param superclass the super class.
  560. * @exception RuntimeException if the existing class is frozen.
  561. */
  562. public synchronized CtClass makeClass(String classname, CtClass superclass)
  563. throws RuntimeException
  564. {
  565. checkNotFrozen(classname,
  566. "the class with the given name is frozen.");
  567. CtClass clazz = new CtNewClass(classname, this, false, superclass);
  568. classes.put(classname, clazz);
  569. return clazz;
  570. }
  571. /**
  572. * Creates a new public interface.
  573. * If there already exists a class/interface with the same name,
  574. * the new interface overwrites that previous one.
  575. *
  576. * @param name a fully-qualified interface name.
  577. * @exception RuntimeException if the existing interface is frozen.
  578. */
  579. public CtClass makeInterface(String name) throws RuntimeException {
  580. return makeInterface(name, null);
  581. }
  582. /**
  583. * Creates a new public interface.
  584. * If there already exists a class/interface with the same name,
  585. * the new interface overwrites that previous one.
  586. *
  587. * @param name a fully-qualified interface name.
  588. * @param superclass the super interface.
  589. * @exception RuntimeException if the existing interface is frozen.
  590. */
  591. public synchronized CtClass makeInterface(String name, CtClass superclass)
  592. throws RuntimeException
  593. {
  594. checkNotFrozen(name,
  595. "the interface with the given name is frozen.");
  596. CtClass clazz = new CtNewClass(name, this, true, superclass);
  597. classes.put(name, clazz);
  598. return clazz;
  599. }
  600. /**
  601. * Throws an exception if the class with the specified name does not
  602. * exist.
  603. */
  604. void checkClassName(String classname)
  605. throws NotFoundException
  606. {
  607. source.checkClassName(classname);
  608. }
  609. /**
  610. * Appends the system search path to the end of the
  611. * search path. The system search path
  612. * usually includes the platform library, extension
  613. * libraries, and the search path specified by the
  614. * <code>-classpath</code> option or the <code>CLASSPATH</code>
  615. * environment variable.
  616. *
  617. * @return the appended class path.
  618. */
  619. public ClassPath appendSystemPath() {
  620. return source.appendSystemPath();
  621. }
  622. /**
  623. * Insert a <code>ClassPath</code> object at the head of the
  624. * search path.
  625. *
  626. * @return the inserted class path.
  627. *
  628. * @see javassist.ClassPath
  629. * @see javassist.URLClassPath
  630. * @see javassist.ByteArrayClassPath
  631. */
  632. public ClassPath insertClassPath(ClassPath cp) {
  633. return source.insertClassPath(cp);
  634. }
  635. /**
  636. * Appends a <code>ClassPath</code> object to the end of the
  637. * search path.
  638. *
  639. * @return the appended class path.
  640. *
  641. * @see javassist.ClassPath
  642. * @see javassist.URLClassPath
  643. * @see javassist.ByteArrayClassPath
  644. */
  645. public ClassPath appendClassPath(ClassPath cp) {
  646. return source.appendClassPath(cp);
  647. }
  648. /**
  649. * Inserts a directory or a jar (or zip) file at the head of the
  650. * search path.
  651. *
  652. * @param pathname the path name of the directory or jar file.
  653. * It must not end with a path separator ("/").
  654. * @return the inserted class path.
  655. * @exception NotFoundException if the jar file is not found.
  656. */
  657. public ClassPath insertClassPath(String pathname)
  658. throws NotFoundException
  659. {
  660. return source.insertClassPath(pathname);
  661. }
  662. /**
  663. * Appends a directory or a jar (or zip) file to the end of the
  664. * search path.
  665. *
  666. * @param pathname the path name of the directory or jar file.
  667. * It must not end with a path separator ("/").
  668. * @return the appended class path.
  669. * @exception NotFoundException if the jar file is not found.
  670. */
  671. public ClassPath appendClassPath(String pathname)
  672. throws NotFoundException
  673. {
  674. return source.appendClassPath(pathname);
  675. }
  676. /**
  677. * Detatches the <code>ClassPath</code> object from the search path.
  678. * The detached <code>ClassPath</code> object cannot be added
  679. * to the pathagain.
  680. */
  681. public synchronized void removeClassPath(ClassPath cp) {
  682. source.removeClassPath(cp);
  683. }
  684. /**
  685. * Appends directories and jar files for search.
  686. *
  687. * <p>The elements of the given path list must be separated by colons
  688. * in Unix or semi-colons in Windows.
  689. *
  690. * @param pathlist a (semi)colon-separated list of
  691. * the path names of directories and jar files.
  692. * The directory name must not end with a path
  693. * separator ("/").
  694. *
  695. * @exception NotFoundException if a jar file is not found.
  696. */
  697. public void appendPathList(String pathlist) throws NotFoundException {
  698. char sep = File.pathSeparatorChar;
  699. int i = 0;
  700. for (;;) {
  701. int j = pathlist.indexOf(sep, i);
  702. if (j < 0) {
  703. appendClassPath(pathlist.substring(i));
  704. break;
  705. }
  706. else {
  707. appendClassPath(pathlist.substring(i, j));
  708. i = j + 1;
  709. }
  710. }
  711. }
  712. }