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

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