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

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