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.

ConstPool.java 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2004 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. import java.io.DataInputStream;
  17. import java.io.DataOutputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.PrintWriter;
  20. import java.io.IOException;
  21. import java.util.Map;
  22. import java.util.HashMap;
  23. import javassist.CtClass;
  24. /**
  25. * Constant pool table.
  26. */
  27. public final class ConstPool {
  28. LongVector items;
  29. int numOfItems;
  30. HashMap classes;
  31. HashMap strings;
  32. int thisClassInfo;
  33. /**
  34. * <code>CONSTANT_Class</code>
  35. */
  36. public static final int CONST_Class = ClassInfo.tag;
  37. /**
  38. * <code>CONSTANT_Fieldref</code>
  39. */
  40. public static final int CONST_Fieldref = FieldrefInfo.tag;
  41. /**
  42. * <code>CONSTANT_Methodref</code>
  43. */
  44. public static final int CONST_Methodref = MethodrefInfo.tag;
  45. /**
  46. * <code>CONSTANT_InterfaceMethodref</code>
  47. */
  48. public static final int CONST_InterfaceMethodref
  49. = InterfaceMethodrefInfo.tag;
  50. /**
  51. * <code>CONSTANT_String</code>
  52. */
  53. public static final int CONST_String = StringInfo.tag;
  54. /**
  55. * <code>CONSTANT_Integer</code>
  56. */
  57. public static final int CONST_Integer = IntegerInfo.tag;
  58. /**
  59. * <code>CONSTANT_Float</code>
  60. */
  61. public static final int CONST_Float = IntegerInfo.tag;
  62. /**
  63. * <code>CONSTANT_Long</code>
  64. */
  65. public static final int CONST_Long = LongInfo.tag;
  66. /**
  67. * <code>CONSTANT_Double</code>
  68. */
  69. public static final int CONST_Double = DoubleInfo.tag;
  70. /**
  71. * <code>CONSTANT_NameAndType</code>
  72. */
  73. public static final int CONST_NameAndType = NameAndTypeInfo.tag;
  74. /**
  75. * <code>CONSTANT_Utf8</code>
  76. */
  77. public static final int CONST_Utf8 = Utf8Info.tag;
  78. /**
  79. * Represents the class using this constant pool table.
  80. */
  81. public static final CtClass THIS = null;
  82. /**
  83. * Constructs a constant pool table.
  84. *
  85. * @param thisclass the name of the class using this constant
  86. * pool table
  87. */
  88. public ConstPool(String thisclass) {
  89. items = new LongVector();
  90. numOfItems = 0;
  91. addItem(null); // index 0 is reserved by the JVM.
  92. classes = new HashMap();
  93. strings = new HashMap();
  94. thisClassInfo = addClassInfo(thisclass);
  95. }
  96. /**
  97. * Constructs a constant pool table from the given byte stream.
  98. *
  99. * @param in byte stream.
  100. */
  101. public ConstPool(DataInputStream in) throws IOException {
  102. classes = new HashMap();
  103. strings = new HashMap();
  104. thisClassInfo = 0;
  105. /* read() initializes items and numOfItems, and do addItem(null).
  106. */
  107. read(in);
  108. }
  109. /**
  110. * Returns the name of the class using this constant pool table.
  111. */
  112. public String getClassName() {
  113. return getClassInfo(thisClassInfo);
  114. }
  115. /**
  116. * Returns the index of <code>CONSTANT_Class_info</code> structure
  117. * specifying the class using this constant pool table.
  118. */
  119. public int getThisClassInfo() {
  120. return thisClassInfo;
  121. }
  122. void setThisClassInfo(int i) {
  123. thisClassInfo = i;
  124. }
  125. ConstInfo getItem(int n) {
  126. return (ConstInfo)items.elementAt(n);
  127. }
  128. /**
  129. * Returns the <code>tag</code> field of the constant pool table
  130. * entry at the given index.
  131. */
  132. public int getTag(int index) {
  133. return getItem(index).getTag();
  134. }
  135. /**
  136. * Reads <code>CONSTANT_Class_info</code> structure
  137. * at the given index.
  138. *
  139. * @return a fully-qualified class or interface name specified
  140. * by <code>name_index</code>.
  141. */
  142. public String getClassInfo(int index) {
  143. ClassInfo c = (ClassInfo)getItem(index);
  144. if (c == null)
  145. return null;
  146. else
  147. return Descriptor.toJavaName(getUtf8Info(c.name));
  148. }
  149. /**
  150. * Reads the <code>name_index</code> field of the
  151. * <code>CONSTANT_NameAndType_info</code> structure
  152. * at the given index.
  153. */
  154. public int getNameAndTypeName(int index) {
  155. NameAndTypeInfo ntinfo = (NameAndTypeInfo)getItem(index);
  156. return ntinfo.memberName;
  157. }
  158. /**
  159. * Reads the <code>descriptor_index</code> field of the
  160. * <code>CONSTANT_NameAndType_info</code> structure
  161. * at the given index.
  162. */
  163. public int getNameAndTypeDescriptor(int index) {
  164. NameAndTypeInfo ntinfo = (NameAndTypeInfo)getItem(index);
  165. return ntinfo.typeDescriptor;
  166. }
  167. /**
  168. * Reads the <code>class_index</code> field of the
  169. * <code>CONSTANT_Fieldref_info</code> structure
  170. * at the given index.
  171. */
  172. public int getFieldrefClass(int index) {
  173. FieldrefInfo finfo = (FieldrefInfo)getItem(index);
  174. return finfo.classIndex;
  175. }
  176. /**
  177. * Reads the <code>class_index</code> field of the
  178. * <code>CONSTANT_Fieldref_info</code> structure
  179. * at the given index.
  180. *
  181. * @return the name of the class at that <code>class_index</code>.
  182. */
  183. public String getFieldrefClassName(int index) {
  184. FieldrefInfo f = (FieldrefInfo)getItem(index);
  185. if (f == null)
  186. return null;
  187. else
  188. return getClassInfo(f.classIndex);
  189. }
  190. /**
  191. * Reads the <code>name_and_type_index</code> field of the
  192. * <code>CONSTANT_Fieldref_info</code> structure
  193. * at the given index.
  194. */
  195. public int getFieldrefNameAndType(int index) {
  196. FieldrefInfo finfo = (FieldrefInfo)getItem(index);
  197. return finfo.nameAndTypeIndex;
  198. }
  199. /**
  200. * Reads the <code>name_index</code> field of the
  201. * <code>CONSTANT_NameAndType_info</code> structure
  202. * indirectly specified by the given index.
  203. *
  204. * @param index an index to a <code>CONSTANT_Fieldref_info</code>.
  205. * @return the name of the field.
  206. */
  207. public String getFieldrefName(int index) {
  208. FieldrefInfo f = (FieldrefInfo)getItem(index);
  209. if (f == null)
  210. return null;
  211. else {
  212. NameAndTypeInfo n = (NameAndTypeInfo)getItem(f.nameAndTypeIndex);
  213. if(n == null)
  214. return null;
  215. else
  216. return getUtf8Info(n.memberName);
  217. }
  218. }
  219. /**
  220. * Reads the <code>descriptor_index</code> field of the
  221. * <code>CONSTANT_NameAndType_info</code> structure
  222. * indirectly specified by the given index.
  223. *
  224. * @param index an index to a <code>CONSTANT_Fieldref_info</code>.
  225. * @return the type descriptor of the field.
  226. */
  227. public String getFieldrefType(int index) {
  228. FieldrefInfo f = (FieldrefInfo)getItem(index);
  229. if (f == null)
  230. return null;
  231. else {
  232. NameAndTypeInfo n = (NameAndTypeInfo) getItem(f.nameAndTypeIndex);
  233. if(n == null)
  234. return null;
  235. else
  236. return getUtf8Info(n.typeDescriptor);
  237. }
  238. }
  239. /**
  240. * Reads the <code>class_index</code> field of the
  241. * <code>CONSTANT_Methodref_info</code> structure
  242. * at the given index.
  243. */
  244. public int getMethodrefClass(int index) {
  245. MethodrefInfo minfo = (MethodrefInfo)getItem(index);
  246. return minfo.classIndex;
  247. }
  248. /**
  249. * Reads the <code>class_index</code> field of the
  250. * <code>CONSTANT_Methodref_info</code> structure
  251. * at the given index.
  252. *
  253. * @return the name of the class at that <code>class_index</code>.
  254. */
  255. public String getMethodrefClassName(int index) {
  256. MethodrefInfo minfo = (MethodrefInfo)getItem(index);
  257. if (minfo == null)
  258. return null;
  259. else
  260. return getClassInfo(minfo.classIndex);
  261. }
  262. /**
  263. * Reads the <code>name_and_type_index</code> field of the
  264. * <code>CONSTANT_Methodref_info</code> structure
  265. * at the given index.
  266. */
  267. public int getMethodrefNameAndType(int index) {
  268. MethodrefInfo minfo = (MethodrefInfo)getItem(index);
  269. return minfo.nameAndTypeIndex;
  270. }
  271. /**
  272. * Reads the <code>name_index</code> field of the
  273. * <code>CONSTANT_NameAndType_info</code> structure
  274. * indirectly specified by the given index.
  275. *
  276. * @param index an index to a <code>CONSTANT_Methodref_info</code>.
  277. * @return the name of the method.
  278. */
  279. public String getMethodrefName(int index) {
  280. MethodrefInfo minfo = (MethodrefInfo)getItem(index);
  281. if (minfo == null)
  282. return null;
  283. else {
  284. NameAndTypeInfo n
  285. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  286. if(n == null)
  287. return null;
  288. else
  289. return getUtf8Info(n.memberName);
  290. }
  291. }
  292. /**
  293. * Reads the <code>descriptor_index</code> field of the
  294. * <code>CONSTANT_NameAndType_info</code> structure
  295. * indirectly specified by the given index.
  296. *
  297. * @param index an index to a <code>CONSTANT_Methodref_info</code>.
  298. * @return the descriptor of the method.
  299. */
  300. public String getMethodrefType(int index) {
  301. MethodrefInfo minfo = (MethodrefInfo)getItem(index);
  302. if (minfo == null)
  303. return null;
  304. else {
  305. NameAndTypeInfo n
  306. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  307. if(n == null)
  308. return null;
  309. else
  310. return getUtf8Info(n.typeDescriptor);
  311. }
  312. }
  313. /**
  314. * Reads the <code>class_index</code> field of the
  315. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  316. * at the given index.
  317. */
  318. public int getInterfaceMethodrefClass(int index) {
  319. InterfaceMethodrefInfo minfo
  320. = (InterfaceMethodrefInfo)getItem(index);
  321. return minfo.classIndex;
  322. }
  323. /**
  324. * Reads the <code>class_index</code> field of the
  325. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  326. * at the given index.
  327. *
  328. * @return the name of the class at that <code>class_index</code>.
  329. */
  330. public String getInterfaceMethodrefClassName(int index) {
  331. InterfaceMethodrefInfo minfo
  332. = (InterfaceMethodrefInfo)getItem(index);
  333. return getClassInfo(minfo.classIndex);
  334. }
  335. /**
  336. * Reads the <code>name_and_type_index</code> field of the
  337. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  338. * at the given index.
  339. */
  340. public int getInterfaceMethodrefNameAndType(int index) {
  341. InterfaceMethodrefInfo minfo
  342. = (InterfaceMethodrefInfo)getItem(index);
  343. return minfo.nameAndTypeIndex;
  344. }
  345. /**
  346. * Reads the <code>name_index</code> field of the
  347. * <code>CONSTANT_NameAndType_info</code> structure
  348. * indirectly specified by the given index.
  349. *
  350. * @param index an index to
  351. * a <code>CONSTANT_InterfaceMethodref_info</code>.
  352. * @return the name of the method.
  353. */
  354. public String getInterfaceMethodrefName(int index) {
  355. InterfaceMethodrefInfo minfo
  356. = (InterfaceMethodrefInfo)getItem(index);
  357. if (minfo == null)
  358. return null;
  359. else {
  360. NameAndTypeInfo n
  361. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  362. if(n == null)
  363. return null;
  364. else
  365. return getUtf8Info(n.memberName);
  366. }
  367. }
  368. /**
  369. * Reads the <code>descriptor_index</code> field of the
  370. * <code>CONSTANT_NameAndType_info</code> structure
  371. * indirectly specified by the given index.
  372. *
  373. * @param index an index to
  374. * a <code>CONSTANT_InterfaceMethodref_info</code>.
  375. * @return the descriptor of the method.
  376. */
  377. public String getInterfaceMethodrefType(int index) {
  378. InterfaceMethodrefInfo minfo
  379. = (InterfaceMethodrefInfo)getItem(index);
  380. if (minfo == null)
  381. return null;
  382. else {
  383. NameAndTypeInfo n
  384. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  385. if(n == null)
  386. return null;
  387. else
  388. return getUtf8Info(n.typeDescriptor);
  389. }
  390. }
  391. /**
  392. * Reads <code>CONSTANT_Integer_info</code>, <code>_Float_info</code>,
  393. * <code>_Long_info</code>, <code>_Double_info</code>, or
  394. * <code>_String_info</code> structure.
  395. * These are used with the LDC instruction.
  396. *
  397. * @return a <code>String</code> value or a wrapped primitive-type
  398. * value.
  399. */
  400. public Object getLdcValue(int index) {
  401. ConstInfo constInfo = this.getItem(index);
  402. Object value = null;
  403. if (constInfo instanceof StringInfo)
  404. value = this.getStringInfo(index);
  405. else if (constInfo instanceof FloatInfo)
  406. value = new Float(getFloatInfo(index));
  407. else if (constInfo instanceof IntegerInfo)
  408. value = new Integer(getIntegerInfo(index));
  409. else if (constInfo instanceof LongInfo)
  410. value = new Long(getLongInfo(index));
  411. else if (constInfo instanceof DoubleInfo)
  412. value = new Double(getDoubleInfo(index));
  413. else
  414. value = null;
  415. return value;
  416. }
  417. /**
  418. * Reads <code>CONSTANT_Integer_info</code> structure
  419. * at the given index.
  420. *
  421. * @return the value specified by this entry.
  422. */
  423. public int getIntegerInfo(int index) {
  424. IntegerInfo i = (IntegerInfo)getItem(index);
  425. return i.value;
  426. }
  427. /**
  428. * Reads <code>CONSTANT_Float_info</code> structure
  429. * at the given index.
  430. *
  431. * @return the value specified by this entry.
  432. */
  433. public float getFloatInfo(int index) {
  434. FloatInfo i = (FloatInfo)getItem(index);
  435. return i.value;
  436. }
  437. /**
  438. * Reads <code>CONSTANT_Long_info</code> structure
  439. * at the given index.
  440. *
  441. * @return the value specified by this entry.
  442. */
  443. public long getLongInfo(int index) {
  444. LongInfo i = (LongInfo)getItem(index);
  445. return i.value;
  446. }
  447. /**
  448. * Reads <code>CONSTANT_Double_info</code> structure
  449. * at the given index.
  450. *
  451. * @return the value specified by this entry.
  452. */
  453. public double getDoubleInfo(int index) {
  454. DoubleInfo i = (DoubleInfo)getItem(index);
  455. return i.value;
  456. }
  457. /**
  458. * Reads <code>CONSTANT_String_info</code> structure
  459. * at the given index.
  460. *
  461. * @return the string specified by <code>string_index</code>.
  462. */
  463. public String getStringInfo(int index) {
  464. StringInfo si = (StringInfo)getItem(index);
  465. return getUtf8Info(si.string);
  466. }
  467. /**
  468. * Reads <code>CONSTANT_utf8_info</code> structure
  469. * at the given index.
  470. *
  471. * @return the string specified by this entry.
  472. */
  473. public String getUtf8Info(int index) {
  474. Utf8Info utf = (Utf8Info)getItem(index);
  475. return utf.string;
  476. }
  477. /**
  478. * Determines whether <code>CONSTANT_Methodref_info</code>
  479. * structure at the given index represents the constructor
  480. * of the given class.
  481. *
  482. * @return the <code>descriptor_index</code> specifying
  483. * the type descriptor of the that constructor.
  484. * If it is not that constructor,
  485. * <code>isConstructor()</code> returns 0.
  486. */
  487. public int isConstructor(String classname, int index) {
  488. return isMember(classname, MethodInfo.nameInit, index);
  489. }
  490. /**
  491. * Determines whether <code>CONSTANT_Methodref_info</code>,
  492. * <code>CONSTANT_Fieldref_info</code>, or
  493. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  494. * at the given index represents the member with the specified
  495. * name and declaring class.
  496. *
  497. * @param classname the class declaring the member
  498. * @param membername the member name
  499. * @param index the index into the constant pool table
  500. *
  501. * @return the <code>descriptor_index</code> specifying
  502. * the type descriptor of that member.
  503. * If it is not that member,
  504. * <code>isMember()</code> returns 0.
  505. */
  506. public int isMember(String classname, String membername, int index) {
  507. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  508. if (getClassInfo(minfo.classIndex).equals(classname)) {
  509. NameAndTypeInfo ntinfo
  510. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  511. if (getUtf8Info(ntinfo.memberName).equals(membername))
  512. return ntinfo.typeDescriptor;
  513. }
  514. return 0; // false
  515. }
  516. private int addItem(ConstInfo info) {
  517. items.addElement(info);
  518. return numOfItems++;
  519. }
  520. /**
  521. * Copies the n-th item in this ConstPool object into the destination
  522. * ConstPool object.
  523. * The class names that the item refers to are renamed according
  524. * to the given map.
  525. *
  526. * @param n the <i>n</i>-th item
  527. * @param dest destination constant pool table
  528. * @param classnames the map or null.
  529. */
  530. public int copy(int n, ConstPool dest, Map classnames) {
  531. if (n == 0)
  532. return 0;
  533. ConstInfo info = getItem(n);
  534. return info.copy(this, dest, classnames);
  535. }
  536. int addConstInfoPadding() {
  537. return addItem(new ConstInfoPadding());
  538. }
  539. /**
  540. * Adds a new <code>CONSTANT_Class_info</code> structure.
  541. *
  542. * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure
  543. * for storing the class name.
  544. *
  545. * @return the index of the added entry.
  546. */
  547. public int addClassInfo(CtClass c) {
  548. if (c == THIS)
  549. return thisClassInfo;
  550. else if (!c.isArray())
  551. return addClassInfo(c.getName());
  552. else {
  553. // an array type is recorded in the hashtable with
  554. // the key "[L<classname>;" instead of "<classname>".
  555. //
  556. // note: toJvmName(toJvmName(c)) is equal to toJvmName(c).
  557. return addClassInfo(Descriptor.toJvmName(c));
  558. }
  559. }
  560. /**
  561. * Adds a new <code>CONSTANT_Class_info</code> structure.
  562. *
  563. * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure
  564. * for storing the class name.
  565. *
  566. * @param qname a fully-qualified class name
  567. * (or the JVM-internal representation of that name).
  568. * @return the index of the added entry.
  569. */
  570. public int addClassInfo(String qname) {
  571. ClassInfo info = (ClassInfo)classes.get(qname);
  572. if (info != null)
  573. return info.index;
  574. else {
  575. int utf8 = addUtf8Info(Descriptor.toJvmName(qname));
  576. info = new ClassInfo(utf8, numOfItems);
  577. classes.put(qname, info);
  578. return addItem(info);
  579. }
  580. }
  581. /**
  582. * Adds a new <code>CONSTANT_NameAndType_info</code> structure.
  583. *
  584. * <p>This also adds <code>CONSTANT_Utf8_info</code> structures.
  585. *
  586. * @param name <code>name_index</code>
  587. * @param type <code>descriptor_index</code>
  588. * @return the index of the added entry.
  589. */
  590. public int addNameAndTypeInfo(String name, String type) {
  591. return addNameAndTypeInfo(addUtf8Info(name), addUtf8Info(type));
  592. }
  593. /**
  594. * Adds a new <code>CONSTANT_NameAndType_info</code> structure.
  595. *
  596. * @param name <code>name_index</code>
  597. * @param type <code>descriptor_index</code>
  598. * @return the index of the added entry.
  599. */
  600. public int addNameAndTypeInfo(int name, int type) {
  601. return addItem(new NameAndTypeInfo(name, type));
  602. }
  603. /**
  604. * Adds a new <code>CONSTANT_Fieldref_info</code> structure.
  605. *
  606. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  607. * structure.
  608. *
  609. * @param classInfo <code>class_index</code>
  610. * @param name <code>name_index</code>
  611. * of <code>CONSTANT_NameAndType_info</code>.
  612. * @param type <code>descriptor_index</code>
  613. * of <code>CONSTANT_NameAndType_info</code>.
  614. * @return the index of the added entry.
  615. */
  616. public int addFieldrefInfo(int classInfo, String name, String type) {
  617. int nt = addNameAndTypeInfo(name, type);
  618. return addFieldrefInfo(classInfo, nt);
  619. }
  620. /**
  621. * Adds a new <code>CONSTANT_Fieldref_info</code> structure.
  622. *
  623. * @param classInfo <code>class_index</code>
  624. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  625. * @return the index of the added entry.
  626. */
  627. public int addFieldrefInfo(int classInfo, int nameAndTypeInfo) {
  628. return addItem(new FieldrefInfo(classInfo, nameAndTypeInfo));
  629. }
  630. /**
  631. * Adds a new <code>CONSTANT_Methodref_info</code> structure.
  632. *
  633. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  634. * structure.
  635. *
  636. * @param classInfo <code>class_index</code>
  637. * @param name <code>name_index</code>
  638. * of <code>CONSTANT_NameAndType_info</code>.
  639. * @param type <code>descriptor_index</code>
  640. * of <code>CONSTANT_NameAndType_info</code>.
  641. * @return the index of the added entry.
  642. */
  643. public int addMethodrefInfo(int classInfo, String name, String type) {
  644. int nt = addNameAndTypeInfo(name, type);
  645. return addMethodrefInfo(classInfo, nt);
  646. }
  647. /**
  648. * Adds a new <code>CONSTANT_Methodref_info</code> structure.
  649. *
  650. * @param classInfo <code>class_index</code>
  651. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  652. * @return the index of the added entry.
  653. */
  654. public int addMethodrefInfo(int classInfo, int nameAndTypeInfo) {
  655. return addItem(new MethodrefInfo(classInfo, nameAndTypeInfo));
  656. }
  657. /**
  658. * Adds a new <code>CONSTANT_InterfaceMethodref_info</code>
  659. * structure.
  660. *
  661. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  662. * structure.
  663. *
  664. * @param classInfo <code>class_index</code>
  665. * @param name <code>name_index</code>
  666. * of <code>CONSTANT_NameAndType_info</code>.
  667. * @param type <code>descriptor_index</code>
  668. * of <code>CONSTANT_NameAndType_info</code>.
  669. * @return the index of the added entry.
  670. */
  671. public int addInterfaceMethodrefInfo(int classInfo, String name,
  672. String type) {
  673. int nt = addNameAndTypeInfo(name, type);
  674. return addInterfaceMethodrefInfo(classInfo, nt);
  675. }
  676. /**
  677. * Adds a new <code>CONSTANT_InterfaceMethodref_info</code>
  678. * structure.
  679. *
  680. * @param classInfo <code>class_index</code>
  681. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  682. * @return the index of the added entry.
  683. */
  684. public int addInterfaceMethodrefInfo(int classInfo,
  685. int nameAndTypeInfo) {
  686. return addItem(new InterfaceMethodrefInfo(classInfo,
  687. nameAndTypeInfo));
  688. }
  689. /**
  690. * Adds a new <code>CONSTANT_String_info</code>
  691. * structure.
  692. *
  693. * <p>This also adds a new <code>CONSTANT_Utf8_info</code>
  694. * structure.
  695. *
  696. * @return the index of the added entry.
  697. */
  698. public int addStringInfo(String str) {
  699. return addItem(new StringInfo(addUtf8Info(str)));
  700. }
  701. /**
  702. * Adds a new <code>CONSTANT_Integer_info</code>
  703. * structure.
  704. *
  705. * @return the index of the added entry.
  706. */
  707. public int addIntegerInfo(int i) {
  708. return addItem(new IntegerInfo(i));
  709. }
  710. /**
  711. * Adds a new <code>CONSTANT_Float_info</code>
  712. * structure.
  713. *
  714. * @return the index of the added entry.
  715. */
  716. public int addFloatInfo(float f) {
  717. return addItem(new FloatInfo(f));
  718. }
  719. /**
  720. * Adds a new <code>CONSTANT_Long_info</code>
  721. * structure.
  722. *
  723. * @return the index of the added entry.
  724. */
  725. public int addLongInfo(long l) {
  726. int i = addItem(new LongInfo(l));
  727. addItem(new ConstInfoPadding());
  728. return i;
  729. }
  730. /**
  731. * Adds a new <code>CONSTANT_Double_info</code>
  732. * structure.
  733. *
  734. * @return the index of the added entry.
  735. */
  736. public int addDoubleInfo(double d) {
  737. int i = addItem(new DoubleInfo(d));
  738. addItem(new ConstInfoPadding());
  739. return i;
  740. }
  741. /**
  742. * Adds a new <code>CONSTANT_Utf8_info</code>
  743. * structure.
  744. *
  745. * <p>If the given utf8 string has been already recorded in the
  746. * table, then this method does not add a new entry to avoid adding
  747. * a duplicated entry.
  748. * Instead, it returns the index of the entry already recorded.
  749. *
  750. * @return the index of the added entry.
  751. */
  752. public int addUtf8Info(String utf8) {
  753. Utf8Info info = (Utf8Info)strings.get(utf8);
  754. if (info != null)
  755. return info.index;
  756. else {
  757. info = new Utf8Info(utf8, numOfItems);
  758. strings.put(utf8, info);
  759. return addItem(info);
  760. }
  761. }
  762. /**
  763. * Replaces all occurrences of a class name.
  764. *
  765. * @param oldName the replaced name
  766. * @param newName the substituted name.
  767. */
  768. public void renameClass(String oldName, String newName) {
  769. LongVector v = items;
  770. int size = numOfItems;
  771. for (int i = 1; i < size; ++i)
  772. ((ConstInfo)v.elementAt(i)).renameClass(this, oldName, newName);
  773. }
  774. /**
  775. * Replaces all occurrences of class names.
  776. *
  777. * @param classnames specifies pairs of replaced and substituted
  778. * name.
  779. */
  780. public void renameClass(Map classnames) {
  781. LongVector v = items;
  782. int size = numOfItems;
  783. for (int i = 1; i < size; ++i)
  784. ((ConstInfo)v.elementAt(i)).renameClass(this, classnames);
  785. }
  786. private void read(DataInputStream in) throws IOException {
  787. int n = in.readUnsignedShort();
  788. int size = (n / LongVector.SIZE + 1) * LongVector.SIZE;
  789. items = new LongVector(size);
  790. numOfItems = 0;
  791. addItem(null); // index 0 is reserved by the JVM.
  792. while (--n > 0) { // index 0 is reserved by JVM
  793. int tag = readOne(in);
  794. if ((tag == LongInfo.tag) || (tag == DoubleInfo.tag)) {
  795. addItem(new ConstInfoPadding());
  796. --n;
  797. }
  798. }
  799. }
  800. private int readOne(DataInputStream in) throws IOException {
  801. ConstInfo info;
  802. int tag = in.readUnsignedByte();
  803. switch (tag) {
  804. case Utf8Info.tag : // 1
  805. info = new Utf8Info(in, numOfItems);
  806. strings.put(((Utf8Info)info).string, info);
  807. break;
  808. case IntegerInfo.tag : // 3
  809. info = new IntegerInfo(in);
  810. break;
  811. case FloatInfo.tag : // 4
  812. info = new FloatInfo(in);
  813. break;
  814. case LongInfo.tag : // 5
  815. info = new LongInfo(in);
  816. break;
  817. case DoubleInfo.tag : // 6
  818. info = new DoubleInfo(in);
  819. break;
  820. case ClassInfo.tag : // 7
  821. info = new ClassInfo(in, numOfItems);
  822. // classes.put(<classname>, info);
  823. break;
  824. case StringInfo.tag : // 8
  825. info = new StringInfo(in);
  826. break;
  827. case FieldrefInfo.tag : // 9
  828. info = new FieldrefInfo(in);
  829. break;
  830. case MethodrefInfo.tag : // 10
  831. info = new MethodrefInfo(in);
  832. break;
  833. case InterfaceMethodrefInfo.tag : // 11
  834. info = new InterfaceMethodrefInfo(in);
  835. break;
  836. case NameAndTypeInfo.tag : // 12
  837. info = new NameAndTypeInfo(in);
  838. break;
  839. default :
  840. throw new IOException("invalid constant type: " + tag);
  841. }
  842. addItem(info);
  843. return tag;
  844. }
  845. /**
  846. * Writes the contents of the constant pool table.
  847. */
  848. public void write(DataOutputStream out) throws IOException {
  849. out.writeShort(numOfItems);
  850. LongVector v = items;
  851. int size = numOfItems;
  852. for (int i = 1; i < size; ++i)
  853. ((ConstInfo)v.elementAt(i)).write(out);
  854. }
  855. /**
  856. * Prints the contents of the constant pool table.
  857. */
  858. public void print() {
  859. print(new PrintWriter(System.out, true));
  860. }
  861. /**
  862. * Prints the contents of the constant pool table.
  863. */
  864. public void print(PrintWriter out) {
  865. int size = numOfItems;
  866. for (int i = 1; i < size; ++i) {
  867. out.print(i);
  868. out.print(" ");
  869. ((ConstInfo)items.elementAt(i)).print(out);
  870. }
  871. }
  872. }
  873. abstract class ConstInfo {
  874. public abstract int getTag();
  875. public void renameClass(ConstPool cp, String oldName, String newName) {}
  876. public void renameClass(ConstPool cp, Map classnames) {}
  877. public abstract int copy(ConstPool src, ConstPool dest, Map classnames);
  878. // ** classnames is a mapping between JVM names.
  879. public abstract void write(DataOutputStream out) throws IOException;
  880. public abstract void print(PrintWriter out);
  881. public String toString() {
  882. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  883. PrintWriter out = new PrintWriter(bout);
  884. print(out);
  885. return bout.toString();
  886. }
  887. }
  888. /* padding following DoubleInfo or LongInfo.
  889. */
  890. class ConstInfoPadding extends ConstInfo {
  891. public int getTag() { return 0; }
  892. public int copy(ConstPool src, ConstPool dest, Map map) {
  893. return dest.addConstInfoPadding();
  894. }
  895. public void write(DataOutputStream out) throws IOException {}
  896. public void print(PrintWriter out) {
  897. out.println("padding");
  898. }
  899. }
  900. class ClassInfo extends ConstInfo {
  901. static final int tag = 7;
  902. int name;
  903. int index;
  904. public ClassInfo(int className, int i) {
  905. name = className;
  906. index = i;
  907. }
  908. public ClassInfo(DataInputStream in, int i) throws IOException {
  909. name = in.readUnsignedShort();
  910. index = i;
  911. }
  912. public int getTag() { return tag; }
  913. public void renameClass(ConstPool cp, String oldName, String newName) {
  914. String nameStr = cp.getUtf8Info(name);
  915. if (nameStr.equals(oldName))
  916. name = cp.addUtf8Info(newName);
  917. else if (nameStr.charAt(0) == '[') {
  918. String nameStr2 = Descriptor.rename(nameStr, oldName, newName);
  919. if (nameStr != nameStr2)
  920. name = cp.addUtf8Info(nameStr2);
  921. }
  922. }
  923. public void renameClass(ConstPool cp, Map map) {
  924. String oldName = cp.getUtf8Info(name);
  925. if (oldName.charAt(0) == '[') {
  926. String newName = Descriptor.rename(oldName, map);
  927. if (oldName != newName)
  928. name = cp.addUtf8Info(newName);
  929. }
  930. else {
  931. String newName = (String)map.get(oldName);
  932. if (newName != null && !newName.equals(oldName))
  933. name = cp.addUtf8Info(newName);
  934. }
  935. }
  936. public int copy(ConstPool src, ConstPool dest, Map map) {
  937. String classname = src.getUtf8Info(name);
  938. if (map != null) {
  939. String newname = (String)map.get(classname);
  940. if (newname != null)
  941. classname = newname;
  942. }
  943. return dest.addClassInfo(classname);
  944. }
  945. public void write(DataOutputStream out) throws IOException {
  946. out.writeByte(tag);
  947. out.writeShort(name);
  948. }
  949. public void print(PrintWriter out) {
  950. out.print("Class #");
  951. out.println(name);
  952. }
  953. }
  954. class NameAndTypeInfo extends ConstInfo {
  955. static final int tag = 12;
  956. int memberName;
  957. int typeDescriptor;
  958. public NameAndTypeInfo(int name, int type) {
  959. memberName = name;
  960. typeDescriptor = type;
  961. }
  962. public NameAndTypeInfo(DataInputStream in) throws IOException {
  963. memberName = in.readUnsignedShort();
  964. typeDescriptor = in.readUnsignedShort();
  965. }
  966. public int getTag() { return tag; }
  967. public void renameClass(ConstPool cp, String oldName, String newName) {
  968. String type = cp.getUtf8Info(typeDescriptor);
  969. String type2 = Descriptor.rename(type, oldName, newName);
  970. if (type != type2)
  971. typeDescriptor = cp.addUtf8Info(type2);
  972. }
  973. public void renameClass(ConstPool cp, Map map) {
  974. String type = cp.getUtf8Info(typeDescriptor);
  975. String type2 = Descriptor.rename(type, map);
  976. if (type != type2)
  977. typeDescriptor = cp.addUtf8Info(type2);
  978. }
  979. public int copy(ConstPool src, ConstPool dest, Map map) {
  980. String mname = src.getUtf8Info(memberName);
  981. String tdesc = src.getUtf8Info(typeDescriptor);
  982. tdesc = Descriptor.rename(tdesc, map);
  983. return dest.addNameAndTypeInfo(dest.addUtf8Info(mname),
  984. dest.addUtf8Info(tdesc));
  985. }
  986. public void write(DataOutputStream out) throws IOException {
  987. out.writeByte(tag);
  988. out.writeShort(memberName);
  989. out.writeShort(typeDescriptor);
  990. }
  991. public void print(PrintWriter out) {
  992. out.print("NameAndType #");
  993. out.print(memberName);
  994. out.print(", type #");
  995. out.println(typeDescriptor);
  996. }
  997. }
  998. abstract class MemberrefInfo extends ConstInfo {
  999. int classIndex;
  1000. int nameAndTypeIndex;
  1001. public MemberrefInfo(int cindex, int ntindex) {
  1002. classIndex = cindex;
  1003. nameAndTypeIndex = ntindex;
  1004. }
  1005. public MemberrefInfo(DataInputStream in) throws IOException {
  1006. classIndex = in.readUnsignedShort();
  1007. nameAndTypeIndex = in.readUnsignedShort();
  1008. }
  1009. public int copy(ConstPool src, ConstPool dest, Map map) {
  1010. int classIndex2 = src.getItem(classIndex).copy(src, dest, map);
  1011. int ntIndex2 = src.getItem(nameAndTypeIndex).copy(src, dest, map);
  1012. return copy2(dest, classIndex2, ntIndex2);
  1013. }
  1014. abstract protected int copy2(ConstPool dest, int cindex, int ntindex);
  1015. public void write(DataOutputStream out) throws IOException {
  1016. out.writeByte(getTag());
  1017. out.writeShort(classIndex);
  1018. out.writeShort(nameAndTypeIndex);
  1019. }
  1020. public void print(PrintWriter out) {
  1021. out.print(getTagName() + " #");
  1022. out.print(classIndex);
  1023. out.print(", name&type #");
  1024. out.println(nameAndTypeIndex);
  1025. }
  1026. public abstract String getTagName();
  1027. }
  1028. class FieldrefInfo extends MemberrefInfo {
  1029. static final int tag = 9;
  1030. public FieldrefInfo(int cindex, int ntindex) {
  1031. super(cindex, ntindex);
  1032. }
  1033. public FieldrefInfo(DataInputStream in) throws IOException {
  1034. super(in);
  1035. }
  1036. public int getTag() { return tag; }
  1037. public String getTagName() { return "Field"; }
  1038. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1039. return dest.addFieldrefInfo(cindex, ntindex);
  1040. }
  1041. }
  1042. class MethodrefInfo extends MemberrefInfo {
  1043. static final int tag = 10;
  1044. public MethodrefInfo(int cindex, int ntindex) {
  1045. super(cindex, ntindex);
  1046. }
  1047. public MethodrefInfo(DataInputStream in) throws IOException {
  1048. super(in);
  1049. }
  1050. public int getTag() { return tag; }
  1051. public String getTagName() { return "Method"; }
  1052. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1053. return dest.addMethodrefInfo(cindex, ntindex);
  1054. }
  1055. }
  1056. class InterfaceMethodrefInfo extends MemberrefInfo {
  1057. static final int tag = 11;
  1058. public InterfaceMethodrefInfo(int cindex, int ntindex) {
  1059. super(cindex, ntindex);
  1060. }
  1061. public InterfaceMethodrefInfo(DataInputStream in) throws IOException {
  1062. super(in);
  1063. }
  1064. public int getTag() { return tag; }
  1065. public String getTagName() { return "Interface"; }
  1066. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1067. return dest.addInterfaceMethodrefInfo(cindex, ntindex);
  1068. }
  1069. }
  1070. class StringInfo extends ConstInfo {
  1071. static final int tag = 8;
  1072. int string;
  1073. public StringInfo(int str) {
  1074. string = str;
  1075. }
  1076. public StringInfo(DataInputStream in) throws IOException {
  1077. string = in.readUnsignedShort();
  1078. }
  1079. public int getTag() { return tag; }
  1080. public int copy(ConstPool src, ConstPool dest, Map map) {
  1081. return dest.addStringInfo(src.getUtf8Info(string));
  1082. }
  1083. public void write(DataOutputStream out) throws IOException {
  1084. out.writeByte(tag);
  1085. out.writeShort(string);
  1086. }
  1087. public void print(PrintWriter out) {
  1088. out.print("String #");
  1089. out.println(string);
  1090. }
  1091. }
  1092. class IntegerInfo extends ConstInfo {
  1093. static final int tag = 3;
  1094. int value;
  1095. public IntegerInfo(int i) {
  1096. value = i;
  1097. }
  1098. public IntegerInfo(DataInputStream in) throws IOException {
  1099. value = in.readInt();
  1100. }
  1101. public int getTag() { return tag; }
  1102. public int copy(ConstPool src, ConstPool dest, Map map) {
  1103. return dest.addIntegerInfo(value);
  1104. }
  1105. public void write(DataOutputStream out) throws IOException {
  1106. out.writeByte(tag);
  1107. out.writeInt(value);
  1108. }
  1109. public void print(PrintWriter out) {
  1110. out.print("Integer ");
  1111. out.println(value);
  1112. }
  1113. }
  1114. class FloatInfo extends ConstInfo {
  1115. static final int tag = 4;
  1116. float value;
  1117. public FloatInfo(float f) {
  1118. value = f;
  1119. }
  1120. public FloatInfo(DataInputStream in) throws IOException {
  1121. value = in.readFloat();
  1122. }
  1123. public int getTag() { return tag; }
  1124. public int copy(ConstPool src, ConstPool dest, Map map) {
  1125. return dest.addFloatInfo(value);
  1126. }
  1127. public void write(DataOutputStream out) throws IOException {
  1128. out.writeByte(tag);
  1129. out.writeFloat(value);
  1130. }
  1131. public void print(PrintWriter out) {
  1132. out.print("Float ");
  1133. out.println(value);
  1134. }
  1135. }
  1136. class LongInfo extends ConstInfo {
  1137. static final int tag = 5;
  1138. long value;
  1139. public LongInfo(long l) {
  1140. value = l;
  1141. }
  1142. public LongInfo(DataInputStream in) throws IOException {
  1143. value = in.readLong();
  1144. }
  1145. public int getTag() { return tag; }
  1146. public int copy(ConstPool src, ConstPool dest, Map map) {
  1147. return dest.addLongInfo(value);
  1148. }
  1149. public void write(DataOutputStream out) throws IOException {
  1150. out.writeByte(tag);
  1151. out.writeLong(value);
  1152. }
  1153. public void print(PrintWriter out) {
  1154. out.print("Long ");
  1155. out.println(value);
  1156. }
  1157. }
  1158. class DoubleInfo extends ConstInfo {
  1159. static final int tag = 6;
  1160. double value;
  1161. public DoubleInfo(double d) {
  1162. value = d;
  1163. }
  1164. public DoubleInfo(DataInputStream in) throws IOException {
  1165. value = in.readDouble();
  1166. }
  1167. public int getTag() { return tag; }
  1168. public int copy(ConstPool src, ConstPool dest, Map map) {
  1169. return dest.addDoubleInfo(value);
  1170. }
  1171. public void write(DataOutputStream out) throws IOException {
  1172. out.writeByte(tag);
  1173. out.writeDouble(value);
  1174. }
  1175. public void print(PrintWriter out) {
  1176. out.print("Double ");
  1177. out.println(value);
  1178. }
  1179. }
  1180. class Utf8Info extends ConstInfo {
  1181. static final int tag = 1;
  1182. String string;
  1183. int index;
  1184. public Utf8Info(String utf8, int i) {
  1185. string = utf8;
  1186. index = i;
  1187. }
  1188. public Utf8Info(DataInputStream in, int i) throws IOException {
  1189. string = in.readUTF();
  1190. index = i;
  1191. }
  1192. public int getTag() { return tag; }
  1193. public int copy(ConstPool src, ConstPool dest, Map map) {
  1194. return dest.addUtf8Info(string);
  1195. }
  1196. public void write(DataOutputStream out) throws IOException {
  1197. out.writeByte(tag);
  1198. out.writeUTF(string);
  1199. }
  1200. public void print(PrintWriter out) {
  1201. out.print("UTF8 \"");
  1202. out.print(string);
  1203. out.println("\"");
  1204. }
  1205. }