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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2005 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.BufferedInputStream;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.net.URL;
  22. import java.util.Hashtable;
  23. import java.util.Iterator;
  24. import java.util.ArrayList;
  25. import javassist.bytecode.Descriptor;
  26. /**
  27. * A container of <code>CtClass</code> objects.
  28. * A <code>CtClass</code> object must be obtained from this object.
  29. * If <code>get()</code> is called on this object,
  30. * it searches various sources represented by <code>ClassPath</code>
  31. * to find a class file and then it creates a <code>CtClass</code> object
  32. * representing that class file. The created object is returned to the
  33. * caller.
  34. *
  35. * <p><b>Memory consumption memo:</b>
  36. *
  37. * <p><code>ClassPool</code> objects hold all the <code>CtClass</code>es
  38. * that have been created so that the consistency among modified classes
  39. * can be guaranteed. Thus if a large number of <code>CtClass</code>es
  40. * are processed, the <code>ClassPool</code> will consume a huge amount
  41. * of memory. To avoid this, a <code>ClassPool</code> object
  42. * should be recreated, for example, every hundred classes processed.
  43. * Note that <code>getDefault()</code> is a singleton factory.
  44. * Otherwise, <code>detach()</code> in <code>CtClass</code> should be used
  45. * to avoid huge memory consumption.
  46. *
  47. * <p><b><code>ClassPool</code> hierarchy:</b>
  48. *
  49. * <p><code>ClassPool</code>s can make a parent-child hierarchy as
  50. * <code>java.lang.ClassLoader</code>s. If a <code>ClassPool</code> has
  51. * a parent pool, <code>get()</code> first asks the parent pool to find
  52. * a class file. Only if the parent could not find the class file,
  53. * <code>get()</code> searches the <code>ClassPath</code>s of
  54. * the child <code>ClassPool</code>. This search order is reversed if
  55. * <code>ClassPath.childFirstLookup</code> is <code>true</code>.
  56. *
  57. * @see javassist.CtClass
  58. * @see javassist.ClassPath
  59. */
  60. public class ClassPool {
  61. /**
  62. * Determines the search order.
  63. *
  64. * <p>If this field is true, <code>get()</code> first searches the
  65. * class path associated to this <code>ClassPool</code> and then
  66. * the class path associated with the parent <code>ClassPool</code>.
  67. * Otherwise, the class path associated with the parent is searched
  68. * first.
  69. *
  70. * <p>The default value is false.
  71. */
  72. public boolean childFirstLookup = false;
  73. /**
  74. * Turning the automatic pruning on/off.
  75. *
  76. * <p>If this field is true, <code>CtClass</code> objects are
  77. * automatically pruned by default when <code>toBytecode()</code> etc.
  78. * are called. The automatic pruning can be turned on/off individually
  79. * for each <code>CtClass</code> object.
  80. *
  81. * <p>The initial value is true.
  82. *
  83. * @see CtClass#prune()
  84. * @see CtClass#stopPruning(boolean)
  85. */
  86. public static boolean doPruning = true;
  87. /* releaseUnmodifiedClassFile was introduced for avoiding a bug
  88. of JBoss AOP. So the value should be true except for JBoss AOP.
  89. */
  90. /**
  91. * If true, unmodified and not-recently-used class files are
  92. * periodically released for saving memory.
  93. *
  94. * <p>The initial value is true.
  95. */
  96. public static boolean releaseUnmodifiedClassFile = true;
  97. protected ClassPoolTail source;
  98. protected ClassPool parent;
  99. protected Hashtable classes; // should be synchronous
  100. /**
  101. * Table of registered cflow variables.
  102. */
  103. private Hashtable cflow = null; // should be synchronous.
  104. private static final int INIT_HASH_SIZE = 191;
  105. private ArrayList importedPackages;
  106. /**
  107. * Creates a root class pool. No parent class pool is specified.
  108. */
  109. public ClassPool() {
  110. this(null);
  111. }
  112. /**
  113. * Creates a root class pool. If <code>useDefaultPath</code> is
  114. * true, <code>appendSystemPath()</code> is called. Otherwise,
  115. * this constructor is equivalent to the constructor taking no
  116. * parameter.
  117. *
  118. * @param useDefaultPath true if the system search path is
  119. * appended.
  120. */
  121. public ClassPool(boolean useDefaultPath) {
  122. this(null);
  123. if (useDefaultPath)
  124. appendSystemPath();
  125. }
  126. /**
  127. * Creates a class pool.
  128. *
  129. * @param parent the parent of this class pool. If this is a root
  130. * class pool, this parameter must be <code>null</code>.
  131. * @see javassist.ClassPool#getDefault()
  132. */
  133. public ClassPool(ClassPool parent) {
  134. this.classes = new Hashtable(INIT_HASH_SIZE);
  135. this.source = new ClassPoolTail();
  136. this.parent = parent;
  137. if (parent == null) {
  138. CtClass[] pt = CtClass.primitiveTypes;
  139. for (int i = 0; i < pt.length; ++i)
  140. classes.put(pt[i].getName(), pt[i]);
  141. }
  142. this.cflow = null;
  143. clearImportedPackages();
  144. }
  145. /**
  146. * Returns the default class pool.
  147. * The returned object is always identical since this method is
  148. * a singleton factory.
  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. * <p>When this method is called for the first time, the default
  157. * class pool is created with the following code snippet:
  158. *
  159. * <ul><code>ClassPool cp = new ClassPool();
  160. * cp.appendSystemPath();
  161. * </code></ul>
  162. *
  163. * <p>If the default class pool cannot find any class files,
  164. * try <code>ClassClassPath</code> and <code>LoaderClassPath</code>.
  165. *
  166. * @see ClassClassPath
  167. * @see LoaderClassPath
  168. */
  169. public static synchronized ClassPool getDefault() {
  170. if (defaultPool == null) {
  171. defaultPool = new ClassPool(null);
  172. defaultPool.appendSystemPath();
  173. }
  174. return defaultPool;
  175. }
  176. private static ClassPool defaultPool = null;
  177. /**
  178. * Provide a hook so that subclasses can do their own
  179. * caching of classes.
  180. *
  181. * @see #cacheCtClass(String,CtClass,boolean)
  182. * @see #removeCached(String)
  183. */
  184. protected CtClass getCached(String classname) {
  185. return (CtClass)classes.get(classname);
  186. }
  187. /**
  188. * Provides a hook so that subclasses can do their own
  189. * caching of classes.
  190. *
  191. * @see #getCached(String)
  192. * @see #removeCached(String,CtClass)
  193. */
  194. protected void cacheCtClass(String classname, CtClass c, boolean dynamic) {
  195. classes.put(classname, c);
  196. }
  197. /**
  198. * Provide a hook so that subclasses can do their own
  199. * caching of classes.
  200. *
  201. * @see #getCached(String)
  202. * @see #cacheCtClass(String,CtClass,boolean)
  203. */
  204. protected CtClass removeCached(String classname) {
  205. return (CtClass)classes.remove(classname);
  206. }
  207. /**
  208. * Returns the class search path.
  209. */
  210. public String toString() {
  211. return source.toString();
  212. }
  213. /**
  214. * Record a package name so that the Javassist compiler searches
  215. * the package to resolve a class name.
  216. * Don't record the <code>java.lang</code> package, which has
  217. * been implicitly recorded by default.
  218. *
  219. * <p>Note that <code>get()</code> in <code>ClassPool</code> does
  220. * not search the recorded package. Only the compiler searches it.
  221. *
  222. * @param packageName the package name.
  223. * It must not include the last '.' (dot).
  224. * For example, "java.util" is valid but "java.util." is wrong.
  225. * @since 3.1
  226. */
  227. public void importPackage(String packageName) {
  228. importedPackages.add(packageName);
  229. }
  230. /**
  231. * Clear all the package names recorded by <code>importPackage()</code>.
  232. * The <code>java.lang</code> package is not removed.
  233. *
  234. * @see #importPackage(String)
  235. * @since 3.1
  236. */
  237. public void clearImportedPackages() {
  238. importedPackages = new ArrayList();
  239. importedPackages.add("java.lang");
  240. }
  241. /**
  242. * Returns all the package names recorded by <code>importPackage()</code>.
  243. *
  244. * @see #importPackage(String)
  245. * @since 3.1
  246. */
  247. public Iterator getImportedPackages() {
  248. return importedPackages.iterator();
  249. }
  250. /**
  251. * Records a name that never exists.
  252. * For example, a package name can be recorded by this method.
  253. * This would improve execution performance
  254. * since <code>get()</code> does not search the class path at all
  255. * if the given name is an invalid name recorded by this method.
  256. * Note that searching the class path takes relatively long time.
  257. *
  258. * @param name a class name (separeted by dot).
  259. */
  260. public void recordInvalidClassName(String name) {
  261. source.recordInvalidClassName(name);
  262. }
  263. /**
  264. * Records the <code>$cflow</code> variable for the field specified
  265. * by <code>cname</code> and <code>fname</code>.
  266. *
  267. * @param name variable name
  268. * @param cname class name
  269. * @param fname field name
  270. */
  271. void recordCflow(String name, String cname, String fname) {
  272. if (cflow == null)
  273. cflow = new Hashtable();
  274. cflow.put(name, new Object[] { cname, fname });
  275. }
  276. /**
  277. * Undocumented method. Do not use; internal-use only.
  278. *
  279. * @param name the name of <code>$cflow</code> variable
  280. */
  281. public Object[] lookupCflow(String name) {
  282. if (cflow == null)
  283. cflow = new Hashtable();
  284. return (Object[])cflow.get(name);
  285. }
  286. /**
  287. * Reads a class file and constructs a <code>CtClass</code>
  288. * object with a new name.
  289. * This method is useful if you want to generate a new class as a copy
  290. * of another class (except the class name). For example,
  291. *
  292. * <ul><pre>
  293. * getAndRename("Point", "Pair")
  294. * </pre></ul>
  295. *
  296. * returns a <code>CtClass</code> object representing <code>Pair</code>
  297. * class. The definition of <code>Pair</code> is the same as that of
  298. * <code>Point</code> class except the class name since <code>Pair</code>
  299. * is defined by reading <code>Point.class</code>.
  300. *
  301. * @param orgName the original (fully-qualified) class name
  302. * @param newName the new class name
  303. */
  304. public CtClass getAndRename(String orgName, String newName)
  305. throws NotFoundException
  306. {
  307. CtClass clazz = get0(orgName, false);
  308. if (clazz == null)
  309. throw new NotFoundException(orgName);
  310. if (clazz instanceof CtClassType)
  311. ((CtClassType)clazz).setClassPool(this);
  312. clazz.setName(newName); // indirectly calls
  313. // classNameChanged() in this class
  314. return clazz;
  315. }
  316. /*
  317. * This method is invoked by CtClassType.setName(). It removes a
  318. * CtClass object from the hash table and inserts it with the new
  319. * name. Don't delegate to the parent.
  320. */
  321. synchronized void classNameChanged(String oldname, CtClass clazz) {
  322. CtClass c = (CtClass)getCached(oldname);
  323. if (c == clazz) // must check this equation.
  324. removeCached(oldname); // see getAndRename().
  325. String newName = clazz.getName();
  326. checkNotFrozen(newName);
  327. cacheCtClass(newName, clazz, false);
  328. }
  329. /**
  330. * Reads a class file from the source and returns a reference
  331. * to the <code>CtClass</code>
  332. * object representing that class file. If that class file has been
  333. * already read, this method returns a reference to the
  334. * <code>CtClass</code> created when that class file was read at the
  335. * first time.
  336. *
  337. * <p>If <code>classname</code> ends with "[]", then this method
  338. * returns a <code>CtClass</code> object for that array type.
  339. *
  340. * <p>To obtain an inner class, use "$" instead of "." for separating
  341. * the enclosing class name and the inner class name.
  342. *
  343. * @param classname a fully-qualified class name.
  344. */
  345. public CtClass get(String classname) throws NotFoundException {
  346. CtClass clazz;
  347. if (classname == null)
  348. clazz = null;
  349. else
  350. clazz = get0(classname, true);
  351. if (clazz == null)
  352. throw new NotFoundException(classname);
  353. else {
  354. clazz.incGetCounter();
  355. return clazz;
  356. }
  357. }
  358. /**
  359. * @param useCache false if the cached CtClass must be ignored.
  360. * @param searchParent false if the parent class pool is not searched.
  361. * @return null if the class could not be found.
  362. */
  363. protected synchronized CtClass get0(String classname, boolean useCache)
  364. throws NotFoundException
  365. {
  366. CtClass clazz = null;
  367. if (useCache) {
  368. clazz = getCached(classname);
  369. if (clazz != null)
  370. return clazz;
  371. }
  372. if (!childFirstLookup && parent != null) {
  373. clazz = parent.get0(classname, useCache);
  374. if (clazz != null)
  375. return clazz;
  376. }
  377. clazz = createCtClass(classname, useCache);
  378. if (clazz != null) {
  379. if (useCache)
  380. cacheCtClass(classname, clazz, false);
  381. return clazz;
  382. }
  383. if (childFirstLookup && parent != null)
  384. clazz = parent.get0(classname, useCache);
  385. return clazz;
  386. }
  387. /**
  388. * Creates a CtClass object representing the specified class.
  389. * It first examines whether or not the corresponding class
  390. * file exists. If yes, it creates a CtClass object.
  391. *
  392. * @return null if the class file could not be found.
  393. */
  394. protected CtClass createCtClass(String classname, boolean useCache) {
  395. // accept "[L<class name>;" as a class name.
  396. if (classname.charAt(0) == '[')
  397. classname = Descriptor.toClassName(classname);
  398. if (classname.endsWith("[]")) {
  399. String base = classname.substring(0, classname.indexOf('['));
  400. if ((!useCache || getCached(base) == null) && find(base) == null)
  401. return null;
  402. else
  403. return new CtArray(classname, this);
  404. }
  405. else
  406. if (find(classname) == null)
  407. return null;
  408. else
  409. return new CtClassType(classname, this);
  410. }
  411. /**
  412. * Searches the class path to obtain the URL of the class file
  413. * specified by classname. It is also used to determine whether
  414. * the class file exists.
  415. *
  416. * @param classname a fully-qualified class name.
  417. * @return null if the class file could not be found.
  418. * @see CtClass#getURL()
  419. */
  420. public URL find(String classname) {
  421. return source.find(classname);
  422. }
  423. /*
  424. * Is invoked by CtClassType.setName() and methods in this class.
  425. * This method throws an exception if the class is already frozen or
  426. * if this class pool cannot edit the class since it is in a parent
  427. * class pool.
  428. */
  429. void checkNotFrozen(String classname) throws RuntimeException {
  430. CtClass clazz = getCached(classname);
  431. if (clazz == null) {
  432. if (!childFirstLookup && parent != null) {
  433. try {
  434. clazz = parent.get0(classname, true);
  435. }
  436. catch (NotFoundException e) {}
  437. if (clazz != null)
  438. throw new RuntimeException(classname
  439. + " is in a parent ClassPool. Use the parent.");
  440. }
  441. }
  442. else
  443. if (clazz.isFrozen())
  444. throw new RuntimeException(classname
  445. + ": frozen class (cannot edit)");
  446. }
  447. /* for CtClassType.getClassFile2(). Don't delegate to the parent.
  448. */
  449. InputStream openClassfile(String classname) throws NotFoundException {
  450. return source.openClassfile(classname);
  451. }
  452. void writeClassfile(String classname, OutputStream out)
  453. throws NotFoundException, IOException, CannotCompileException
  454. {
  455. source.writeClassfile(classname, out);
  456. }
  457. /**
  458. * Reads class files from the source and returns an array of
  459. * <code>CtClass</code>
  460. * objects representing those class files.
  461. *
  462. * <p>If an element of <code>classnames</code> ends with "[]",
  463. * then this method
  464. * returns a <code>CtClass</code> object for that array type.
  465. *
  466. * @param classnames an array of fully-qualified class name.
  467. */
  468. public CtClass[] get(String[] classnames) throws NotFoundException {
  469. if (classnames == null)
  470. return new CtClass[0];
  471. int num = classnames.length;
  472. CtClass[] result = new CtClass[num];
  473. for (int i = 0; i < num; ++i)
  474. result[i] = get(classnames[i]);
  475. return result;
  476. }
  477. /**
  478. * Reads a class file and obtains a compile-time method.
  479. *
  480. * @param classname the class name
  481. * @param methodname the method name
  482. * @see CtClass#getDeclaredMethod(String)
  483. */
  484. public CtMethod getMethod(String classname, String methodname)
  485. throws NotFoundException
  486. {
  487. CtClass c = get(classname);
  488. return c.getDeclaredMethod(methodname);
  489. }
  490. /**
  491. * Creates a new class (or interface) from the given class file.
  492. * If there already exists a class with the same name, the new class
  493. * overwrites that previous class.
  494. *
  495. * <p>This method is used for creating a <code>CtClass</code> object
  496. * directly from a class file. The qualified class name is obtained
  497. * from the class file; you do not have to explicitly give the name.
  498. *
  499. * @param classfile class file.
  500. * @throws RuntimeException if there is a frozen class with the
  501. * the same name.
  502. * @see javassist.ByteArrayClassPath
  503. */
  504. public CtClass makeClass(InputStream classfile)
  505. throws IOException, RuntimeException {
  506. classfile = new BufferedInputStream(classfile);
  507. CtClass clazz = new CtClassType(classfile, this);
  508. clazz.checkModify();
  509. String classname = clazz.getName();
  510. checkNotFrozen(classname);
  511. cacheCtClass(classname, clazz, true);
  512. return clazz;
  513. }
  514. /**
  515. * Creates a new public class.
  516. * If there already exists a class with the same name, the new class
  517. * overwrites that previous class.
  518. *
  519. * @param classname a fully-qualified class name.
  520. * @throws RuntimeException if the existing class is frozen.
  521. */
  522. public CtClass makeClass(String classname) throws RuntimeException {
  523. return makeClass(classname, null);
  524. }
  525. /**
  526. * Creates a new public class.
  527. * If there already exists a class/interface with the same name,
  528. * the new class overwrites that previous class.
  529. *
  530. * @param classname a fully-qualified class name.
  531. * @param superclass the super class.
  532. * @throws RuntimeException if the existing class is frozen.
  533. */
  534. public synchronized CtClass makeClass(String classname, CtClass superclass)
  535. throws RuntimeException
  536. {
  537. checkNotFrozen(classname);
  538. CtClass clazz = new CtNewClass(classname, this, false, superclass);
  539. cacheCtClass(classname, clazz, true);
  540. return clazz;
  541. }
  542. /**
  543. * Creates a new public nested class.
  544. * This method is called by CtClassType.makeNestedClass().
  545. *
  546. * @param classname a fully-qualified class name.
  547. * @return the nested class.
  548. */
  549. synchronized CtClass makeNestedClass(String classname) {
  550. checkNotFrozen(classname);
  551. CtClass clazz = new CtNewNestedClass(classname, this, false, null);
  552. cacheCtClass(classname, clazz, true);
  553. return clazz;
  554. }
  555. /**
  556. * Creates a new public interface.
  557. * If there already exists a class/interface with the same name,
  558. * the new interface overwrites that previous one.
  559. *
  560. * @param name a fully-qualified interface name.
  561. * @throws RuntimeException if the existing interface is frozen.
  562. */
  563. public CtClass makeInterface(String name) throws RuntimeException {
  564. return makeInterface(name, null);
  565. }
  566. /**
  567. * Creates a new public interface.
  568. * If there already exists a class/interface with the same name,
  569. * the new interface overwrites that previous one.
  570. *
  571. * @param name a fully-qualified interface name.
  572. * @param superclass the super interface.
  573. * @throws RuntimeException if the existing interface is frozen.
  574. */
  575. public synchronized CtClass makeInterface(String name, CtClass superclass)
  576. throws RuntimeException
  577. {
  578. checkNotFrozen(name);
  579. CtClass clazz = new CtNewClass(name, this, true, superclass);
  580. cacheCtClass(name, clazz, true);
  581. return clazz;
  582. }
  583. /**
  584. * Appends the system search path to the end of the
  585. * search path. The system search path
  586. * usually includes the platform library, extension
  587. * libraries, and the search path specified by the
  588. * <code>-classpath</code> option or the <code>CLASSPATH</code>
  589. * environment variable.
  590. *
  591. * @return the appended class path.
  592. */
  593. public ClassPath appendSystemPath() {
  594. return source.appendSystemPath();
  595. }
  596. /**
  597. * Insert a <code>ClassPath</code> object at the head of the
  598. * search path.
  599. *
  600. * @return the inserted class path.
  601. * @see javassist.ClassPath
  602. * @see javassist.URLClassPath
  603. * @see javassist.ByteArrayClassPath
  604. */
  605. public ClassPath insertClassPath(ClassPath cp) {
  606. return source.insertClassPath(cp);
  607. }
  608. /**
  609. * Appends a <code>ClassPath</code> object to the end of the
  610. * search path.
  611. *
  612. * @return the appended class path.
  613. * @see javassist.ClassPath
  614. * @see javassist.URLClassPath
  615. * @see javassist.ByteArrayClassPath
  616. */
  617. public ClassPath appendClassPath(ClassPath cp) {
  618. return source.appendClassPath(cp);
  619. }
  620. /**
  621. * Inserts a directory or a jar (or zip) file at the head of the
  622. * search path.
  623. *
  624. * @param pathname the path name of the directory or jar file.
  625. * It must not end with a path separator ("/").
  626. * @return the inserted class path.
  627. * @throws NotFoundException if the jar file is not found.
  628. */
  629. public ClassPath insertClassPath(String pathname)
  630. throws NotFoundException
  631. {
  632. return source.insertClassPath(pathname);
  633. }
  634. /**
  635. * Appends a directory or a jar (or zip) file to the end of the
  636. * search path.
  637. *
  638. * @param pathname the path name of the directory or jar file.
  639. * It must not end with a path separator ("/").
  640. * @return the appended class path.
  641. * @throws NotFoundException if the jar file is not found.
  642. */
  643. public ClassPath appendClassPath(String pathname)
  644. throws NotFoundException
  645. {
  646. return source.appendClassPath(pathname);
  647. }
  648. /**
  649. * Detatches the <code>ClassPath</code> object from the search path.
  650. * The detached <code>ClassPath</code> object cannot be added
  651. * to the pathagain.
  652. */
  653. public void removeClassPath(ClassPath cp) {
  654. source.removeClassPath(cp);
  655. }
  656. /**
  657. * Appends directories and jar files for search.
  658. *
  659. * <p>The elements of the given path list must be separated by colons
  660. * in Unix or semi-colons in Windows.
  661. *
  662. * @param pathlist a (semi)colon-separated list of
  663. * the path names of directories and jar files.
  664. * The directory name must not end with a path
  665. * separator ("/").
  666. * @throws NotFoundException if a jar file is not found.
  667. */
  668. public void appendPathList(String pathlist) throws NotFoundException {
  669. char sep = File.pathSeparatorChar;
  670. int i = 0;
  671. for (;;) {
  672. int j = pathlist.indexOf(sep, i);
  673. if (j < 0) {
  674. appendClassPath(pathlist.substring(i));
  675. break;
  676. }
  677. else {
  678. appendClassPath(pathlist.substring(i, j));
  679. i = j + 1;
  680. }
  681. }
  682. }
  683. /**
  684. * Converts the given class to a <code>java.lang.Class</code> object.
  685. * Once this method is called, further modifications are not
  686. * allowed any more.
  687. * To load the class, this method uses the context class loader
  688. * of the current thread. If the program is running on some application
  689. * server, the context class loader might be inappropriate to load the
  690. * class.
  691. *
  692. * <p>This method is provided for convenience. If you need more
  693. * complex functionality, you should write your own class loader.
  694. *
  695. * @see #toClass(CtClass, java.lang.ClassLoader)
  696. */
  697. public Class toClass(CtClass clazz) throws CannotCompileException {
  698. return toClass(clazz, Thread.currentThread().getContextClassLoader());
  699. }
  700. /**
  701. * Converts the class to a <code>java.lang.Class</code> object.
  702. * Once this method is called, further modifications are not allowed
  703. * any more.
  704. *
  705. * <p>The class file represented by the given <code>CtClass</code> is
  706. * loaded by the given class loader to construct a
  707. * <code>java.lang.Class</code> object. Since a private method
  708. * on the class loader is invoked through the reflection API,
  709. * the caller must have permissions to do that.
  710. *
  711. * <p>This method is provided for convenience. If you need more
  712. * complex functionality, you should write your own class loader.
  713. *
  714. * @param loader the class loader used to load this class.
  715. */
  716. public Class toClass(CtClass ct, ClassLoader loader)
  717. throws CannotCompileException
  718. {
  719. try {
  720. byte[] b = ct.toBytecode();
  721. Class cl = Class.forName("java.lang.ClassLoader");
  722. java.lang.reflect.Method method =
  723. cl.getDeclaredMethod("defineClass",
  724. new Class[] { String.class, byte[].class,
  725. int.class, int.class });
  726. method.setAccessible(true);
  727. Object[] args = new Object[] { ct.getName(), b, new Integer(0),
  728. new Integer(b.length)};
  729. Class clazz = (Class)method.invoke(loader, args);
  730. method.setAccessible(false);
  731. return clazz;
  732. }
  733. catch (RuntimeException e) {
  734. throw e;
  735. }
  736. catch (java.lang.reflect.InvocationTargetException e) {
  737. throw new CannotCompileException(e.getTargetException());
  738. }
  739. catch (Exception e) {
  740. throw new CannotCompileException(e);
  741. }
  742. }
  743. }