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

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