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.

CtClass.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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.DataOutputStream;
  17. import java.io.IOException;
  18. import javassist.bytecode.*;
  19. import java.util.Collection;
  20. import javassist.expr.ExprEditor;
  21. // Subclasses of CtClass: CtClassType, CtPrimitiveType, and CtArray
  22. /**
  23. * An instance of <code>CtClass</code> represents a class.
  24. * It is obtained from <code>ClassPool</code>.
  25. *
  26. * @see ClassPool#get(String)
  27. */
  28. public abstract class CtClass {
  29. protected String qualifiedName;
  30. /**
  31. * The version number of this release.
  32. */
  33. public static final String version = "2.6";
  34. static final String javaLangObject = "java.lang.Object";
  35. /**
  36. * The <code>CtClass</code> object representing
  37. * the <code>boolean</code> type.
  38. */
  39. public static CtClass booleanType;
  40. /**
  41. * The <code>CtClass</code> object representing
  42. * the <code>char</code> type.
  43. */
  44. public static CtClass charType;
  45. /**
  46. * The <code>CtClass</code> object representing
  47. * the <code>byte</code> type.
  48. */
  49. public static CtClass byteType;
  50. /**
  51. * The <code>CtClass</code> object representing
  52. * the <code>short</code> type.
  53. */
  54. public static CtClass shortType;
  55. /**
  56. * The <code>CtClass</code> object representing
  57. * the <code>int</code> type.
  58. */
  59. public static CtClass intType;
  60. /**
  61. * The <code>CtClass</code> object representing
  62. * the <code>long</code> type.
  63. */
  64. public static CtClass longType;
  65. /**
  66. * The <code>CtClass</code> object representing
  67. * the <code>float</code> type.
  68. */
  69. public static CtClass floatType;
  70. /**
  71. * The <code>CtClass</code> object representing
  72. * the <code>double</code> type.
  73. */
  74. public static CtClass doubleType;
  75. /**
  76. * The <code>CtClass</code> object representing
  77. * the <code>void</code> type.
  78. */
  79. public static CtClass voidType;
  80. static CtClass[] primitiveTypes;
  81. static {
  82. primitiveTypes = new CtClass[9];
  83. booleanType = new CtPrimitiveType("boolean", 'Z', "java.lang.Boolean",
  84. "booleanValue", "()Z", Opcode.IRETURN,
  85. Opcode.T_BOOLEAN, 1);
  86. primitiveTypes[0] = booleanType;
  87. charType = new CtPrimitiveType("char", 'C', "java.lang.Character",
  88. "charValue", "()C", Opcode.IRETURN,
  89. Opcode.T_CHAR, 1);
  90. primitiveTypes[1] = charType;
  91. byteType = new CtPrimitiveType("byte", 'B', "java.lang.Byte",
  92. "byteValue", "()B", Opcode.IRETURN,
  93. Opcode.T_BYTE, 1);
  94. primitiveTypes[2] = byteType;
  95. shortType = new CtPrimitiveType("short", 'S', "java.lang.Short",
  96. "shortValue", "()S", Opcode.IRETURN,
  97. Opcode.T_SHORT, 1);
  98. primitiveTypes[3] = shortType;
  99. intType = new CtPrimitiveType("int", 'I', "java.lang.Integer",
  100. "intValue", "()I", Opcode.IRETURN,
  101. Opcode.T_INT, 1);
  102. primitiveTypes[4] = intType;
  103. longType = new CtPrimitiveType("long", 'J', "java.lang.Long",
  104. "longValue", "()J", Opcode.LRETURN,
  105. Opcode.T_LONG, 2);
  106. primitiveTypes[5] = longType;
  107. floatType = new CtPrimitiveType("float", 'F', "java.lang.Float",
  108. "floatValue", "()F", Opcode.FRETURN,
  109. Opcode.T_FLOAT, 1);
  110. primitiveTypes[6] = floatType;
  111. doubleType = new CtPrimitiveType("double", 'D', "java.lang.Double",
  112. "doubleValue", "()D", Opcode.DRETURN,
  113. Opcode.T_DOUBLE, 2);
  114. primitiveTypes[7] = doubleType;
  115. voidType = new CtPrimitiveType("void", 'V', "java.lang.Void",
  116. null, null, Opcode.RETURN, 0, 0);
  117. primitiveTypes[8] = voidType;
  118. }
  119. protected CtClass(String name) {
  120. qualifiedName = name;
  121. }
  122. /**
  123. * Returns a <code>ClassPool</code> for this class.
  124. */
  125. public ClassPool getClassPool() { return null; }
  126. /**
  127. * Returns a class file for this class.
  128. *
  129. * <p>This method is not available if <code>isFrozen()</code>
  130. * is true.
  131. */
  132. public ClassFile getClassFile() {
  133. checkModify();
  134. return getClassFile2();
  135. }
  136. /**
  137. * Undocumented method. Do not use; internal-use only.
  138. */
  139. public ClassFile getClassFile2() { return null; }
  140. /**
  141. * Returns true if the definition of the class has been modified.
  142. */
  143. public boolean isModified() { return false; }
  144. /**
  145. * Returns true if the class has been loaded or written out
  146. * and thus it cannot be modified any more.
  147. *
  148. * @see #defrost()
  149. */
  150. public boolean isFrozen() { return true; }
  151. void freeze() {}
  152. void checkModify() throws RuntimeException {
  153. if (isFrozen())
  154. throw new RuntimeException("the class is frozen");
  155. // isModified() must return true after this method is invoked.
  156. }
  157. /**
  158. * Defrosts the class so that the class can be modified again.
  159. *
  160. * To avoid changes that will be never reflected,
  161. * the class is frozen to be unmodifiable if it is loaded or
  162. * written out. This method should be called only in a case
  163. * that the class will be reloaded or written out later again.
  164. *
  165. * @see #isFrozen()
  166. */
  167. public void defrost() {
  168. throw new RuntimeException("cannot defrost " + getName());
  169. }
  170. /**
  171. * Returns <code>true</code> if this object represents a primitive
  172. * Java type: boolean, byte, char, short, int, long, float, double,
  173. * or void.
  174. */
  175. public boolean isPrimitive() { return false; }
  176. /**
  177. * Returns <code>true</code> if this object represents an array type.
  178. */
  179. public boolean isArray() {
  180. return false;
  181. }
  182. /**
  183. * If this object represents an array, this method returns the component
  184. * type of the array. Otherwise, it returns <code>null</code>.
  185. */
  186. public CtClass getComponentType() throws NotFoundException {
  187. return null;
  188. }
  189. /**
  190. * Returns <code>true</code> if this class extends or implements
  191. * <code>clazz</code>. It also returns <code>true</code> if
  192. * this class is the same as <code>clazz</code>.
  193. */
  194. public boolean subtypeOf(CtClass clazz) throws NotFoundException {
  195. return this == clazz || getName().equals(clazz.getName());
  196. }
  197. /**
  198. * Obtains the fully-qualified name of the class.
  199. */
  200. public String getName() { return qualifiedName; }
  201. /**
  202. * Obtains the not-qualified class name.
  203. */
  204. public final String getSimpleName() {
  205. String qname = qualifiedName;
  206. int index = qname.lastIndexOf('.');
  207. if (index < 0)
  208. return qname;
  209. else
  210. return qname.substring(index + 1);
  211. }
  212. /**
  213. * Obtains the package name. It may be <code>null</code>.
  214. */
  215. public final String getPackageName() {
  216. String qname = qualifiedName;
  217. int index = qname.lastIndexOf('.');
  218. if (index < 0)
  219. return null;
  220. else
  221. return qname.substring(0, index);
  222. }
  223. /**
  224. * Sets the class name
  225. *
  226. * @param name fully-qualified name
  227. */
  228. public void setName(String name) {
  229. checkModify();
  230. if (name != null)
  231. qualifiedName = name;
  232. }
  233. /**
  234. * Substitutes <code>newName</code> for all occurrences of a class
  235. * name <code>oldName</code> in the class file.
  236. *
  237. * @param oldName replaced class name
  238. * @param newName substituted class name
  239. */
  240. public void replaceClassName(String oldname, String newname) {
  241. checkModify();
  242. }
  243. /**
  244. * Changes class names appearing in the class file according to the
  245. * given <code>map</code>.
  246. *
  247. * <p>All the class names appearing in the class file are tested
  248. * with <code>map</code> to determine whether each class name is
  249. * replaced or not. Thus this method can be used for collecting
  250. * all the class names in the class file. To do that, first define
  251. * a subclass of <code>ClassMap</code> so that <code>get()</code>
  252. * records all the given parameters. Then, make an instance of
  253. * that subclass as an empty hash-table. Finally, pass that instance
  254. * to this method. After this method finishes, that instance would
  255. * contain all the class names appearing in the class file.
  256. *
  257. * @param map the hashtable associating replaced class names
  258. * with substituted names.
  259. */
  260. public void replaceClassName(ClassMap map) {
  261. checkModify();
  262. }
  263. /**
  264. * Returns a collection of the names of all the classes
  265. * referenced in this class.
  266. * That collection includes the name of this class.
  267. *
  268. * <p>This method may return <code>null</code>.
  269. */
  270. public Collection getRefClasses() {
  271. ClassFile cf = getClassFile2();
  272. if (cf != null) {
  273. ClassMap cm = new ClassMap() {
  274. public void put(String oldname, String newname) {
  275. put0(oldname, newname);
  276. }
  277. public Object get(Object jvmClassName) {
  278. String n = toJavaName((String)jvmClassName);
  279. put0(n, n);
  280. return null;
  281. }
  282. public void fix(String name) {}
  283. };
  284. cf.renameClass(cm);
  285. return cm.values();
  286. }
  287. else
  288. return null;
  289. }
  290. /**
  291. * Determines whether this object represents a class or an interface.
  292. * It returns <code>true</code> if this object represents an interface.
  293. */
  294. public boolean isInterface() {
  295. return false;
  296. }
  297. /**
  298. * Returns the modifiers for this class, encoded in an integer.
  299. * For decoding, use <code>javassist.Modifier</code>.
  300. *
  301. * @see Modifier
  302. */
  303. public int getModifiers() {
  304. return 0;
  305. }
  306. /**
  307. * Sets the modifiers.
  308. *
  309. * @param mod modifiers encoded by
  310. * <code>javassist.Modifier</code>
  311. * @see Modifier
  312. */
  313. public void setModifiers(int mod) {
  314. checkModify();
  315. }
  316. /**
  317. * Determines whether the class directly or indirectly extends
  318. * the given class. If this class extends a class A and
  319. * the class A extends a class B, then subclassof(B) returns true.
  320. *
  321. * <p>This method returns true if the given class is identical to
  322. * the class represented by this object.
  323. */
  324. public boolean subclassOf(CtClass superclass) {
  325. return false;
  326. }
  327. /**
  328. * Obtains the class object representing the superclass of the
  329. * class.
  330. * It returns null if this object represents the
  331. * <code>java.lang.Object</code> class and thus it does not have
  332. * the super class.
  333. */
  334. public CtClass getSuperclass() throws NotFoundException {
  335. return null;
  336. }
  337. /**
  338. * Changes a super class. The new super class must be compatible
  339. * with the old one.
  340. */
  341. public void setSuperclass(CtClass clazz) throws CannotCompileException {
  342. checkModify();
  343. }
  344. /**
  345. * Obtains the class objects representing the interfaces of the
  346. * class.
  347. */
  348. public CtClass[] getInterfaces() throws NotFoundException {
  349. return new CtClass[0];
  350. }
  351. /**
  352. * Sets interfaces.
  353. *
  354. * @param list a list of the <code>CtClass</code> objects
  355. * representing interfaces, or
  356. * <code>null</code> if the class implements
  357. * no interfaces.
  358. */
  359. public void setInterfaces(CtClass[] list) {
  360. checkModify();
  361. }
  362. /**
  363. * Adds an interface.
  364. *
  365. * @param anInterface the added interface.
  366. */
  367. public void addInterface(CtClass anInterface) {
  368. checkModify();
  369. }
  370. /**
  371. * Returns an array containing <code>CtField</code> objects
  372. * representing all the public fields of the class.
  373. * That array includes public fields inherited from the
  374. * superclasses.
  375. */
  376. public CtField[] getFields() { return new CtField[0]; }
  377. /**
  378. * Returns the field with the specified name. The returned field
  379. * may be a private field declared in a super class or interface.
  380. */
  381. public CtField getField(String name) throws NotFoundException {
  382. throw new NotFoundException(name);
  383. }
  384. /**
  385. * Gets all the fields declared in the class. The inherited fields
  386. * are not included.
  387. *
  388. * <p>Note: the result does not include inherited fields.
  389. */
  390. public CtField[] getDeclaredFields() { return new CtField[0]; }
  391. /**
  392. * Retrieves the field with the specified name among the fields
  393. * declared in the class.
  394. *
  395. * <p>Note: this method does not search the superclasses.
  396. */
  397. public CtField getDeclaredField(String name) throws NotFoundException {
  398. throw new NotFoundException(name);
  399. }
  400. /**
  401. * Gets all the constructors and methods declared in the class.
  402. */
  403. public CtBehavior[] getDeclaredBehaviors() {
  404. return new CtBehavior[0];
  405. }
  406. /**
  407. * Returns an array containing <code>CtConstructor</code> objects
  408. * representing all the public constructors of the class.
  409. */
  410. public CtConstructor[] getConstructors() {
  411. return new CtConstructor[0];
  412. }
  413. /**
  414. * Returns the constructor with the given signature,
  415. * which is represented by a character string
  416. * called method descriptor.
  417. * For details of the method descriptor, see the JVM specification
  418. * or <code>javassist.bytecode.Descriptor</code>.
  419. *
  420. * @param name method name
  421. * @param desc method descriptor
  422. * @see javassist.bytecode.Descriptor
  423. */
  424. public CtConstructor getConstructor(String desc)
  425. throws NotFoundException
  426. {
  427. throw new NotFoundException("no such a constructor");
  428. }
  429. /**
  430. * Gets all the constructors declared in the class.
  431. *
  432. * @see javassist.CtConstructor
  433. */
  434. public CtConstructor[] getDeclaredConstructors() {
  435. return new CtConstructor[0];
  436. }
  437. /**
  438. * Returns a constructor receiving the specified parameters.
  439. *
  440. * @param params parameter types.
  441. */
  442. public CtConstructor getDeclaredConstructor(CtClass[] params)
  443. throws NotFoundException
  444. {
  445. String desc = Descriptor.ofConstructor(params);
  446. return getConstructor(desc);
  447. }
  448. /**
  449. * Gets the class initializer (static constructor)
  450. * declared in the class.
  451. * This method returns <code>null</code> if
  452. * no class initializer is not declared.
  453. *
  454. * @see #makeClassInitializer()
  455. * @see javassist.CtConstructor
  456. */
  457. public CtConstructor getClassInitializer() {
  458. return null;
  459. }
  460. /**
  461. * Returns an array containing <code>CtMethod</code> objects
  462. * representing all the public methods of the class.
  463. * That array includes public methods inherited from the
  464. * superclasses.
  465. */
  466. public CtMethod[] getMethods() {
  467. return new CtMethod[0];
  468. }
  469. /**
  470. * Returns the method with the given name and signature.
  471. * The returned method may be declared in a super class.
  472. * The method signature is represented by a character string
  473. * called method descriptor,
  474. * which is defined in the JVM specification.
  475. *
  476. * @param name method name
  477. * @param desc method descriptor
  478. * @see javassist.bytecode.Descriptor
  479. */
  480. public CtMethod getMethod(String name, String desc)
  481. throws NotFoundException
  482. {
  483. throw new NotFoundException(name);
  484. }
  485. /**
  486. * Gets all methods declared in the class. The inherited methods
  487. * are not included.
  488. *
  489. * @see javassist.CtMethod
  490. */
  491. public CtMethod[] getDeclaredMethods() {
  492. return new CtMethod[0];
  493. }
  494. /**
  495. * Retrieves the method with the specified name and parameter types
  496. * among the methods declared in the class.
  497. *
  498. * <p>Note: this method does not search the superclasses.
  499. *
  500. * @param name method name
  501. * @param params parameter types
  502. * @see javassist.CtMethod
  503. */
  504. public CtMethod getDeclaredMethod(String name, CtClass[] params)
  505. throws NotFoundException
  506. {
  507. throw new NotFoundException(name);
  508. }
  509. /**
  510. * Retrieves the method with the specified name among the methods
  511. * declared in the class. If there are multiple methods with
  512. * the specified name, then this method returns one of them.
  513. *
  514. * <p>Note: this method does not search the superclasses.
  515. *
  516. * @see javassist.CtMethod
  517. */
  518. public CtMethod getDeclaredMethod(String name) throws NotFoundException {
  519. throw new NotFoundException(name);
  520. }
  521. /**
  522. * Makes a class initializer (static constructor).
  523. * If the class already includes a class initializer,
  524. * this method returns it.
  525. *
  526. * @see #getClassInitializer()
  527. */
  528. public CtConstructor makeClassInitializer()
  529. throws CannotCompileException
  530. {
  531. throw new CannotCompileException("not a class");
  532. }
  533. /**
  534. * Adds a constructor.
  535. */
  536. public void addConstructor(CtConstructor c)
  537. throws CannotCompileException
  538. {
  539. checkModify();
  540. }
  541. /**
  542. * Adds a method.
  543. */
  544. public void addMethod(CtMethod m) throws CannotCompileException {
  545. checkModify();
  546. }
  547. /**
  548. * Adds a field.
  549. *
  550. * <p>The <code>CtField</code> belonging to another
  551. * <code>CtClass</code> cannot be directly added to this class.
  552. * Only a field created for this class can be added.
  553. *
  554. * @see javassist.CtField#CtField(CtField,CtClass)
  555. */
  556. public void addField(CtField f) throws CannotCompileException {
  557. addField(f, (CtField.Initializer)null);
  558. }
  559. /**
  560. * Adds a field with an initial value.
  561. *
  562. * <p>The <code>CtField</code> belonging to another
  563. * <code>CtClass</code> cannot be directly added to this class.
  564. * Only a field created for this class can be added.
  565. *
  566. * <p>The initial value is given as an expression written in Java.
  567. * Any regular Java expression can be used for specifying the initial
  568. * value. The followings are examples.
  569. *
  570. * <ul><pre>
  571. * cc.addField(f, "0") // the initial value is 0.
  572. * cc.addField(f, "i + 1") // i + 1.
  573. * cc.addField(f, "new Point()"); // a Point object.
  574. * </pre></ul>
  575. *
  576. * <p>Here, the type of variable <code>cc</code> is <code>CtClass</code>.
  577. * The type of <code>f</code> is <code>CtField</code>.
  578. *
  579. * @param init an expression for the initial value.
  580. *
  581. * @see javassist.CtField.Initializer#byExpr(String)
  582. * @see javassist.CtField#CtField(CtField,CtClass)
  583. */
  584. public void addField(CtField f, String init)
  585. throws CannotCompileException
  586. {
  587. checkModify();
  588. }
  589. /**
  590. * Adds a field with an initial value.
  591. *
  592. * <p>The <code>CtField</code> belonging to another
  593. * <code>CtClass</code> cannot be directly added to this class.
  594. * Only a field created for this class can be added.
  595. *
  596. * <p>For example,
  597. *
  598. * <ul><pre>
  599. * CtClass cc = ...;
  600. * addField(new CtField(CtClass.intType, "i", cc),
  601. * CtField.Initializer.constant(1));
  602. * </pre></ul>
  603. *
  604. * <p>This code adds an <code>int</code> field named "i". The
  605. * initial value of this field is 1.
  606. *
  607. * @param init specifies the initial value of the field.
  608. *
  609. * @see javassist.CtField#CtField(CtField,CtClass)
  610. */
  611. public void addField(CtField f, CtField.Initializer init)
  612. throws CannotCompileException
  613. {
  614. checkModify();
  615. }
  616. /**
  617. * Obtains an attribute with the given name.
  618. * If that attribute is not found in the class file, this
  619. * method returns null.
  620. *
  621. * @param name attribute name
  622. */
  623. public byte[] getAttribute(String name) {
  624. return null;
  625. }
  626. /**
  627. * Adds a named attribute.
  628. * An arbitrary data (smaller than 64Kb) can be saved in the class
  629. * file. Some attribute name are reserved by the JVM.
  630. * The attributes with the non-reserved names are ignored when a
  631. * class file is loaded into the JVM.
  632. * If there is already an attribute with
  633. * the same name, this method substitutes the new one for it.
  634. *
  635. * @param name attribute name
  636. * @param data attribute value
  637. */
  638. public void setAttribute(String name, byte[] data) {
  639. checkModify();
  640. }
  641. /**
  642. * Applies the given converter to all methods and constructors
  643. * declared in the class. This method calls <code>instrument()</code>
  644. * on every <code>CtMethod</code> and <code>CtConstructor</code> object
  645. * in the class.
  646. *
  647. * @param converter specifies how to modify.
  648. */
  649. public void instrument(CodeConverter converter)
  650. throws CannotCompileException
  651. {
  652. checkModify();
  653. }
  654. /**
  655. * Modifies the bodies of all methods and constructors
  656. * declared in the class. This method calls <code>instrument()</code>
  657. * on every <code>CtMethod</code> and <code>CtConstructor</code> object
  658. * in the class.
  659. *
  660. * @param editor specifies how to modify.
  661. */
  662. public void instrument(ExprEditor editor)
  663. throws CannotCompileException
  664. {
  665. checkModify();
  666. }
  667. /**
  668. * Converts this class to a <code>java.lang.Class</code> object.
  669. * Once this method is called, further modifications are not
  670. * possible any more.
  671. *
  672. * <p>This method is equivalent to:
  673. * <ul><pre>this.getClassPool().writeAsClass(this.getName())</pre></ul>
  674. *
  675. * <p>See the description of <code>ClassPool.writeAsClass()</code>
  676. * before you use this method.
  677. * This method is provided for convenience. If you need more
  678. * complex functionality, you should write your own class loader.
  679. *
  680. * @see javassist.ClassPool#writeAsClass(String)
  681. * @see javassist.ClassPool#forName(String)
  682. */
  683. public Class toClass()
  684. throws NotFoundException, IOException, CannotCompileException
  685. {
  686. return getClassPool2().writeAsClass(getName());
  687. }
  688. /**
  689. * Converts this class to a class file.
  690. * Once this method is called, further modifications are not
  691. * possible any more.
  692. *
  693. * <p>This method is equivalent to:
  694. * <ul><pre>this.getClassPool().write(this.getName())</pre></ul>
  695. *
  696. * @see javassist.ClassPool#write(String)
  697. */
  698. public byte[] toBytecode()
  699. throws NotFoundException, IOException, CannotCompileException
  700. {
  701. return getClassPool2().write(getName());
  702. }
  703. /**
  704. * Writes a class file represented by this <code>CtClass</code>
  705. * object in the current directory.
  706. * Once this method is called, further modifications are not
  707. * possible any more.
  708. *
  709. * <p>This method is equivalent to:
  710. * <ul><pre>this.getClassPool().writeFile(this.getName())</pre></ul>
  711. *
  712. * @see javassist.ClassPool#writeFile(String)
  713. */
  714. public void writeFile()
  715. throws NotFoundException, IOException, CannotCompileException
  716. {
  717. getClassPool2().writeFile(getName());
  718. }
  719. private ClassPool getClassPool2() throws CannotCompileException {
  720. ClassPool cp = getClassPool();
  721. if (cp == null)
  722. throw new CannotCompileException(
  723. "no ClassPool found. not a class?");
  724. else
  725. return cp;
  726. }
  727. /**
  728. * Converts this class to a class file.
  729. * Once this method is called, further modifications are not
  730. * possible any more.
  731. *
  732. * <p>If this method is used to obtain a byte array representing
  733. * the class file, <code>Translator.onWrite()</code> is never
  734. * called on this class. <code>ClassPool.write()</code> should
  735. * be used.
  736. *
  737. * <p>This method dose not close the output stream in the end.
  738. *
  739. * @param out the output stream that a class file is written to.
  740. */
  741. void toBytecode(DataOutputStream out)
  742. throws CannotCompileException, IOException
  743. {
  744. throw new CannotCompileException("not a class");
  745. }
  746. }