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

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