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

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