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

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