Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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