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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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.20.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-2015 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. * @param sig a generic signature.
  414. * @see javassist.bytecode.SignatureAttribute.ClassSignature#encode()
  415. * @see javassist.bytecode.SignatureAttribute.MethodSignature#encode()
  416. * @see CtMember#setGenericSignature(String)
  417. * @since 3.17
  418. */
  419. public void setGenericSignature(String sig) { checkModify(); }
  420. /**
  421. * Substitutes <code>newName</code> for all occurrences of a class
  422. * name <code>oldName</code> in the class file.
  423. *
  424. * @param oldName replaced class name
  425. * @param newName substituted class name
  426. */
  427. public void replaceClassName(String oldName, String newName) {
  428. checkModify();
  429. }
  430. /**
  431. * Changes class names appearing in the class file according to the
  432. * given <code>map</code>.
  433. *
  434. * <p>All the class names appearing in the class file are tested
  435. * with <code>map</code> to determine whether each class name is
  436. * replaced or not. Thus this method can be used for collecting
  437. * all the class names in the class file. To do that, first define
  438. * a subclass of <code>ClassMap</code> so that <code>get()</code>
  439. * records all the given parameters. Then, make an instance of
  440. * that subclass as an empty hash-table. Finally, pass that instance
  441. * to this method. After this method finishes, that instance would
  442. * contain all the class names appearing in the class file.
  443. *
  444. * @param map the hashtable associating replaced class names
  445. * with substituted names.
  446. */
  447. public void replaceClassName(ClassMap map) {
  448. checkModify();
  449. }
  450. /**
  451. * Returns a collection of the names of all the classes
  452. * referenced in this class.
  453. * That collection includes the name of this class.
  454. *
  455. * <p>This method may return <code>null</code>.
  456. *
  457. * @return a <code>Collection&lt;String&gt;</code> object.
  458. */
  459. public synchronized Collection getRefClasses() {
  460. ClassFile cf = getClassFile2();
  461. if (cf != null) {
  462. ClassMap cm = new ClassMap() {
  463. public void put(String oldname, String newname) {
  464. put0(oldname, newname);
  465. }
  466. public Object get(Object jvmClassName) {
  467. String n = toJavaName((String)jvmClassName);
  468. put0(n, n);
  469. return null;
  470. }
  471. public void fix(String name) {}
  472. };
  473. cf.getRefClasses(cm);
  474. return cm.values();
  475. }
  476. else
  477. return null;
  478. }
  479. /**
  480. * Determines whether this object represents a class or an interface.
  481. * It returns <code>true</code> if this object represents an interface.
  482. */
  483. public boolean isInterface() {
  484. return false;
  485. }
  486. /**
  487. * Determines whether this object represents an annotation type.
  488. * It returns <code>true</code> if this object represents an annotation type.
  489. *
  490. * @since 3.2
  491. */
  492. public boolean isAnnotation() {
  493. return false;
  494. }
  495. /**
  496. * Determines whether this object represents an enum.
  497. * It returns <code>true</code> if this object represents an enum.
  498. *
  499. * @since 3.2
  500. */
  501. public boolean isEnum() {
  502. return false;
  503. }
  504. /**
  505. * Returns the modifiers for this class, encoded in an integer.
  506. * For decoding, use <code>javassist.Modifier</code>.
  507. *
  508. * <p>If the class is a static nested class (a.k.a. static inner class),
  509. * the returned modifiers include <code>Modifier.STATIC</code>.
  510. *
  511. * @see Modifier
  512. */
  513. public int getModifiers() {
  514. return 0;
  515. }
  516. /**
  517. * Returns true if the class has the specified annotation class.
  518. *
  519. * @param clz the annotation class.
  520. * @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
  521. * @since 3.11
  522. */
  523. public boolean hasAnnotation(Class clz) {
  524. return false;
  525. }
  526. /**
  527. * Returns the annotation if the class has the specified annotation class.
  528. * For example, if an annotation <code>@Author</code> is associated
  529. * with this class, an <code>Author</code> object is returned.
  530. * The member values can be obtained by calling methods on
  531. * the <code>Author</code> object.
  532. *
  533. * @param clz the annotation class.
  534. * @return the annotation if found, otherwise <code>null</code>.
  535. * @since 3.11
  536. */
  537. public Object getAnnotation(Class clz) throws ClassNotFoundException {
  538. return null;
  539. }
  540. /**
  541. * Returns the annotations associated with this class.
  542. * For example, if an annotation <code>@Author</code> is associated
  543. * with this class, the returned array contains an <code>Author</code>
  544. * object. The member values can be obtained by calling methods on
  545. * the <code>Author</code> object.
  546. *
  547. * @return an array of annotation-type objects.
  548. * @see CtMember#getAnnotations()
  549. * @since 3.1
  550. */
  551. public Object[] getAnnotations() throws ClassNotFoundException {
  552. return new Object[0];
  553. }
  554. /**
  555. * Returns the annotations associated with this class.
  556. * This method is equivalent to <code>getAnnotations()</code>
  557. * except that, if any annotations are not on the classpath,
  558. * they are not included in the returned array.
  559. *
  560. * @return an array of annotation-type objects.
  561. * @see #getAnnotations()
  562. * @see CtMember#getAvailableAnnotations()
  563. * @since 3.3
  564. */
  565. public Object[] getAvailableAnnotations(){
  566. return new Object[0];
  567. }
  568. /**
  569. * Returns an array of nested classes declared in the class.
  570. * Nested classes are inner classes, anonymous classes, local classes,
  571. * and static nested classes. This simply calls <code>getNestedClasses()</code>.
  572. *
  573. * @see #getNestedClasses()
  574. * @since 3.15
  575. */
  576. public CtClass[] getDeclaredClasses() throws NotFoundException {
  577. return getNestedClasses();
  578. }
  579. /**
  580. * Returns an array of nested classes declared in the class.
  581. * Nested classes are inner classes, anonymous classes, local classes,
  582. * and static nested classes.
  583. *
  584. * @since 3.2
  585. */
  586. public CtClass[] getNestedClasses() throws NotFoundException {
  587. return new CtClass[0];
  588. }
  589. /**
  590. * Sets the modifiers.
  591. *
  592. * <p>If the class is a nested class, this method also modifies
  593. * the class declaring that nested class (i.e. the enclosing
  594. * class is modified).
  595. *
  596. * @param mod modifiers encoded by
  597. * <code>javassist.Modifier</code>
  598. * @see Modifier
  599. */
  600. public void setModifiers(int mod) {
  601. checkModify();
  602. }
  603. /**
  604. * Determines whether the class directly or indirectly extends
  605. * the given class. If this class extends a class A and
  606. * the class A extends a class B, then subclassof(B) returns true.
  607. *
  608. * <p>This method returns true if the given class is identical to
  609. * the class represented by this object.
  610. */
  611. public boolean subclassOf(CtClass superclass) {
  612. return false;
  613. }
  614. /**
  615. * Obtains the class object representing the superclass of the
  616. * class.
  617. * It returns null if this object represents the
  618. * <code>java.lang.Object</code> class and thus it does not have
  619. * the super class.
  620. *
  621. * <p>If this object represents an interface, this method
  622. * always returns the <code>java.lang.Object</code> class.
  623. * To obtain the super interfaces
  624. * extended by that interface, call <code>getInterfaces()</code>.
  625. */
  626. public CtClass getSuperclass() throws NotFoundException {
  627. return null;
  628. }
  629. /**
  630. * Changes a super class unless this object represents an interface.
  631. * The new super class must be compatible with the old one; for example,
  632. * it should inherit from the old super class.
  633. *
  634. * <p>If this object represents an interface, this method is equivalent
  635. * to <code>addInterface()</code>; it appends <code>clazz</code> to
  636. * the list of the super interfaces extended by that interface.
  637. * Note that an interface can extend multiple super interfaces.
  638. *
  639. * @see #replaceClassName(String, String)
  640. * @see #replaceClassName(ClassMap)
  641. */
  642. public void setSuperclass(CtClass clazz) throws CannotCompileException {
  643. checkModify();
  644. }
  645. /**
  646. * Obtains the class objects representing the interfaces implemented
  647. * by the class or, if this object represents an interface, the interfaces
  648. * extended by that interface.
  649. */
  650. public CtClass[] getInterfaces() throws NotFoundException {
  651. return new CtClass[0];
  652. }
  653. /**
  654. * Sets implemented interfaces. If this object represents an interface,
  655. * this method sets the interfaces extended by that interface.
  656. *
  657. * @param list a list of the <code>CtClass</code> objects
  658. * representing interfaces, or
  659. * <code>null</code> if the class implements
  660. * no interfaces.
  661. */
  662. public void setInterfaces(CtClass[] list) {
  663. checkModify();
  664. }
  665. /**
  666. * Adds an interface.
  667. *
  668. * @param anInterface the added interface.
  669. */
  670. public void addInterface(CtClass anInterface) {
  671. checkModify();
  672. }
  673. /**
  674. * If this class is a member class or interface of another class,
  675. * then the class enclosing this class is returned.
  676. *
  677. * @return null if this class is a top-level class or an anonymous class.
  678. */
  679. public CtClass getDeclaringClass() throws NotFoundException {
  680. return null;
  681. }
  682. /**
  683. * Returns the immediately enclosing method of this class.
  684. * This method works only with JDK 1.5 or later.
  685. *
  686. * @return null if this class is not a local class or an anonymous
  687. * class.
  688. * @deprecated The enclosing method might be a constructor.
  689. * Use {@link #getEnclosingBehavior()}.
  690. * @see #getEnclosingBehavior()
  691. */
  692. public final CtMethod getEnclosingMethod() throws NotFoundException {
  693. CtBehavior b = getEnclosingBehavior();
  694. if (b == null)
  695. return null;
  696. else if (b instanceof CtMethod)
  697. return (CtMethod)b;
  698. else
  699. throw new NotFoundException(b.getLongName() + " is enclosing " + getName());
  700. }
  701. /**
  702. * Returns the immediately enclosing method of this class.
  703. * It might be not a method but a constructor.
  704. * This method works only with JDK 1.5 or later.
  705. *
  706. * @return null if this class is not a local class or an anonymous
  707. * class.
  708. */
  709. public CtBehavior getEnclosingBehavior() throws NotFoundException {
  710. return null;
  711. }
  712. /**
  713. * Makes a new public nested class. If this method is called,
  714. * the <code>CtClass</code>, which encloses the nested class, is modified
  715. * since a class file includes a list of nested classes.
  716. *
  717. * <p>The current implementation only supports a static nested class.
  718. * <code>isStatic</code> must be true.
  719. *
  720. * @param name the simple name of the nested class.
  721. * @param isStatic true if the nested class is static.
  722. */
  723. public CtClass makeNestedClass(String name, boolean isStatic) {
  724. throw new RuntimeException(getName() + " is not a class");
  725. }
  726. /**
  727. * Returns an array containing <code>CtField</code> objects
  728. * representing all the non-private fields of the class.
  729. * That array includes non-private fields inherited from the
  730. * superclasses.
  731. */
  732. public CtField[] getFields() { return new CtField[0]; }
  733. /**
  734. * Returns the field with the specified name. The returned field
  735. * may be a private field declared in a super class or interface.
  736. */
  737. public CtField getField(String name) throws NotFoundException {
  738. return getField(name, null);
  739. }
  740. /**
  741. * Returns the field with the specified name and type. The returned field
  742. * may be a private field declared in a super class or interface.
  743. * Unlike Java, the JVM allows a class to have
  744. * multiple fields with the same name but different types.
  745. *
  746. * @param name the field name.
  747. * @param desc the type descriptor of the field. It is available by
  748. * {@link CtField#getSignature()}.
  749. * @see CtField#getSignature()
  750. */
  751. public CtField getField(String name, String desc) throws NotFoundException {
  752. throw new NotFoundException(name);
  753. }
  754. /**
  755. * @return null if the specified field is not found.
  756. */
  757. CtField getField2(String name, String desc) { return null; }
  758. /**
  759. * Gets all the fields declared in the class. The inherited fields
  760. * are not included.
  761. *
  762. * <p>Note: the result does not include inherited fields.
  763. */
  764. public CtField[] getDeclaredFields() { return new CtField[0]; }
  765. /**
  766. * Retrieves the field with the specified name among the fields
  767. * declared in the class.
  768. *
  769. * <p>Note: this method does not search the super classes.
  770. */
  771. public CtField getDeclaredField(String name) throws NotFoundException {
  772. throw new NotFoundException(name);
  773. }
  774. /**
  775. * Retrieves the field with the specified name and type among the fields
  776. * declared in the class. Unlike Java, the JVM allows a class to have
  777. * multiple fields with the same name but different types.
  778. *
  779. * <p>Note: this method does not search the super classes.
  780. *
  781. * @param name the field name.
  782. * @param desc the type descriptor of the field. It is available by
  783. * {@link CtField#getSignature()}.
  784. * @see CtField#getSignature()
  785. */
  786. public CtField getDeclaredField(String name, String desc) throws NotFoundException {
  787. throw new NotFoundException(name);
  788. }
  789. /**
  790. * Gets all the constructors and methods declared in the class.
  791. */
  792. public CtBehavior[] getDeclaredBehaviors() {
  793. return new CtBehavior[0];
  794. }
  795. /**
  796. * Returns an array containing <code>CtConstructor</code> objects
  797. * representing all the non-private constructors of the class.
  798. */
  799. public CtConstructor[] getConstructors() {
  800. return new CtConstructor[0];
  801. }
  802. /**
  803. * Returns the constructor with the given signature,
  804. * which is represented by a character string
  805. * called method descriptor.
  806. * For details of the method descriptor, see the JVM specification
  807. * or <code>javassist.bytecode.Descriptor</code>.
  808. *
  809. * @param desc method descriptor
  810. * @see javassist.bytecode.Descriptor
  811. */
  812. public CtConstructor getConstructor(String desc)
  813. throws NotFoundException
  814. {
  815. throw new NotFoundException("no such constructor");
  816. }
  817. /**
  818. * Gets all the constructors declared in the class.
  819. *
  820. * @see javassist.CtConstructor
  821. */
  822. public CtConstructor[] getDeclaredConstructors() {
  823. return new CtConstructor[0];
  824. }
  825. /**
  826. * Returns a constructor receiving the specified parameters.
  827. *
  828. * @param params parameter types.
  829. */
  830. public CtConstructor getDeclaredConstructor(CtClass[] params)
  831. throws NotFoundException
  832. {
  833. String desc = Descriptor.ofConstructor(params);
  834. return getConstructor(desc);
  835. }
  836. /**
  837. * Gets the class initializer (static constructor)
  838. * declared in the class.
  839. * This method returns <code>null</code> if
  840. * no class initializer is not declared.
  841. *
  842. * @see #makeClassInitializer()
  843. * @see javassist.CtConstructor
  844. */
  845. public CtConstructor getClassInitializer() {
  846. return null;
  847. }
  848. /**
  849. * Returns an array containing <code>CtMethod</code> objects
  850. * representing all the non-private methods of the class.
  851. * That array includes non-private methods inherited from the
  852. * superclasses.
  853. */
  854. public CtMethod[] getMethods() {
  855. return new CtMethod[0];
  856. }
  857. /**
  858. * Returns the method with the given name and signature.
  859. * The returned method may be declared in a super class.
  860. * The method signature is represented by a character string
  861. * called method descriptor,
  862. * which is defined in the JVM specification.
  863. *
  864. * @param name method name
  865. * @param desc method descriptor
  866. * @see CtBehavior#getSignature()
  867. * @see javassist.bytecode.Descriptor
  868. */
  869. public CtMethod getMethod(String name, String desc)
  870. throws NotFoundException
  871. {
  872. throw new NotFoundException(name);
  873. }
  874. /**
  875. * Gets all methods declared in the class. The inherited methods
  876. * are not included.
  877. *
  878. * @see javassist.CtMethod
  879. */
  880. public CtMethod[] getDeclaredMethods() {
  881. return new CtMethod[0];
  882. }
  883. /**
  884. * Retrieves the method with the specified name and parameter types
  885. * among the methods declared in the class.
  886. *
  887. * <p>Note: this method does not search the superclasses.
  888. *
  889. * @param name method name
  890. * @param params parameter types
  891. * @see javassist.CtMethod
  892. */
  893. public CtMethod getDeclaredMethod(String name, CtClass[] params)
  894. throws NotFoundException
  895. {
  896. throw new NotFoundException(name);
  897. }
  898. /**
  899. * Retrieves methods with the specified name among the methods
  900. * declared in the class. Multiple methods with different parameters
  901. * may be returned.
  902. *
  903. * <p>Note: this method does not search the superclasses.</p>
  904. *
  905. * @param name method name.
  906. * @since 3.19
  907. */
  908. public CtMethod[] getDeclaredMethods(String name) throws NotFoundException {
  909. throw new NotFoundException(name);
  910. }
  911. /**
  912. * Retrieves the method with the specified name among the methods
  913. * declared in the class. If there are multiple methods with
  914. * the specified name, then this method returns one of them.
  915. *
  916. * <p>Note: this method does not search the superclasses.
  917. *
  918. * @see javassist.CtMethod
  919. */
  920. public CtMethod getDeclaredMethod(String name) throws NotFoundException {
  921. throw new NotFoundException(name);
  922. }
  923. /**
  924. * Makes an empty class initializer (static constructor).
  925. * If the class already includes a class initializer,
  926. * this method returns it.
  927. *
  928. * @see #getClassInitializer()
  929. */
  930. public CtConstructor makeClassInitializer()
  931. throws CannotCompileException
  932. {
  933. throw new CannotCompileException("not a class");
  934. }
  935. /**
  936. * Adds a constructor. To add a class initializer (static constructor),
  937. * call <code>makeClassInitializer()</code>.
  938. *
  939. * @see #makeClassInitializer()
  940. */
  941. public void addConstructor(CtConstructor c)
  942. throws CannotCompileException
  943. {
  944. checkModify();
  945. }
  946. /**
  947. * Removes a constructor declared in this class.
  948. *
  949. * @param c removed constructor.
  950. * @throws NotFoundException if the constructor is not found.
  951. */
  952. public void removeConstructor(CtConstructor c) throws NotFoundException {
  953. checkModify();
  954. }
  955. /**
  956. * Adds a method.
  957. */
  958. public void addMethod(CtMethod m) throws CannotCompileException {
  959. checkModify();
  960. }
  961. /**
  962. * Removes a method declared in this class.
  963. *
  964. * @param m removed method.
  965. * @throws NotFoundException if the method is not found.
  966. */
  967. public void removeMethod(CtMethod m) throws NotFoundException {
  968. checkModify();
  969. }
  970. /**
  971. * Adds a field.
  972. *
  973. * <p>The <code>CtField</code> belonging to another
  974. * <code>CtClass</code> cannot be directly added to this class.
  975. * Only a field created for this class can be added.
  976. *
  977. * @see javassist.CtField#CtField(CtField,CtClass)
  978. */
  979. public void addField(CtField f) throws CannotCompileException {
  980. addField(f, (CtField.Initializer)null);
  981. }
  982. /**
  983. * Adds a field with an initial value.
  984. *
  985. * <p>The <code>CtField</code> belonging to another
  986. * <code>CtClass</code> cannot be directly added to this class.
  987. * Only a field created for this class can be added.
  988. *
  989. * <p>The initial value is given as an expression written in Java.
  990. * Any regular Java expression can be used for specifying the initial
  991. * value. The followings are examples.
  992. *
  993. * <pre>
  994. * cc.addField(f, "0") // the initial value is 0.
  995. * cc.addField(f, "i + 1") // i + 1.
  996. * cc.addField(f, "new Point()"); // a Point object.
  997. * </pre>
  998. *
  999. * <p>Here, the type of variable <code>cc</code> is <code>CtClass</code>.
  1000. * The type of <code>f</code> is <code>CtField</code>.
  1001. *
  1002. * <p>Note: do not change the modifier of the field
  1003. * (in particular, do not add or remove <code>static</code>
  1004. * to/from the modifier)
  1005. * after it is added to the class by <code>addField()</code>.
  1006. *
  1007. * @param init an expression for the initial value.
  1008. *
  1009. * @see javassist.CtField.Initializer#byExpr(String)
  1010. * @see javassist.CtField#CtField(CtField,CtClass)
  1011. */
  1012. public void addField(CtField f, String init)
  1013. throws CannotCompileException
  1014. {
  1015. checkModify();
  1016. }
  1017. /**
  1018. * Adds a field with an initial value.
  1019. *
  1020. * <p>The <code>CtField</code> belonging to another
  1021. * <code>CtClass</code> cannot be directly added to this class.
  1022. * Only a field created for this class can be added.
  1023. *
  1024. * <p>For example,
  1025. *
  1026. * <pre>
  1027. * CtClass cc = ...;
  1028. * addField(new CtField(CtClass.intType, "i", cc),
  1029. * CtField.Initializer.constant(1));
  1030. * </pre>
  1031. *
  1032. * <p>This code adds an <code>int</code> field named "i". The
  1033. * initial value of this field is 1.
  1034. *
  1035. * @param init specifies the initial value of the field.
  1036. *
  1037. * @see javassist.CtField#CtField(CtField,CtClass)
  1038. */
  1039. public void addField(CtField f, CtField.Initializer init)
  1040. throws CannotCompileException
  1041. {
  1042. checkModify();
  1043. }
  1044. /**
  1045. * Removes a field declared in this class.
  1046. *
  1047. * @param f removed field.
  1048. * @throws NotFoundException if the field is not found.
  1049. */
  1050. public void removeField(CtField f) throws NotFoundException {
  1051. checkModify();
  1052. }
  1053. /**
  1054. * Obtains an attribute with the given name.
  1055. * If that attribute is not found in the class file, this
  1056. * method returns null.
  1057. *
  1058. * <p>This is a convenient method mainly for obtaining
  1059. * a user-defined attribute. For dealing with attributes, see the
  1060. * <code>javassist.bytecode</code> package. For example, the following
  1061. * expression returns all the attributes of a class file.
  1062. *
  1063. * <pre>
  1064. * getClassFile().getAttributes()
  1065. * </pre>
  1066. *
  1067. * @param name attribute name
  1068. * @see javassist.bytecode.AttributeInfo
  1069. */
  1070. public byte[] getAttribute(String name) {
  1071. return null;
  1072. }
  1073. /**
  1074. * Adds a named attribute.
  1075. * An arbitrary data (smaller than 64Kb) can be saved in the class
  1076. * file. Some attribute name are reserved by the JVM.
  1077. * The attributes with the non-reserved names are ignored when a
  1078. * class file is loaded into the JVM.
  1079. * If there is already an attribute with
  1080. * the same name, this method substitutes the new one for it.
  1081. *
  1082. * <p>This is a convenient method mainly for adding
  1083. * a user-defined attribute. For dealing with attributes, see the
  1084. * <code>javassist.bytecode</code> package. For example, the following
  1085. * expression adds an attribute <code>info</code> to a class file.
  1086. *
  1087. * <pre>
  1088. * getClassFile().addAttribute(info)
  1089. * </pre>
  1090. *
  1091. * @param name attribute name
  1092. * @param data attribute value
  1093. * @see javassist.bytecode.AttributeInfo
  1094. */
  1095. public void setAttribute(String name, byte[] data) {
  1096. checkModify();
  1097. }
  1098. /**
  1099. * Applies the given converter to all methods and constructors
  1100. * declared in the class. This method calls <code>instrument()</code>
  1101. * on every <code>CtMethod</code> and <code>CtConstructor</code> object
  1102. * in the class.
  1103. *
  1104. * @param converter specifies how to modify.
  1105. */
  1106. public void instrument(CodeConverter converter)
  1107. throws CannotCompileException
  1108. {
  1109. checkModify();
  1110. }
  1111. /**
  1112. * Modifies the bodies of all methods and constructors
  1113. * declared in the class. This method calls <code>instrument()</code>
  1114. * on every <code>CtMethod</code> and <code>CtConstructor</code> object
  1115. * in the class.
  1116. *
  1117. * @param editor specifies how to modify.
  1118. */
  1119. public void instrument(ExprEditor editor)
  1120. throws CannotCompileException
  1121. {
  1122. checkModify();
  1123. }
  1124. /**
  1125. * Converts this class to a <code>java.lang.Class</code> object.
  1126. * Once this method is called, further modifications are not
  1127. * allowed any more.
  1128. * To load the class, this method uses the context class loader
  1129. * of the current thread. If the program is running on some application
  1130. * server, the context class loader might be inappropriate to load the
  1131. * class.
  1132. *
  1133. * <p>This method is provided for convenience. If you need more
  1134. * complex functionality, you should write your own class loader.
  1135. *
  1136. * <p>Note: this method calls <code>toClass()</code>
  1137. * in <code>ClassPool</code>.
  1138. *
  1139. * <p><b>Warining:</b> A Class object returned by this method may not
  1140. * work with a security manager or a signed jar file because a
  1141. * protection domain is not specified.
  1142. *
  1143. * @see #toClass(java.lang.ClassLoader,ProtectionDomain)
  1144. * @see ClassPool#toClass(CtClass)
  1145. */
  1146. public Class toClass() throws CannotCompileException {
  1147. return getClassPool().toClass(this);
  1148. }
  1149. /**
  1150. * Converts this class to a <code>java.lang.Class</code> object.
  1151. * Once this method is called, further modifications are not allowed
  1152. * any more.
  1153. *
  1154. * <p>The class file represented by this <code>CtClass</code> is
  1155. * loaded by the given class loader to construct a
  1156. * <code>java.lang.Class</code> object. Since a private method
  1157. * on the class loader is invoked through the reflection API,
  1158. * the caller must have permissions to do that.
  1159. *
  1160. * <p>An easy way to obtain <code>ProtectionDomain</code> object is
  1161. * to call <code>getProtectionDomain()</code>
  1162. * in <code>java.lang.Class</code>. It returns the domain that
  1163. * the class belongs to.
  1164. *
  1165. * <p>This method is provided for convenience. If you need more
  1166. * complex functionality, you should write your own class loader.
  1167. *
  1168. * <p>Note: this method calls <code>toClass()</code>
  1169. * in <code>ClassPool</code>.
  1170. *
  1171. * @param loader the class loader used to load this class.
  1172. * If it is null, the class loader returned by
  1173. * {@link ClassPool#getClassLoader()} is used.
  1174. * @param domain the protection domain that the class belongs to.
  1175. * If it is null, the default domain created
  1176. * by <code>java.lang.ClassLoader</code> is used.
  1177. * @see ClassPool#toClass(CtClass,java.lang.ClassLoader)
  1178. * @since 3.3
  1179. */
  1180. public Class toClass(ClassLoader loader, ProtectionDomain domain)
  1181. throws CannotCompileException
  1182. {
  1183. ClassPool cp = getClassPool();
  1184. if (loader == null)
  1185. loader = cp.getClassLoader();
  1186. return cp.toClass(this, loader, domain);
  1187. }
  1188. /**
  1189. * Converts this class to a <code>java.lang.Class</code> object.
  1190. *
  1191. * <p><b>Warining:</b> A Class object returned by this method may not
  1192. * work with a security manager or a signed jar file because a
  1193. * protection domain is not specified.
  1194. *
  1195. * @deprecated Replaced by {@link #toClass(ClassLoader,ProtectionDomain)}
  1196. */
  1197. public final Class toClass(ClassLoader loader)
  1198. throws CannotCompileException
  1199. {
  1200. return getClassPool().toClass(this, loader);
  1201. }
  1202. /**
  1203. * Removes this <code>CtClass</code> object from the
  1204. * <code>ClassPool</code>.
  1205. * After this method is called, any method cannot be called on the
  1206. * removed <code>CtClass</code> object.
  1207. *
  1208. * <p>If <code>get()</code> in <code>ClassPool</code> is called
  1209. * with the name of the removed method,
  1210. * the <code>ClassPool</code> will read the class file again
  1211. * and constructs another <code>CtClass</code> object representing
  1212. * the same class.
  1213. */
  1214. public void detach() {
  1215. ClassPool cp = getClassPool();
  1216. CtClass obj = cp.removeCached(getName());
  1217. if (obj != this)
  1218. cp.cacheCtClass(getName(), obj, false);
  1219. }
  1220. /**
  1221. * Disallows (or allows) automatically pruning this <code>CtClass</code>
  1222. * object.
  1223. *
  1224. * <p>
  1225. * Javassist can automatically prune a <code>CtClass</code> object
  1226. * when <code>toBytecode()</code> (or <code>toClass()</code>,
  1227. * <code>writeFile()</code>) is called.
  1228. * Since a <code>ClassPool</code> holds all instances of <code>CtClass</code>
  1229. * even after <code>toBytecode()</code> (or <code>toClass()</code>,
  1230. * <code>writeFile()</code>) is called, pruning may significantly
  1231. * save memory consumption.
  1232. *
  1233. * <p>If <code>ClassPool.doPruning</code> is true, the automatic pruning
  1234. * is on by default. Otherwise, it is off. The default value of
  1235. * <code>ClassPool.doPruning</code> is false.
  1236. *
  1237. * @param stop disallow pruning if true. Otherwise, allow.
  1238. * @return the previous status of pruning. true if pruning is already stopped.
  1239. *
  1240. * @see #detach()
  1241. * @see #prune()
  1242. * @see ClassPool#doPruning
  1243. */
  1244. public boolean stopPruning(boolean stop) { return true; }
  1245. /**
  1246. * Discards unnecessary attributes, in particular,
  1247. * <code>CodeAttribute</code>s (method bodies) of the class,
  1248. * to minimize the memory footprint.
  1249. * After calling this method, the class is read only.
  1250. * It cannot be modified any more.
  1251. * Furthermore, <code>toBytecode()</code>,
  1252. * <code>writeFile()</code>, <code>toClass()</code>,
  1253. * or <code>instrument()</code> cannot be called.
  1254. * However, the method names and signatures in the class etc.
  1255. * are still accessible.
  1256. *
  1257. * <p><code>toBytecode()</code>, <code>writeFile()</code>, and
  1258. * <code>toClass()</code> internally call this method if
  1259. * automatic pruning is on.
  1260. *
  1261. * <p>According to some experiments, pruning does not really reduce
  1262. * memory consumption. Only about 20%. Since pruning takes time,
  1263. * it might not pay off. So the automatic pruning is off by default.
  1264. *
  1265. * @see #stopPruning(boolean)
  1266. * @see #detach()
  1267. * @see ClassPool#doPruning
  1268. *
  1269. * @see #toBytecode()
  1270. * @see #toClass()
  1271. * @see #writeFile()
  1272. * @see #instrument(CodeConverter)
  1273. * @see #instrument(ExprEditor)
  1274. */
  1275. public void prune() {}
  1276. /* Called by get() in ClassPool.
  1277. * CtClassType overrides this method.
  1278. */
  1279. void incGetCounter() {}
  1280. /**
  1281. * If this method is called, the class file will be
  1282. * rebuilt when it is finally generated by
  1283. * <code>toBytecode()</code> and <code>writeFile()</code>.
  1284. * For a performance reason, the symbol table of the
  1285. * class file may contain unused entries, for example,
  1286. * after a method or a filed is deleted.
  1287. * This method
  1288. * removes those unused entries. This removal will
  1289. * minimize the size of the class file.
  1290. *
  1291. * @since 3.8.1
  1292. */
  1293. public void rebuildClassFile() {}
  1294. /**
  1295. * Converts this class to a class file.
  1296. * Once this method is called, further modifications are not
  1297. * possible any more.
  1298. *
  1299. * @return the contents of the class file.
  1300. */
  1301. public byte[] toBytecode() throws IOException, CannotCompileException {
  1302. ByteArrayOutputStream barray = new ByteArrayOutputStream();
  1303. DataOutputStream out = new DataOutputStream(barray);
  1304. try {
  1305. toBytecode(out);
  1306. }
  1307. finally {
  1308. out.close();
  1309. }
  1310. return barray.toByteArray();
  1311. }
  1312. /**
  1313. * Writes a class file represented by this <code>CtClass</code>
  1314. * object in the current directory.
  1315. * Once this method is called, further modifications are not
  1316. * possible any more.
  1317. *
  1318. * @see #debugWriteFile()
  1319. */
  1320. public void writeFile()
  1321. throws NotFoundException, IOException, CannotCompileException
  1322. {
  1323. writeFile(".");
  1324. }
  1325. /**
  1326. * Writes a class file represented by this <code>CtClass</code>
  1327. * object on a local disk.
  1328. * Once this method is called, further modifications are not
  1329. * possible any more.
  1330. *
  1331. * @param directoryName it must end without a directory separator.
  1332. * @see #debugWriteFile(String)
  1333. */
  1334. public void writeFile(String directoryName)
  1335. throws CannotCompileException, IOException
  1336. {
  1337. DataOutputStream out = makeFileOutput(directoryName);
  1338. try {
  1339. toBytecode(out);
  1340. }
  1341. finally {
  1342. out.close();
  1343. }
  1344. }
  1345. protected DataOutputStream makeFileOutput(String directoryName) {
  1346. String classname = getName();
  1347. String filename = directoryName + File.separatorChar
  1348. + classname.replace('.', File.separatorChar) + ".class";
  1349. int pos = filename.lastIndexOf(File.separatorChar);
  1350. if (pos > 0) {
  1351. String dir = filename.substring(0, pos);
  1352. if (!dir.equals("."))
  1353. new File(dir).mkdirs();
  1354. }
  1355. return new DataOutputStream(new BufferedOutputStream(
  1356. new DelayedFileOutputStream(filename)));
  1357. }
  1358. /**
  1359. * Writes a class file as <code>writeFile()</code> does although this
  1360. * method does not prune or freeze the class after writing the class
  1361. * file. Note that, once <code>writeFile()</code> or <code>toBytecode()</code>
  1362. * is called, it cannot be called again since the class is pruned and frozen.
  1363. * This method would be useful for debugging.
  1364. */
  1365. public void debugWriteFile() {
  1366. debugWriteFile(".");
  1367. }
  1368. /**
  1369. * Writes a class file as <code>writeFile()</code> does although this
  1370. * method does not prune or freeze the class after writing the class
  1371. * file. Note that, once <code>writeFile()</code> or <code>toBytecode()</code>
  1372. * is called, it cannot be called again since the class is pruned and frozen.
  1373. * This method would be useful for debugging.
  1374. *
  1375. * @param directoryName it must end without a directory separator.
  1376. */
  1377. public void debugWriteFile(String directoryName) {
  1378. try {
  1379. boolean p = stopPruning(true);
  1380. writeFile(directoryName);
  1381. defrost();
  1382. stopPruning(p);
  1383. }
  1384. catch (Exception e) {
  1385. throw new RuntimeException(e);
  1386. }
  1387. }
  1388. static class DelayedFileOutputStream extends OutputStream {
  1389. private FileOutputStream file;
  1390. private String filename;
  1391. DelayedFileOutputStream(String name) {
  1392. file = null;
  1393. filename = name;
  1394. }
  1395. private void init() throws IOException {
  1396. if (file == null)
  1397. file = new FileOutputStream(filename);
  1398. }
  1399. public void write(int b) throws IOException {
  1400. init();
  1401. file.write(b);
  1402. }
  1403. public void write(byte[] b) throws IOException {
  1404. init();
  1405. file.write(b);
  1406. }
  1407. public void write(byte[] b, int off, int len) throws IOException {
  1408. init();
  1409. file.write(b, off, len);
  1410. }
  1411. public void flush() throws IOException {
  1412. init();
  1413. file.flush();
  1414. }
  1415. public void close() throws IOException {
  1416. init();
  1417. file.close();
  1418. }
  1419. }
  1420. /**
  1421. * Converts this class to a class file.
  1422. * Once this method is called, further modifications are not
  1423. * possible any more.
  1424. *
  1425. * <p>This method dose not close the output stream in the end.
  1426. *
  1427. * @param out the output stream that a class file is written to.
  1428. */
  1429. public void toBytecode(DataOutputStream out)
  1430. throws CannotCompileException, IOException
  1431. {
  1432. throw new CannotCompileException("not a class");
  1433. }
  1434. /**
  1435. * Makes a unique member name. This method guarantees that
  1436. * the returned name is not used as a prefix of any methods
  1437. * or fields visible in this class.
  1438. * If the returned name is XYZ, then any method or field names
  1439. * in this class do not start with XYZ.
  1440. *
  1441. * @param prefix the prefix of the member name.
  1442. */
  1443. public String makeUniqueName(String prefix) {
  1444. throw new RuntimeException("not available in " + getName());
  1445. }
  1446. /* Invoked from ClassPool#compress().
  1447. * This method is overridden by CtClassType.
  1448. */
  1449. void compress() {}
  1450. }