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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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.BufferedOutputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.DataOutputStream;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.net.URL;
  24. import java.security.ProtectionDomain;
  25. import java.util.Collection;
  26. import javassist.bytecode.ClassFile;
  27. import javassist.bytecode.Descriptor;
  28. import javassist.bytecode.Opcode;
  29. import javassist.expr.ExprEditor;
  30. /* Note:
  31. *
  32. * This class is an abstract class and several methods just return null
  33. * or throw an exception. Those methods are overridden in subclasses
  34. * of this class. Read the source code of CtClassType if you are
  35. * interested in the implementation of Javassist.
  36. *
  37. * Subclasses of CtClass are CtClassType, CtPrimitiveType, and CtArray.
  38. */
  39. /**
  40. * An instance of <code>CtClass</code> represents a class.
  41. * It is obtained from <code>ClassPool</code>.
  42. *
  43. * @see ClassPool#get(String)
  44. */
  45. public abstract class CtClass {
  46. protected String qualifiedName;
  47. /**
  48. * The version number of this release.
  49. */
  50. public static final String version = "3.7.0.beta";
  51. /**
  52. * Prints the version number and the copyright notice.
  53. *
  54. * <p>The following command invokes this method:
  55. *
  56. * <ul><pre>java -jar javassist.jar</pre></ul>
  57. */
  58. public static void main(String[] args) {
  59. System.out.println("Javassist version " + CtClass.version);
  60. System.out.println("Copyright (C) 1999-2007 Shigeru Chiba."
  61. + " All Rights Reserved.");
  62. }
  63. static final String javaLangObject = "java.lang.Object";
  64. /**
  65. * The <code>CtClass</code> object representing
  66. * the <code>boolean</code> type.
  67. */
  68. public static CtClass booleanType;
  69. /**
  70. * The <code>CtClass</code> object representing
  71. * the <code>char</code> type.
  72. */
  73. public static CtClass charType;
  74. /**
  75. * The <code>CtClass</code> object representing
  76. * the <code>byte</code> type.
  77. */
  78. public static CtClass byteType;
  79. /**
  80. * The <code>CtClass</code> object representing
  81. * the <code>short</code> type.
  82. */
  83. public static CtClass shortType;
  84. /**
  85. * The <code>CtClass</code> object representing
  86. * the <code>int</code> type.
  87. */
  88. public static CtClass intType;
  89. /**
  90. * The <code>CtClass</code> object representing
  91. * the <code>long</code> type.
  92. */
  93. public static CtClass longType;
  94. /**
  95. * The <code>CtClass</code> object representing
  96. * the <code>float</code> type.
  97. */
  98. public static CtClass floatType;
  99. /**
  100. * The <code>CtClass</code> object representing
  101. * the <code>double</code> type.
  102. */
  103. public static CtClass doubleType;
  104. /**
  105. * The <code>CtClass</code> object representing
  106. * the <code>void</code> type.
  107. */
  108. public static CtClass voidType;
  109. static CtClass[] primitiveTypes;
  110. static {
  111. primitiveTypes = new CtClass[9];
  112. booleanType =
  113. new CtPrimitiveType("boolean", 'Z', "java.lang.Boolean",
  114. "booleanValue", "()Z", Opcode.IRETURN,
  115. Opcode.T_BOOLEAN, 1);
  116. primitiveTypes[0] = booleanType;
  117. charType = new CtPrimitiveType("char", 'C', "java.lang.Character",
  118. "charValue", "()C", Opcode.IRETURN,
  119. Opcode.T_CHAR, 1);
  120. primitiveTypes[1] = charType;
  121. byteType = new CtPrimitiveType("byte", 'B', "java.lang.Byte",
  122. "byteValue", "()B", Opcode.IRETURN,
  123. Opcode.T_BYTE, 1);
  124. primitiveTypes[2] = byteType;
  125. shortType = new CtPrimitiveType("short", 'S', "java.lang.Short",
  126. "shortValue", "()S", Opcode.IRETURN,
  127. Opcode.T_SHORT, 1);
  128. primitiveTypes[3] = shortType;
  129. intType = new CtPrimitiveType("int", 'I', "java.lang.Integer",
  130. "intValue", "()I", Opcode.IRETURN,
  131. Opcode.T_INT, 1);
  132. primitiveTypes[4] = intType;
  133. longType = new CtPrimitiveType("long", 'J', "java.lang.Long",
  134. "longValue", "()J", Opcode.LRETURN,
  135. Opcode.T_LONG, 2);
  136. primitiveTypes[5] = longType;
  137. floatType = new CtPrimitiveType("float", 'F', "java.lang.Float",
  138. "floatValue", "()F", Opcode.FRETURN,
  139. Opcode.T_FLOAT, 1);
  140. primitiveTypes[6] = floatType;
  141. doubleType = new CtPrimitiveType("double", 'D', "java.lang.Double",
  142. "doubleValue", "()D", Opcode.DRETURN,
  143. Opcode.T_DOUBLE, 2);
  144. primitiveTypes[7] = doubleType;
  145. voidType = new CtPrimitiveType("void", 'V', "java.lang.Void",
  146. null, null, Opcode.RETURN, 0, 0);
  147. primitiveTypes[8] = voidType;
  148. }
  149. protected CtClass(String name) {
  150. qualifiedName = name;
  151. }
  152. /**
  153. * Converts the object to a string.
  154. */
  155. public String toString() {
  156. StringBuffer buf = new StringBuffer(getClass().getName());
  157. buf.append("@");
  158. buf.append(Integer.toHexString(hashCode()));
  159. buf.append("[");
  160. extendToString(buf);
  161. buf.append("]");
  162. return buf.toString();
  163. }
  164. /**
  165. * Implemented in subclasses to add to the {@link #toString()} result.
  166. * Subclasses should put a space before each token added to the buffer.
  167. */
  168. protected void extendToString(StringBuffer buffer) {
  169. buffer.append(getName());
  170. }
  171. /**
  172. * Returns a <code>ClassPool</code> for this class.
  173. */
  174. public ClassPool getClassPool() { return null; }
  175. /**
  176. * Returns a class file for this class.
  177. *
  178. * <p>This method is not available if <code>isFrozen()</code>
  179. * is true.
  180. */
  181. public ClassFile getClassFile() {
  182. checkModify();
  183. return getClassFile2();
  184. }
  185. /**
  186. * Returns a class file for this class (read only).
  187. * Normal applications do not need calling this method. Use
  188. * <code>getClassFile()</code>.
  189. *
  190. * <p>The <code>ClassFile</code> object obtained by this method
  191. * is read only. Changes to this object might not be reflected
  192. * on a class file generated by <code>toBytecode()</code>,
  193. * <code>toClass()</code>, etc.
  194. *
  195. * <p>This method is available even if <code>isFrozen()</code>
  196. * is true. However, if the class is frozen, it might be also
  197. * pruned.
  198. *
  199. * @see CtClass#getClassFile()
  200. * @see CtClass#isFrozen()
  201. * @see CtClass#prune()
  202. */
  203. public ClassFile getClassFile2() { return null; }
  204. /**
  205. * Undocumented method. Do not use; internal-use only.
  206. */
  207. public javassist.compiler.AccessorMaker getAccessorMaker() {
  208. return null;
  209. }
  210. /**
  211. * Returns the uniform resource locator (URL) of the class file.
  212. */
  213. public URL getURL() throws NotFoundException {
  214. throw new NotFoundException(getName());
  215. }
  216. /**
  217. * Returns true if the definition of the class has been modified.
  218. */
  219. public boolean isModified() { return false; }
  220. /**
  221. * Returns true if the class has been loaded or written out
  222. * and thus it cannot be modified any more.
  223. *
  224. * @see #defrost()
  225. * @see #detach()
  226. */
  227. public boolean isFrozen() { return true; }
  228. /**
  229. * Makes the class frozen.
  230. *
  231. * @see #isFrozen()
  232. * @see #defrost()
  233. * @since 3.6
  234. */
  235. public void freeze() {}
  236. /* Note: this method is overridden by CtClassType
  237. */
  238. void checkModify() throws RuntimeException {
  239. if (isFrozen())
  240. throw new RuntimeException(getName() + " class is frozen");
  241. // isModified() must return true after this method is invoked.
  242. }
  243. /**
  244. * Defrosts the class so that the class can be modified again.
  245. *
  246. * <p>To avoid changes that will be never reflected,
  247. * the class is frozen to be unmodifiable if it is loaded or
  248. * written out. This method should be called only in a case
  249. * that the class will be reloaded or written out later again.
  250. *
  251. * <p>If <code>defrost()</code> will be called later, pruning
  252. * must be disallowed in advance.
  253. *
  254. * @see #isFrozen()
  255. * @see #stopPruning(boolean)
  256. * @see #detach()
  257. */
  258. public void defrost() {
  259. throw new RuntimeException("cannot defrost " + getName());
  260. }
  261. /**
  262. * Returns <code>true</code> if this object represents a primitive
  263. * Java type: boolean, byte, char, short, int, long, float, double,
  264. * or void.
  265. */
  266. public boolean isPrimitive() { return false; }
  267. /**
  268. * Returns <code>true</code> if this object represents an array type.
  269. */
  270. public boolean isArray() {
  271. return false;
  272. }
  273. /**
  274. * If this object represents an array, this method returns the component
  275. * type of the array. Otherwise, it returns <code>null</code>.
  276. */
  277. public CtClass getComponentType() throws NotFoundException {
  278. return null;
  279. }
  280. /**
  281. * Returns <code>true</code> if this class extends or implements
  282. * <code>clazz</code>. It also returns <code>true</code> if
  283. * this class is the same as <code>clazz</code>.
  284. */
  285. public boolean subtypeOf(CtClass clazz) throws NotFoundException {
  286. return this == clazz || getName().equals(clazz.getName());
  287. }
  288. /**
  289. * Obtains the fully-qualified name of the class.
  290. */
  291. public String getName() { return qualifiedName; }
  292. /**
  293. * Obtains the not-qualified class name.
  294. */
  295. public final String getSimpleName() {
  296. String qname = qualifiedName;
  297. int index = qname.lastIndexOf('.');
  298. if (index < 0)
  299. return qname;
  300. else
  301. return qname.substring(index + 1);
  302. }
  303. /**
  304. * Obtains the package name. It may be <code>null</code>.
  305. */
  306. public final String getPackageName() {
  307. String qname = qualifiedName;
  308. int index = qname.lastIndexOf('.');
  309. if (index < 0)
  310. return null;
  311. else
  312. return qname.substring(0, index);
  313. }
  314. /**
  315. * Sets the class name
  316. *
  317. * @param name fully-qualified name
  318. */
  319. public void setName(String name) {
  320. checkModify();
  321. if (name != null)
  322. qualifiedName = name;
  323. }
  324. /**
  325. * Substitutes <code>newName</code> for all occurrences of a class
  326. * name <code>oldName</code> in the class file.
  327. *
  328. * @param oldName replaced class name
  329. * @param newName substituted class name
  330. */
  331. public void replaceClassName(String oldName, String newName) {
  332. checkModify();
  333. }
  334. /**
  335. * Changes class names appearing in the class file according to the
  336. * given <code>map</code>.
  337. *
  338. * <p>All the class names appearing in the class file are tested
  339. * with <code>map</code> to determine whether each class name is
  340. * replaced or not. Thus this method can be used for collecting
  341. * all the class names in the class file. To do that, first define
  342. * a subclass of <code>ClassMap</code> so that <code>get()</code>
  343. * records all the given parameters. Then, make an instance of
  344. * that subclass as an empty hash-table. Finally, pass that instance
  345. * to this method. After this method finishes, that instance would
  346. * contain all the class names appearing in the class file.
  347. *
  348. * @param map the hashtable associating replaced class names
  349. * with substituted names.
  350. */
  351. public void replaceClassName(ClassMap map) {
  352. checkModify();
  353. }
  354. /**
  355. * Returns a collection of the names of all the classes
  356. * referenced in this class.
  357. * That collection includes the name of this class.
  358. *
  359. * <p>This method may return <code>null</code>.
  360. */
  361. public Collection getRefClasses() {
  362. ClassFile cf = getClassFile2();
  363. if (cf != null) {
  364. ClassMap cm = new ClassMap() {
  365. public void put(String oldname, String newname) {
  366. put0(oldname, newname);
  367. }
  368. public Object get(Object jvmClassName) {
  369. String n = toJavaName((String)jvmClassName);
  370. put0(n, n);
  371. return null;
  372. }
  373. public void fix(String name) {}
  374. };
  375. cf.renameClass(cm);
  376. return cm.values();
  377. }
  378. else
  379. return null;
  380. }
  381. /**
  382. * Determines whether this object represents a class or an interface.
  383. * It returns <code>true</code> if this object represents an interface.
  384. */
  385. public boolean isInterface() {
  386. return false;
  387. }
  388. /**
  389. * Determines whether this object represents an annotation type.
  390. * It returns <code>true</code> if this object represents an annotation type.
  391. *
  392. * @since 3.2
  393. */
  394. public boolean isAnnotation() {
  395. return false;
  396. }
  397. /**
  398. * Determines whether this object represents an enum.
  399. * It returns <code>true</code> if this object represents an enum.
  400. *
  401. * @since 3.2
  402. */
  403. public boolean isEnum() {
  404. return false;
  405. }
  406. /**
  407. * Returns the modifiers for this class, encoded in an integer.
  408. * For decoding, use <code>javassist.Modifier</code>.
  409. *
  410. * <p>If the class is a static nested class (a.k.a. static inner class),
  411. * the returned modifiers include <code>Modifier.STATIC</code>.
  412. *
  413. * @see Modifier
  414. */
  415. public int getModifiers() {
  416. return 0;
  417. }
  418. /**
  419. * Returns the annotations associated with this class.
  420. * For example, if an annotation <code>@Author</code> is associated
  421. * with this class, the returned array contains an <code>Author</code>
  422. * object. The member values can be obtained by calling methods on
  423. * the <code>Author</code> object.
  424. *
  425. * @return an array of annotation-type objects.
  426. * @see CtMember#getAnnotations()
  427. * @since 3.1
  428. */
  429. public Object[] getAnnotations() throws ClassNotFoundException {
  430. return new Object[0];
  431. }
  432. /**
  433. * Returns the annotations associated with this class.
  434. * This method is equivalent to <code>getAnnotations()</code>
  435. * except that, if any annotations are not on the classpath,
  436. * they are not included in the returned array.
  437. *
  438. * @return an array of annotation-type objects.
  439. * @see #getAnnotations()
  440. * @see CtMember#getAvailableAnnotations()
  441. * @since 3.3
  442. */
  443. public Object[] getAvailableAnnotations(){
  444. return new Object[0];
  445. }
  446. /**
  447. * Returns an array of nested classes declared in the class.
  448. * Nested classes are inner classes, anonymous classes, local classes,
  449. * and static nested classes.
  450. *
  451. * @since 3.2
  452. */
  453. public CtClass[] getNestedClasses() throws NotFoundException {
  454. return new CtClass[0];
  455. }
  456. /**
  457. * Sets the modifiers.
  458. *
  459. * <p>If the class is a nested class, this method also modifies
  460. * the class declaring that nested class (i.e. the enclosing
  461. * class is modified).
  462. *
  463. * @param mod modifiers encoded by
  464. * <code>javassist.Modifier</code>
  465. * @see Modifier
  466. */
  467. public void setModifiers(int mod) {
  468. checkModify();
  469. }
  470. /**
  471. * Determines whether the class directly or indirectly extends
  472. * the given class. If this class extends a class A and
  473. * the class A extends a class B, then subclassof(B) returns true.
  474. *
  475. * <p>This method returns true if the given class is identical to
  476. * the class represented by this object.
  477. */
  478. public boolean subclassOf(CtClass superclass) {
  479. return false;
  480. }
  481. /**
  482. * Obtains the class object representing the superclass of the
  483. * class.
  484. * It returns null if this object represents the
  485. * <code>java.lang.Object</code> class and thus it does not have
  486. * the super class.
  487. *
  488. * <p>If this object represents an interface, this method
  489. * always returns the <code>java.lang.Object</code> class.
  490. * To obtain the super interfaces
  491. * extended by that interface, call <code>getInterfaces()</code>.
  492. */
  493. public CtClass getSuperclass() throws NotFoundException {
  494. return null;
  495. }
  496. /**
  497. * Changes a super class unless this object represents an interface.
  498. * The new super class must be compatible with the old one; for example,
  499. * it should inherit from the old super class.
  500. *
  501. * <p>If this object represents an interface, this method is equivalent
  502. * to <code>addInterface()</code>; it appends <code>clazz</code> to
  503. * the list of the super interfaces extended by that interface.
  504. * Note that an interface can extend multiple super interfaces.
  505. *
  506. * @see #replaceClassName(String, String)
  507. * @see #replaceClassName(ClassMap)
  508. */
  509. public void setSuperclass(CtClass clazz) throws CannotCompileException {
  510. checkModify();
  511. }
  512. /**
  513. * Obtains the class objects representing the interfaces implemented
  514. * by the class or, if this object represents an interface, the interfaces
  515. * extended by that interface.
  516. */
  517. public CtClass[] getInterfaces() throws NotFoundException {
  518. return new CtClass[0];
  519. }
  520. /**
  521. * Sets implemented interfaces. If this object represents an interface,
  522. * this method sets the interfaces extended by that interface.
  523. *
  524. * @param list a list of the <code>CtClass</code> objects
  525. * representing interfaces, or
  526. * <code>null</code> if the class implements
  527. * no interfaces.
  528. */
  529. public void setInterfaces(CtClass[] list) {
  530. checkModify();
  531. }
  532. /**
  533. * Adds an interface.
  534. *
  535. * @param anInterface the added interface.
  536. */
  537. public void addInterface(CtClass anInterface) {
  538. checkModify();
  539. }
  540. /**
  541. * If this class is a member class or interface of another class,
  542. * then the class enclosing this class is returned.
  543. *
  544. * @return null if this class is a top-level class or an anonymous class.
  545. */
  546. public CtClass getDeclaringClass() throws NotFoundException {
  547. return null;
  548. }
  549. /**
  550. * Returns the immediately enclosing method of this class.
  551. * This method works only with JDK 1.5 or later.
  552. *
  553. * @return null if this class is not a local class or an anonymous
  554. * class.
  555. */
  556. public CtMethod getEnclosingMethod() throws NotFoundException {
  557. return null;
  558. }
  559. /**
  560. * Makes a new public nested class. If this method is called,
  561. * the <code>CtClass</code>, which encloses the nested class, is modified
  562. * since a class file includes a list of nested classes.
  563. *
  564. * <p>The current implementation only supports a static nested class.
  565. * <code>isStatic</code> must be true.
  566. *
  567. * @param name the simple name of the nested class.
  568. * @param isStatic true if the nested class is static.
  569. */
  570. public CtClass makeNestedClass(String name, boolean isStatic) {
  571. throw new RuntimeException(getName() + " is not a class");
  572. }
  573. /**
  574. * Returns an array containing <code>CtField</code> objects
  575. * representing all the non-private fields of the class.
  576. * That array includes non-private fields inherited from the
  577. * superclasses.
  578. */
  579. public CtField[] getFields() { return new CtField[0]; }
  580. /**
  581. * Returns the field with the specified name. The returned field
  582. * may be a private field declared in a super class or interface.
  583. */
  584. public CtField getField(String name) throws NotFoundException {
  585. throw new NotFoundException(name);
  586. }
  587. /**
  588. * @return null if the specified field is not found.
  589. */
  590. CtField getField2(String name) { return null; }
  591. /**
  592. * Gets all the fields declared in the class. The inherited fields
  593. * are not included.
  594. *
  595. * <p>Note: the result does not include inherited fields.
  596. */
  597. public CtField[] getDeclaredFields() { return new CtField[0]; }
  598. /**
  599. * Retrieves the field with the specified name among the fields
  600. * declared in the class.
  601. *
  602. * <p>Note: this method does not search the superclasses.
  603. */
  604. public CtField getDeclaredField(String name) throws NotFoundException {
  605. throw new NotFoundException(name);
  606. }
  607. /**
  608. * Gets all the constructors and methods declared in the class.
  609. */
  610. public CtBehavior[] getDeclaredBehaviors() {
  611. return new CtBehavior[0];
  612. }
  613. /**
  614. * Returns an array containing <code>CtConstructor</code> objects
  615. * representing all the non-private constructors of the class.
  616. */
  617. public CtConstructor[] getConstructors() {
  618. return new CtConstructor[0];
  619. }
  620. /**
  621. * Returns the constructor with the given signature,
  622. * which is represented by a character string
  623. * called method descriptor.
  624. * For details of the method descriptor, see the JVM specification
  625. * or <code>javassist.bytecode.Descriptor</code>.
  626. *
  627. * @param desc method descriptor
  628. * @see javassist.bytecode.Descriptor
  629. */
  630. public CtConstructor getConstructor(String desc)
  631. throws NotFoundException
  632. {
  633. throw new NotFoundException("no such a constructor");
  634. }
  635. /**
  636. * Gets all the constructors declared in the class.
  637. *
  638. * @see javassist.CtConstructor
  639. */
  640. public CtConstructor[] getDeclaredConstructors() {
  641. return new CtConstructor[0];
  642. }
  643. /**
  644. * Returns a constructor receiving the specified parameters.
  645. *
  646. * @param params parameter types.
  647. */
  648. public CtConstructor getDeclaredConstructor(CtClass[] params)
  649. throws NotFoundException
  650. {
  651. String desc = Descriptor.ofConstructor(params);
  652. return getConstructor(desc);
  653. }
  654. /**
  655. * Gets the class initializer (static constructor)
  656. * declared in the class.
  657. * This method returns <code>null</code> if
  658. * no class initializer is not declared.
  659. *
  660. * @see #makeClassInitializer()
  661. * @see javassist.CtConstructor
  662. */
  663. public CtConstructor getClassInitializer() {
  664. return null;
  665. }
  666. /**
  667. * Returns an array containing <code>CtMethod</code> objects
  668. * representing all the non-private methods of the class.
  669. * That array includes non-private methods inherited from the
  670. * superclasses.
  671. */
  672. public CtMethod[] getMethods() {
  673. return new CtMethod[0];
  674. }
  675. /**
  676. * Returns the method with the given name and signature.
  677. * The returned method may be declared in a super class.
  678. * The method signature is represented by a character string
  679. * called method descriptor,
  680. * which is defined in the JVM specification.
  681. *
  682. * @param name method name
  683. * @param desc method descriptor
  684. * @see CtBehavior#getSignature()
  685. * @see javassist.bytecode.Descriptor
  686. */
  687. public CtMethod getMethod(String name, String desc)
  688. throws NotFoundException
  689. {
  690. throw new NotFoundException(name);
  691. }
  692. /**
  693. * Gets all methods declared in the class. The inherited methods
  694. * are not included.
  695. *
  696. * @see javassist.CtMethod
  697. */
  698. public CtMethod[] getDeclaredMethods() {
  699. return new CtMethod[0];
  700. }
  701. /**
  702. * Retrieves the method with the specified name and parameter types
  703. * among the methods declared in the class.
  704. *
  705. * <p>Note: this method does not search the superclasses.
  706. *
  707. * @param name method name
  708. * @param params parameter types
  709. * @see javassist.CtMethod
  710. */
  711. public CtMethod getDeclaredMethod(String name, CtClass[] params)
  712. throws NotFoundException
  713. {
  714. throw new NotFoundException(name);
  715. }
  716. /**
  717. * Retrieves the method with the specified name among the methods
  718. * declared in the class. If there are multiple methods with
  719. * the specified name, then this method returns one of them.
  720. *
  721. * <p>Note: this method does not search the superclasses.
  722. *
  723. * @see javassist.CtMethod
  724. */
  725. public CtMethod getDeclaredMethod(String name) throws NotFoundException {
  726. throw new NotFoundException(name);
  727. }
  728. /**
  729. * Makes an empty class initializer (static constructor).
  730. * If the class already includes a class initializer,
  731. * this method returns it.
  732. *
  733. * @see #getClassInitializer()
  734. */
  735. public CtConstructor makeClassInitializer()
  736. throws CannotCompileException
  737. {
  738. throw new CannotCompileException("not a class");
  739. }
  740. /**
  741. * Adds a constructor. To add a class initializer (static constructor),
  742. * call <code>makeClassInitializer()</code>.
  743. *
  744. * @see #makeClassInitializer()
  745. */
  746. public void addConstructor(CtConstructor c)
  747. throws CannotCompileException
  748. {
  749. checkModify();
  750. }
  751. /**
  752. * Removes a constructor declared in this class.
  753. *
  754. * @param c removed constructor.
  755. * @throws NotFoundException if the constructor is not found.
  756. */
  757. public void removeConstructor(CtConstructor c) throws NotFoundException {
  758. checkModify();
  759. }
  760. /**
  761. * Adds a method.
  762. */
  763. public void addMethod(CtMethod m) throws CannotCompileException {
  764. checkModify();
  765. }
  766. /**
  767. * Removes a method declared in this class.
  768. *
  769. * @param m removed method.
  770. * @throws NotFoundException if the method is not found.
  771. */
  772. public void removeMethod(CtMethod m) throws NotFoundException {
  773. checkModify();
  774. }
  775. /**
  776. * Adds a field.
  777. *
  778. * <p>The <code>CtField</code> belonging to another
  779. * <code>CtClass</code> cannot be directly added to this class.
  780. * Only a field created for this class can be added.
  781. *
  782. * @see javassist.CtField#CtField(CtField,CtClass)
  783. */
  784. public void addField(CtField f) throws CannotCompileException {
  785. addField(f, (CtField.Initializer)null);
  786. }
  787. /**
  788. * Adds a field with an initial value.
  789. *
  790. * <p>The <code>CtField</code> belonging to another
  791. * <code>CtClass</code> cannot be directly added to this class.
  792. * Only a field created for this class can be added.
  793. *
  794. * <p>The initial value is given as an expression written in Java.
  795. * Any regular Java expression can be used for specifying the initial
  796. * value. The followings are examples.
  797. *
  798. * <ul><pre>
  799. * cc.addField(f, "0") // the initial value is 0.
  800. * cc.addField(f, "i + 1") // i + 1.
  801. * cc.addField(f, "new Point()"); // a Point object.
  802. * </pre></ul>
  803. *
  804. * <p>Here, the type of variable <code>cc</code> is <code>CtClass</code>.
  805. * The type of <code>f</code> is <code>CtField</code>.
  806. *
  807. * @param init an expression for the initial value.
  808. *
  809. * @see javassist.CtField.Initializer#byExpr(String)
  810. * @see javassist.CtField#CtField(CtField,CtClass)
  811. */
  812. public void addField(CtField f, String init)
  813. throws CannotCompileException
  814. {
  815. checkModify();
  816. }
  817. /**
  818. * Adds a field with an initial value.
  819. *
  820. * <p>The <code>CtField</code> belonging to another
  821. * <code>CtClass</code> cannot be directly added to this class.
  822. * Only a field created for this class can be added.
  823. *
  824. * <p>For example,
  825. *
  826. * <ul><pre>
  827. * CtClass cc = ...;
  828. * addField(new CtField(CtClass.intType, "i", cc),
  829. * CtField.Initializer.constant(1));
  830. * </pre></ul>
  831. *
  832. * <p>This code adds an <code>int</code> field named "i". The
  833. * initial value of this field is 1.
  834. *
  835. * @param init specifies the initial value of the field.
  836. *
  837. * @see javassist.CtField#CtField(CtField,CtClass)
  838. */
  839. public void addField(CtField f, CtField.Initializer init)
  840. throws CannotCompileException
  841. {
  842. checkModify();
  843. }
  844. /**
  845. * Removes a field declared in this class.
  846. *
  847. * @param f removed field.
  848. * @throws NotFoundException if the field is not found.
  849. */
  850. public void removeField(CtField f) throws NotFoundException {
  851. checkModify();
  852. }
  853. /**
  854. * Obtains an attribute with the given name.
  855. * If that attribute is not found in the class file, this
  856. * method returns null.
  857. *
  858. * <p>This is a convenient method mainly for obtaining
  859. * a user-defined attribute. For dealing with attributes, see the
  860. * <code>javassist.bytecode</code> package. For example, the following
  861. * expression returns all the attributes of a class file.
  862. *
  863. * <ul><pre>
  864. * getClassFile().getAttributes()
  865. * </pre></ul>
  866. *
  867. * @param name attribute name
  868. * @see javassist.bytecode.AttributeInfo
  869. */
  870. public byte[] getAttribute(String name) {
  871. return null;
  872. }
  873. /**
  874. * Adds a named attribute.
  875. * An arbitrary data (smaller than 64Kb) can be saved in the class
  876. * file. Some attribute name are reserved by the JVM.
  877. * The attributes with the non-reserved names are ignored when a
  878. * class file is loaded into the JVM.
  879. * If there is already an attribute with
  880. * the same name, this method substitutes the new one for it.
  881. *
  882. * <p>This is a convenient method mainly for adding
  883. * a user-defined attribute. For dealing with attributes, see the
  884. * <code>javassist.bytecode</code> package. For example, the following
  885. * expression adds an attribute <code>info</code> to a class file.
  886. *
  887. * <ul><pre>
  888. * getClassFile().addAttribute(info)
  889. * </pre></ul>
  890. *
  891. * @param name attribute name
  892. * @param data attribute value
  893. * @see javassist.bytecode.AttributeInfo
  894. */
  895. public void setAttribute(String name, byte[] data) {
  896. checkModify();
  897. }
  898. /**
  899. * Applies the given converter to all methods and constructors
  900. * declared in the class. This method calls <code>instrument()</code>
  901. * on every <code>CtMethod</code> and <code>CtConstructor</code> object
  902. * in the class.
  903. *
  904. * @param converter specifies how to modify.
  905. */
  906. public void instrument(CodeConverter converter)
  907. throws CannotCompileException
  908. {
  909. checkModify();
  910. }
  911. /**
  912. * Modifies the bodies of all methods and constructors
  913. * declared in the class. This method calls <code>instrument()</code>
  914. * on every <code>CtMethod</code> and <code>CtConstructor</code> object
  915. * in the class.
  916. *
  917. * @param editor specifies how to modify.
  918. */
  919. public void instrument(ExprEditor editor)
  920. throws CannotCompileException
  921. {
  922. checkModify();
  923. }
  924. /**
  925. * Converts this class to a <code>java.lang.Class</code> object.
  926. * Once this method is called, further modifications are not
  927. * allowed any more.
  928. * To load the class, this method uses the context class loader
  929. * of the current thread. If the program is running on some application
  930. * server, the context class loader might be inappropriate to load the
  931. * class.
  932. *
  933. * <p>This method is provided for convenience. If you need more
  934. * complex functionality, you should write your own class loader.
  935. *
  936. * <p>Note: this method calls <code>toClass()</code>
  937. * in <code>ClassPool</code>.
  938. *
  939. * <p><b>Warining:</b> A Class object returned by this method may not
  940. * work with a security manager or a signed jar file because a
  941. * protection domain is not specified.
  942. *
  943. * @see #toClass(java.lang.ClassLoader,ProtectionDomain)
  944. * @see ClassPool#toClass(CtClass)
  945. */
  946. public Class toClass() throws CannotCompileException {
  947. return getClassPool().toClass(this);
  948. }
  949. /**
  950. * Converts this class to a <code>java.lang.Class</code> object.
  951. * Once this method is called, further modifications are not allowed
  952. * any more.
  953. *
  954. * <p>The class file represented by this <code>CtClass</code> is
  955. * loaded by the given class loader to construct a
  956. * <code>java.lang.Class</code> object. Since a private method
  957. * on the class loader is invoked through the reflection API,
  958. * the caller must have permissions to do that.
  959. *
  960. * <p>An easy way to obtain <code>ProtectionDomain</code> object is
  961. * to call <code>getProtectionDomain()</code>
  962. * in <code>java.lang.Class</code>. It returns the domain that
  963. * the class belongs to.
  964. *
  965. * <p>This method is provided for convenience. If you need more
  966. * complex functionality, you should write your own class loader.
  967. *
  968. * <p>Note: this method calls <code>toClass()</code>
  969. * in <code>ClassPool</code>.
  970. *
  971. * @param loader the class loader used to load this class.
  972. * If it is null, the class loader returned by
  973. * {@link ClassPool#getClassLoader()} is used.
  974. * @param domain the protection domain that the class belongs to.
  975. * If it is null, the default domain created
  976. * by <code>java.lang.ClassLoader</code> is used.
  977. * @see ClassPool#toClass(CtClass,java.lang.ClassLoader)
  978. * @since 3.3
  979. */
  980. public Class toClass(ClassLoader loader, ProtectionDomain domain)
  981. throws CannotCompileException
  982. {
  983. ClassPool cp = getClassPool();
  984. if (loader == null)
  985. loader = cp.getClassLoader();
  986. return cp.toClass(this, loader, domain);
  987. }
  988. /**
  989. * Converts this class to a <code>java.lang.Class</code> object.
  990. *
  991. * <p><b>Warining:</b> A Class object returned by this method may not
  992. * work with a security manager or a signed jar file because a
  993. * protection domain is not specified.
  994. *
  995. * @deprecated Replaced by {@link #toClass(ClassLoader,ProtectionDomain)}
  996. */
  997. public final Class toClass(ClassLoader loader)
  998. throws CannotCompileException
  999. {
  1000. return getClassPool().toClass(this, loader);
  1001. }
  1002. /**
  1003. * Removes this <code>CtClass</code> object from the
  1004. * <code>ClassPool</code>.
  1005. * After this method is called, any method cannot be called on the
  1006. * removed <code>CtClass</code> object.
  1007. *
  1008. * <p>If <code>get()</code> in <code>ClassPool</code> is called
  1009. * with the name of the removed method,
  1010. * the <code>ClassPool</code> will read the class file again
  1011. * and constructs another <code>CtClass</code> object representing
  1012. * the same class.
  1013. */
  1014. public void detach() {
  1015. ClassPool cp = getClassPool();
  1016. CtClass obj = cp.removeCached(getName());
  1017. if (obj != this)
  1018. cp.cacheCtClass(getName(), obj, false);
  1019. }
  1020. /**
  1021. * Disallows (or allows) automatically pruning this <code>CtClass</code>
  1022. * object.
  1023. *
  1024. * <p>
  1025. * Javassist can automatically prune a <code>CtClass</code> object
  1026. * when <code>toBytecode()</code> (or <code>toClass()</code>,
  1027. * <code>writeFile()</code>) is called.
  1028. * Since a <code>ClassPool</code> holds all instances of <code>CtClass</code>
  1029. * even after <code>toBytecode()</code> (or <code>toClass()</code>,
  1030. * <code>writeFile()</code>) is called, pruning may significantly
  1031. * save memory consumption.
  1032. *
  1033. * <p>If <code>ClassPool.doPruning</code> is true, the automatic pruning
  1034. * is on by default. Otherwise, it is off. The default value of
  1035. * <code>ClassPool.doPruning</code> is false.
  1036. *
  1037. * @param stop disallow pruning if true. Otherwise, allow.
  1038. * @return the previous status of pruning. true if pruning is already stopped.
  1039. *
  1040. * @see #detach()
  1041. * @see #prune()
  1042. * @see ClassPool#doPruning
  1043. */
  1044. public boolean stopPruning(boolean stop) { return true; }
  1045. /**
  1046. * Discards unnecessary attributes, in particuar,
  1047. * <code>CodeAttribute</code>s (method bodies) of the class,
  1048. * to minimize the memory footprint.
  1049. * After calling this method, the class is read only.
  1050. * It cannot be modified any more.
  1051. * Furthermore, <code>toBytecode()</code>,
  1052. * <code>writeFile()</code>, <code>toClass()</code>,
  1053. * or <code>instrument()</code> cannot be called.
  1054. * However, the method names and signatures in the class etc.
  1055. * are still accessible.
  1056. *
  1057. * <p><code>toBytecode()</code>, <code>writeFile()</code>, and
  1058. * <code>toClass()</code> internally call this method if
  1059. * automatic pruning is on.
  1060. *
  1061. * <p>According to some experiments, pruning does not really reduce
  1062. * memory consumption. Only about 20%. Since pruning takes time,
  1063. * it might not pay off. So the automatic pruning is off by default.
  1064. *
  1065. * @see #stopPruning(boolean)
  1066. * @see #detach()
  1067. * @see ClassPool#doPruning
  1068. *
  1069. * @see #toBytecode()
  1070. * @see #toClass()
  1071. * @see #writeFile()
  1072. * @see #instrument(CodeConverter)
  1073. * @see #instrument(ExprEditor)
  1074. */
  1075. public void prune() {}
  1076. /* Called by get() in ClassPool.
  1077. * CtClassType overrides this method.
  1078. */
  1079. void incGetCounter() {}
  1080. /**
  1081. * Converts this class to a class file.
  1082. * Once this method is called, further modifications are not
  1083. * possible any more.
  1084. *
  1085. * @return the contents of the class file.
  1086. */
  1087. public byte[] toBytecode() throws IOException, CannotCompileException {
  1088. ByteArrayOutputStream barray = new ByteArrayOutputStream();
  1089. DataOutputStream out = new DataOutputStream(barray);
  1090. try {
  1091. toBytecode(out);
  1092. }
  1093. finally {
  1094. out.close();
  1095. }
  1096. return barray.toByteArray();
  1097. }
  1098. /**
  1099. * Writes a class file represented by this <code>CtClass</code>
  1100. * object in the current directory.
  1101. * Once this method is called, further modifications are not
  1102. * possible any more.
  1103. */
  1104. public void writeFile()
  1105. throws NotFoundException, IOException, CannotCompileException
  1106. {
  1107. writeFile(".");
  1108. }
  1109. /**
  1110. * Writes a class file represented by this <code>CtClass</code>
  1111. * object on a local disk.
  1112. * Once this method is called, further modifications are not
  1113. * possible any more.
  1114. *
  1115. * @param directoryName it must end without a directory separator.
  1116. */
  1117. public void writeFile(String directoryName)
  1118. throws CannotCompileException, IOException
  1119. {
  1120. String classname = getName();
  1121. String filename = directoryName + File.separatorChar
  1122. + classname.replace('.', File.separatorChar) + ".class";
  1123. int pos = filename.lastIndexOf(File.separatorChar);
  1124. if (pos > 0) {
  1125. String dir = filename.substring(0, pos);
  1126. if (!dir.equals("."))
  1127. new File(dir).mkdirs();
  1128. }
  1129. DataOutputStream out
  1130. = new DataOutputStream(new BufferedOutputStream(
  1131. new DelayedFileOutputStream(filename)));
  1132. try {
  1133. toBytecode(out);
  1134. }
  1135. finally {
  1136. out.close();
  1137. }
  1138. }
  1139. /**
  1140. * Writes a class file as <code>writeFile()</code> does although this
  1141. * method does not prune or freeze the class after writing the class
  1142. * file. Note that, once <code>writeFile()</code> or <code>toBytecode()</code>
  1143. * is called, it cannot be called again since the class is pruned and frozen.
  1144. * This method would be useful for debugging.
  1145. */
  1146. public void debugWriteFile() {
  1147. try {
  1148. boolean p = stopPruning(true);
  1149. writeFile();
  1150. defrost();
  1151. stopPruning(p);
  1152. }
  1153. catch (Exception e) {
  1154. throw new RuntimeException(e);
  1155. }
  1156. }
  1157. static class DelayedFileOutputStream extends OutputStream {
  1158. private FileOutputStream file;
  1159. private String filename;
  1160. DelayedFileOutputStream(String name) {
  1161. file = null;
  1162. filename = name;
  1163. }
  1164. private void init() throws IOException {
  1165. if (file == null)
  1166. file = new FileOutputStream(filename);
  1167. }
  1168. public void write(int b) throws IOException {
  1169. init();
  1170. file.write(b);
  1171. }
  1172. public void write(byte[] b) throws IOException {
  1173. init();
  1174. file.write(b);
  1175. }
  1176. public void write(byte[] b, int off, int len) throws IOException {
  1177. init();
  1178. file.write(b, off, len);
  1179. }
  1180. public void flush() throws IOException {
  1181. init();
  1182. file.flush();
  1183. }
  1184. public void close() throws IOException {
  1185. init();
  1186. file.close();
  1187. }
  1188. }
  1189. /**
  1190. * Converts this class to a class file.
  1191. * Once this method is called, further modifications are not
  1192. * possible any more.
  1193. *
  1194. * <p>This method dose not close the output stream in the end.
  1195. *
  1196. * @param out the output stream that a class file is written to.
  1197. */
  1198. public void toBytecode(DataOutputStream out)
  1199. throws CannotCompileException, IOException
  1200. {
  1201. throw new CannotCompileException("not a class");
  1202. }
  1203. /**
  1204. * Makes a unique member name. This method guarantees that
  1205. * the returned name is not used as a prefix of any methods
  1206. * or fields visible in this class.
  1207. * If the returned name is XYZ, then any method or field names
  1208. * in this class do not start with XYZ.
  1209. *
  1210. * @param prefix the prefix of the member name.
  1211. */
  1212. public String makeUniqueName(String prefix) {
  1213. throw new RuntimeException("not available in " + getName());
  1214. }
  1215. /* Invoked from ClassPool#compress().
  1216. * This method is overridden by CtClassType.
  1217. */
  1218. void compress() {}
  1219. }