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

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