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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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. * Changed the Utf8Info of the <code>name_index</code> field of the
  327. * <code>CONSTANT_NameAndType_info</code> structure
  328. * indirectly specified by the given index.
  329. *
  330. * @param index an index to a <code>CONSTANT_Methodref_info</code>.
  331. * @param string the new Utf8 string
  332. */
  333. public void setMethodrefType(int index, String string) {
  334. MethodrefInfo minfo = (MethodrefInfo)getItem(index);
  335. if (minfo == null)
  336. throw new IllegalArgumentException("Not a Methodref_info " + index);
  337. else {
  338. NameAndTypeInfo n
  339. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  340. if(n == null)
  341. throw new IllegalStateException("Unable to find NameAndTypeInfo " + index);
  342. else
  343. setUtf8Info(n.typeDescriptor, string);
  344. }
  345. }
  346. /**
  347. * Reads the <code>class_index</code> field of the
  348. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  349. * at the given index.
  350. */
  351. public int getInterfaceMethodrefClass(int index) {
  352. InterfaceMethodrefInfo minfo
  353. = (InterfaceMethodrefInfo)getItem(index);
  354. return minfo.classIndex;
  355. }
  356. /**
  357. * Reads the <code>class_index</code> field of the
  358. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  359. * at the given index.
  360. *
  361. * @return the name of the class at that <code>class_index</code>.
  362. */
  363. public String getInterfaceMethodrefClassName(int index) {
  364. InterfaceMethodrefInfo minfo
  365. = (InterfaceMethodrefInfo)getItem(index);
  366. return getClassInfo(minfo.classIndex);
  367. }
  368. /**
  369. * Reads the <code>name_and_type_index</code> field of the
  370. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  371. * at the given index.
  372. */
  373. public int getInterfaceMethodrefNameAndType(int index) {
  374. InterfaceMethodrefInfo minfo
  375. = (InterfaceMethodrefInfo)getItem(index);
  376. return minfo.nameAndTypeIndex;
  377. }
  378. /**
  379. * Reads the <code>name_index</code> field of the
  380. * <code>CONSTANT_NameAndType_info</code> structure
  381. * indirectly specified by the given index.
  382. *
  383. * @param index an index to
  384. * a <code>CONSTANT_InterfaceMethodref_info</code>.
  385. * @return the name of the method.
  386. */
  387. public String getInterfaceMethodrefName(int index) {
  388. InterfaceMethodrefInfo minfo
  389. = (InterfaceMethodrefInfo)getItem(index);
  390. if (minfo == null)
  391. return null;
  392. else {
  393. NameAndTypeInfo n
  394. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  395. if(n == null)
  396. return null;
  397. else
  398. return getUtf8Info(n.memberName);
  399. }
  400. }
  401. /**
  402. * Reads the <code>descriptor_index</code> field of the
  403. * <code>CONSTANT_NameAndType_info</code> structure
  404. * indirectly specified by the given index.
  405. *
  406. * @param index an index to
  407. * a <code>CONSTANT_InterfaceMethodref_info</code>.
  408. * @return the descriptor of the method.
  409. */
  410. public String getInterfaceMethodrefType(int index) {
  411. InterfaceMethodrefInfo minfo
  412. = (InterfaceMethodrefInfo)getItem(index);
  413. if (minfo == null)
  414. return null;
  415. else {
  416. NameAndTypeInfo n
  417. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  418. if(n == null)
  419. return null;
  420. else
  421. return getUtf8Info(n.typeDescriptor);
  422. }
  423. }
  424. /**
  425. * Changed the Utf8Info of the <code>name_index</code> field of the
  426. * <code>CONSTANT_NameAndType_info</code> structure
  427. * indirectly specified by the given index.
  428. *
  429. * @param index an index to a <code>CONSTANT_Methodref_info</code>.
  430. * @param string the new Utf8 string
  431. */
  432. public void setInterfaceMethodrefType(int index, String string) {
  433. InterfaceMethodrefInfo minfo = (InterfaceMethodrefInfo)getItem(index);
  434. if (minfo == null)
  435. throw new IllegalArgumentException("Not an InterfaceMethodref_info " + index);
  436. else {
  437. NameAndTypeInfo n
  438. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  439. if(n == null)
  440. throw new IllegalStateException("Unable to find NameAndTypeInfo " + index);
  441. else
  442. setUtf8Info(n.typeDescriptor, string);
  443. }
  444. }
  445. /**
  446. * Reads <code>CONSTANT_Integer_info</code>, <code>_Float_info</code>,
  447. * <code>_Long_info</code>, <code>_Double_info</code>, or
  448. * <code>_String_info</code> structure.
  449. * These are used with the LDC instruction.
  450. *
  451. * @return a <code>String</code> value or a wrapped primitive-type
  452. * value.
  453. */
  454. public Object getLdcValue(int index) {
  455. ConstInfo constInfo = this.getItem(index);
  456. Object value = null;
  457. if (constInfo instanceof StringInfo)
  458. value = this.getStringInfo(index);
  459. else if (constInfo instanceof FloatInfo)
  460. value = new Float(getFloatInfo(index));
  461. else if (constInfo instanceof IntegerInfo)
  462. value = new Integer(getIntegerInfo(index));
  463. else if (constInfo instanceof LongInfo)
  464. value = new Long(getLongInfo(index));
  465. else if (constInfo instanceof DoubleInfo)
  466. value = new Double(getDoubleInfo(index));
  467. else
  468. value = null;
  469. return value;
  470. }
  471. /**
  472. * Reads <code>CONSTANT_Integer_info</code> structure
  473. * at the given index.
  474. *
  475. * @return the value specified by this entry.
  476. */
  477. public int getIntegerInfo(int index) {
  478. IntegerInfo i = (IntegerInfo)getItem(index);
  479. return i.value;
  480. }
  481. /**
  482. * Reads <code>CONSTANT_Float_info</code> structure
  483. * at the given index.
  484. *
  485. * @return the value specified by this entry.
  486. */
  487. public float getFloatInfo(int index) {
  488. FloatInfo i = (FloatInfo)getItem(index);
  489. return i.value;
  490. }
  491. /**
  492. * Reads <code>CONSTANT_Long_info</code> structure
  493. * at the given index.
  494. *
  495. * @return the value specified by this entry.
  496. */
  497. public long getLongInfo(int index) {
  498. LongInfo i = (LongInfo)getItem(index);
  499. return i.value;
  500. }
  501. /**
  502. * Reads <code>CONSTANT_Double_info</code> structure
  503. * at the given index.
  504. *
  505. * @return the value specified by this entry.
  506. */
  507. public double getDoubleInfo(int index) {
  508. DoubleInfo i = (DoubleInfo)getItem(index);
  509. return i.value;
  510. }
  511. /**
  512. * Reads <code>CONSTANT_String_info</code> structure
  513. * at the given index.
  514. *
  515. * @return the string specified by <code>string_index</code>.
  516. */
  517. public String getStringInfo(int index) {
  518. StringInfo si = (StringInfo)getItem(index);
  519. return getUtf8Info(si.string);
  520. }
  521. /**
  522. * Reads <code>CONSTANT_utf8_info</code> structure
  523. * at the given index.
  524. *
  525. * @return the string specified by this entry.
  526. */
  527. public String getUtf8Info(int index) {
  528. Utf8Info utf = (Utf8Info)getItem(index);
  529. return utf.string;
  530. }
  531. /**
  532. * Sets <code>CONSTANT_utf8_info</code> structure
  533. * at the given index.
  534. *
  535. * @param index the index
  536. * @param string the string specified by this entry.
  537. */
  538. private void setUtf8Info(int index, String string) {
  539. Utf8Info utf = (Utf8Info)getItem(index);
  540. utf.string = string;
  541. }
  542. /**
  543. * Determines whether <code>CONSTANT_Methodref_info</code>
  544. * structure at the given index represents the constructor
  545. * of the given class.
  546. *
  547. * @return the <code>descriptor_index</code> specifying
  548. * the type descriptor of the that constructor.
  549. * If it is not that constructor,
  550. * <code>isConstructor()</code> returns 0.
  551. */
  552. public int isConstructor(String classname, int index) {
  553. return isMember(classname, MethodInfo.nameInit, index);
  554. }
  555. /**
  556. * Determines whether <code>CONSTANT_Methodref_info</code>,
  557. * <code>CONSTANT_Fieldref_info</code>, or
  558. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  559. * at the given index represents the member with the specified
  560. * name and declaring class.
  561. *
  562. * @param classname the class declaring the member
  563. * @param membername the member name
  564. * @param index the index into the constant pool table
  565. *
  566. * @return the <code>descriptor_index</code> specifying
  567. * the type descriptor of that member.
  568. * If it is not that member,
  569. * <code>isMember()</code> returns 0.
  570. */
  571. public int isMember(String classname, String membername, int index) {
  572. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  573. if (getClassInfo(minfo.classIndex).equals(classname)) {
  574. NameAndTypeInfo ntinfo
  575. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  576. if (getUtf8Info(ntinfo.memberName).equals(membername))
  577. return ntinfo.typeDescriptor;
  578. }
  579. return 0; // false
  580. }
  581. private int addItem(ConstInfo info) {
  582. items.addElement(info);
  583. return numOfItems++;
  584. }
  585. /**
  586. * Copies the n-th item in this ConstPool object into the destination
  587. * ConstPool object.
  588. * The class names that the item refers to are renamed according
  589. * to the given map.
  590. *
  591. * @param n the <i>n</i>-th item
  592. * @param dest destination constant pool table
  593. * @param classnames the map or null.
  594. * @return the index of the copied item into the destination ClassPool.
  595. */
  596. public int copy(int n, ConstPool dest, Map classnames) {
  597. if (n == 0)
  598. return 0;
  599. ConstInfo info = getItem(n);
  600. return info.copy(this, dest, classnames);
  601. }
  602. int addConstInfoPadding() {
  603. return addItem(new ConstInfoPadding());
  604. }
  605. /**
  606. * Adds a new <code>CONSTANT_Class_info</code> structure.
  607. *
  608. * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure
  609. * for storing the class name.
  610. *
  611. * @return the index of the added entry.
  612. */
  613. public int addClassInfo(CtClass c) {
  614. if (c == THIS)
  615. return thisClassInfo;
  616. else if (!c.isArray())
  617. return addClassInfo(c.getName());
  618. else {
  619. // an array type is recorded in the hashtable with
  620. // the key "[L<classname>;" instead of "<classname>".
  621. //
  622. // note: toJvmName(toJvmName(c)) is equal to toJvmName(c).
  623. return addClassInfo(Descriptor.toJvmName(c));
  624. }
  625. }
  626. /**
  627. * Adds a new <code>CONSTANT_Class_info</code> structure.
  628. *
  629. * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure
  630. * for storing the class name.
  631. *
  632. * @param qname a fully-qualified class name
  633. * (or the JVM-internal representation of that name).
  634. * @return the index of the added entry.
  635. */
  636. public int addClassInfo(String qname) {
  637. ClassInfo info = (ClassInfo)classes.get(qname);
  638. if (info != null)
  639. return info.index;
  640. else {
  641. int utf8 = addUtf8Info(Descriptor.toJvmName(qname));
  642. info = new ClassInfo(utf8, numOfItems);
  643. classes.put(qname, info);
  644. return addItem(info);
  645. }
  646. }
  647. /**
  648. * Adds a new <code>CONSTANT_NameAndType_info</code> structure.
  649. *
  650. * <p>This also adds <code>CONSTANT_Utf8_info</code> structures.
  651. *
  652. * @param name <code>name_index</code>
  653. * @param type <code>descriptor_index</code>
  654. * @return the index of the added entry.
  655. */
  656. public int addNameAndTypeInfo(String name, String type) {
  657. return addNameAndTypeInfo(addUtf8Info(name), addUtf8Info(type));
  658. }
  659. /**
  660. * Adds a new <code>CONSTANT_NameAndType_info</code> structure.
  661. *
  662. * @param name <code>name_index</code>
  663. * @param type <code>descriptor_index</code>
  664. * @return the index of the added entry.
  665. */
  666. public int addNameAndTypeInfo(int name, int type) {
  667. return addItem(new NameAndTypeInfo(name, type));
  668. }
  669. /**
  670. * Adds a new <code>CONSTANT_Fieldref_info</code> structure.
  671. *
  672. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  673. * structure.
  674. *
  675. * @param classInfo <code>class_index</code>
  676. * @param name <code>name_index</code>
  677. * of <code>CONSTANT_NameAndType_info</code>.
  678. * @param type <code>descriptor_index</code>
  679. * of <code>CONSTANT_NameAndType_info</code>.
  680. * @return the index of the added entry.
  681. */
  682. public int addFieldrefInfo(int classInfo, String name, String type) {
  683. int nt = addNameAndTypeInfo(name, type);
  684. return addFieldrefInfo(classInfo, nt);
  685. }
  686. /**
  687. * Adds a new <code>CONSTANT_Fieldref_info</code> structure.
  688. *
  689. * @param classInfo <code>class_index</code>
  690. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  691. * @return the index of the added entry.
  692. */
  693. public int addFieldrefInfo(int classInfo, int nameAndTypeInfo) {
  694. return addItem(new FieldrefInfo(classInfo, nameAndTypeInfo));
  695. }
  696. /**
  697. * Adds a new <code>CONSTANT_Methodref_info</code> structure.
  698. *
  699. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  700. * structure.
  701. *
  702. * @param classInfo <code>class_index</code>
  703. * @param name <code>name_index</code>
  704. * of <code>CONSTANT_NameAndType_info</code>.
  705. * @param type <code>descriptor_index</code>
  706. * of <code>CONSTANT_NameAndType_info</code>.
  707. * @return the index of the added entry.
  708. */
  709. public int addMethodrefInfo(int classInfo, String name, String type) {
  710. int nt = addNameAndTypeInfo(name, type);
  711. return addMethodrefInfo(classInfo, nt);
  712. }
  713. /**
  714. * Adds a new <code>CONSTANT_Methodref_info</code> structure.
  715. *
  716. * @param classInfo <code>class_index</code>
  717. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  718. * @return the index of the added entry.
  719. */
  720. public int addMethodrefInfo(int classInfo, int nameAndTypeInfo) {
  721. return addItem(new MethodrefInfo(classInfo, nameAndTypeInfo));
  722. }
  723. /**
  724. * Adds a new <code>CONSTANT_InterfaceMethodref_info</code>
  725. * structure.
  726. *
  727. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  728. * structure.
  729. *
  730. * @param classInfo <code>class_index</code>
  731. * @param name <code>name_index</code>
  732. * of <code>CONSTANT_NameAndType_info</code>.
  733. * @param type <code>descriptor_index</code>
  734. * of <code>CONSTANT_NameAndType_info</code>.
  735. * @return the index of the added entry.
  736. */
  737. public int addInterfaceMethodrefInfo(int classInfo, String name,
  738. String type) {
  739. int nt = addNameAndTypeInfo(name, type);
  740. return addInterfaceMethodrefInfo(classInfo, nt);
  741. }
  742. /**
  743. * Adds a new <code>CONSTANT_InterfaceMethodref_info</code>
  744. * structure.
  745. *
  746. * @param classInfo <code>class_index</code>
  747. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  748. * @return the index of the added entry.
  749. */
  750. public int addInterfaceMethodrefInfo(int classInfo,
  751. int nameAndTypeInfo) {
  752. return addItem(new InterfaceMethodrefInfo(classInfo,
  753. nameAndTypeInfo));
  754. }
  755. /**
  756. * Adds a new <code>CONSTANT_String_info</code>
  757. * structure.
  758. *
  759. * <p>This also adds a new <code>CONSTANT_Utf8_info</code>
  760. * structure.
  761. *
  762. * @return the index of the added entry.
  763. */
  764. public int addStringInfo(String str) {
  765. return addItem(new StringInfo(addUtf8Info(str)));
  766. }
  767. /**
  768. * Adds a new <code>CONSTANT_Integer_info</code>
  769. * structure.
  770. *
  771. * @return the index of the added entry.
  772. */
  773. public int addIntegerInfo(int i) {
  774. return addItem(new IntegerInfo(i));
  775. }
  776. /**
  777. * Adds a new <code>CONSTANT_Float_info</code>
  778. * structure.
  779. *
  780. * @return the index of the added entry.
  781. */
  782. public int addFloatInfo(float f) {
  783. return addItem(new FloatInfo(f));
  784. }
  785. /**
  786. * Adds a new <code>CONSTANT_Long_info</code>
  787. * structure.
  788. *
  789. * @return the index of the added entry.
  790. */
  791. public int addLongInfo(long l) {
  792. int i = addItem(new LongInfo(l));
  793. addItem(new ConstInfoPadding());
  794. return i;
  795. }
  796. /**
  797. * Adds a new <code>CONSTANT_Double_info</code>
  798. * structure.
  799. *
  800. * @return the index of the added entry.
  801. */
  802. public int addDoubleInfo(double d) {
  803. int i = addItem(new DoubleInfo(d));
  804. addItem(new ConstInfoPadding());
  805. return i;
  806. }
  807. /**
  808. * Adds a new <code>CONSTANT_Utf8_info</code>
  809. * structure.
  810. *
  811. * <p>If the given utf8 string has been already recorded in the
  812. * table, then this method does not add a new entry to avoid adding
  813. * a duplicated entry.
  814. * Instead, it returns the index of the entry already recorded.
  815. *
  816. * @return the index of the added entry.
  817. */
  818. public int addUtf8Info(String utf8) {
  819. Utf8Info info = (Utf8Info)strings.get(utf8);
  820. if (info != null)
  821. return info.index;
  822. else {
  823. info = new Utf8Info(utf8, numOfItems);
  824. strings.put(utf8, info);
  825. return addItem(info);
  826. }
  827. }
  828. /**
  829. * Get all the class names.
  830. *
  831. * @return a set of class names
  832. */
  833. public Set getClassNames()
  834. {
  835. HashSet result = new HashSet();
  836. LongVector v = items;
  837. int size = numOfItems;
  838. for (int i = 1; i < size; ++i) {
  839. String className = ((ConstInfo) v.elementAt(i)).getClassName(this);
  840. if (className != null)
  841. result.add(className);
  842. }
  843. return result;
  844. }
  845. /**
  846. * Replaces all occurrences of a class name.
  847. *
  848. * @param oldName the replaced name (JVM-internal representation).
  849. * @param newName the substituted name (JVM-internal representation).
  850. */
  851. public void renameClass(String oldName, String newName) {
  852. LongVector v = items;
  853. int size = numOfItems;
  854. classes = new HashMap(classes.size() * 2);
  855. for (int i = 1; i < size; ++i) {
  856. ConstInfo ci = (ConstInfo)v.elementAt(i);
  857. ci.renameClass(this, oldName, newName);
  858. ci.makeHashtable(this);
  859. }
  860. }
  861. /**
  862. * Replaces all occurrences of class names.
  863. *
  864. * @param classnames specifies pairs of replaced and substituted
  865. * name.
  866. */
  867. public void renameClass(Map classnames) {
  868. LongVector v = items;
  869. int size = numOfItems;
  870. classes = new HashMap(classes.size() * 2);
  871. for (int i = 1; i < size; ++i) {
  872. ConstInfo ci = (ConstInfo)v.elementAt(i);
  873. ci.renameClass(this, classnames);
  874. ci.makeHashtable(this);
  875. }
  876. }
  877. private void read(DataInputStream in) throws IOException {
  878. int n = in.readUnsignedShort();
  879. items = new LongVector(n);
  880. numOfItems = 0;
  881. addItem(null); // index 0 is reserved by the JVM.
  882. while (--n > 0) { // index 0 is reserved by JVM
  883. int tag = readOne(in);
  884. if ((tag == LongInfo.tag) || (tag == DoubleInfo.tag)) {
  885. addItem(new ConstInfoPadding());
  886. --n;
  887. }
  888. }
  889. int i = 1;
  890. while (true) {
  891. ConstInfo info = (ConstInfo)items.elementAt(i++);
  892. if (info == null)
  893. break;
  894. else
  895. info.makeHashtable(this);
  896. }
  897. }
  898. private int readOne(DataInputStream in) throws IOException {
  899. ConstInfo info;
  900. int tag = in.readUnsignedByte();
  901. switch (tag) {
  902. case Utf8Info.tag : // 1
  903. info = new Utf8Info(in, numOfItems);
  904. strings.put(((Utf8Info)info).string, info);
  905. break;
  906. case IntegerInfo.tag : // 3
  907. info = new IntegerInfo(in);
  908. break;
  909. case FloatInfo.tag : // 4
  910. info = new FloatInfo(in);
  911. break;
  912. case LongInfo.tag : // 5
  913. info = new LongInfo(in);
  914. break;
  915. case DoubleInfo.tag : // 6
  916. info = new DoubleInfo(in);
  917. break;
  918. case ClassInfo.tag : // 7
  919. info = new ClassInfo(in, numOfItems);
  920. // classes.put(<classname>, info);
  921. break;
  922. case StringInfo.tag : // 8
  923. info = new StringInfo(in);
  924. break;
  925. case FieldrefInfo.tag : // 9
  926. info = new FieldrefInfo(in);
  927. break;
  928. case MethodrefInfo.tag : // 10
  929. info = new MethodrefInfo(in);
  930. break;
  931. case InterfaceMethodrefInfo.tag : // 11
  932. info = new InterfaceMethodrefInfo(in);
  933. break;
  934. case NameAndTypeInfo.tag : // 12
  935. info = new NameAndTypeInfo(in);
  936. break;
  937. default :
  938. throw new IOException("invalid constant type: " + tag);
  939. }
  940. addItem(info);
  941. return tag;
  942. }
  943. /**
  944. * Writes the contents of the constant pool table.
  945. */
  946. public void write(DataOutputStream out) throws IOException {
  947. out.writeShort(numOfItems);
  948. LongVector v = items;
  949. int size = numOfItems;
  950. for (int i = 1; i < size; ++i)
  951. ((ConstInfo)v.elementAt(i)).write(out);
  952. }
  953. /**
  954. * Prints the contents of the constant pool table.
  955. */
  956. public void print() {
  957. print(new PrintWriter(System.out, true));
  958. }
  959. /**
  960. * Prints the contents of the constant pool table.
  961. */
  962. public void print(PrintWriter out) {
  963. int size = numOfItems;
  964. for (int i = 1; i < size; ++i) {
  965. out.print(i);
  966. out.print(" ");
  967. ((ConstInfo)items.elementAt(i)).print(out);
  968. }
  969. }
  970. }
  971. abstract class ConstInfo {
  972. public abstract int getTag();
  973. public String getClassName(ConstPool cp) { return null; }
  974. public void renameClass(ConstPool cp, String oldName, String newName) {}
  975. public void renameClass(ConstPool cp, Map classnames) {}
  976. public abstract int copy(ConstPool src, ConstPool dest, Map classnames);
  977. // ** classnames is a mapping between JVM names.
  978. public abstract void write(DataOutputStream out) throws IOException;
  979. public abstract void print(PrintWriter out);
  980. void makeHashtable(ConstPool cp) {} // called after read() finishes in ConstPool.
  981. public String toString() {
  982. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  983. PrintWriter out = new PrintWriter(bout);
  984. print(out);
  985. return bout.toString();
  986. }
  987. }
  988. /* padding following DoubleInfo or LongInfo.
  989. */
  990. class ConstInfoPadding extends ConstInfo {
  991. public int getTag() { return 0; }
  992. public int copy(ConstPool src, ConstPool dest, Map map) {
  993. return dest.addConstInfoPadding();
  994. }
  995. public void write(DataOutputStream out) throws IOException {}
  996. public void print(PrintWriter out) {
  997. out.println("padding");
  998. }
  999. }
  1000. class ClassInfo extends ConstInfo {
  1001. static final int tag = 7;
  1002. int name;
  1003. int index;
  1004. public ClassInfo(int className, int i) {
  1005. name = className;
  1006. index = i;
  1007. }
  1008. public ClassInfo(DataInputStream in, int i) throws IOException {
  1009. name = in.readUnsignedShort();
  1010. index = i;
  1011. }
  1012. public int getTag() { return tag; }
  1013. public String getClassName(ConstPool cp) {
  1014. return cp.getUtf8Info(name);
  1015. };
  1016. public void renameClass(ConstPool cp, String oldName, String newName) {
  1017. String nameStr = cp.getUtf8Info(name);
  1018. if (nameStr.equals(oldName))
  1019. name = cp.addUtf8Info(newName);
  1020. else if (nameStr.charAt(0) == '[') {
  1021. String nameStr2 = Descriptor.rename(nameStr, oldName, newName);
  1022. if (nameStr != nameStr2)
  1023. name = cp.addUtf8Info(nameStr2);
  1024. }
  1025. }
  1026. public void renameClass(ConstPool cp, Map map) {
  1027. String oldName = cp.getUtf8Info(name);
  1028. if (oldName.charAt(0) == '[') {
  1029. String newName = Descriptor.rename(oldName, map);
  1030. if (oldName != newName)
  1031. name = cp.addUtf8Info(newName);
  1032. }
  1033. else {
  1034. String newName = (String)map.get(oldName);
  1035. if (newName != null && !newName.equals(oldName))
  1036. name = cp.addUtf8Info(newName);
  1037. }
  1038. }
  1039. public int copy(ConstPool src, ConstPool dest, Map map) {
  1040. String classname = src.getUtf8Info(name);
  1041. if (map != null) {
  1042. String newname = (String)map.get(classname);
  1043. if (newname != null)
  1044. classname = newname;
  1045. }
  1046. return dest.addClassInfo(classname);
  1047. }
  1048. public void write(DataOutputStream out) throws IOException {
  1049. out.writeByte(tag);
  1050. out.writeShort(name);
  1051. }
  1052. public void print(PrintWriter out) {
  1053. out.print("Class #");
  1054. out.println(name);
  1055. }
  1056. void makeHashtable(ConstPool cp) {
  1057. String name = Descriptor.toJavaName(getClassName(cp));
  1058. cp.classes.put(name, this);
  1059. }
  1060. }
  1061. class NameAndTypeInfo extends ConstInfo {
  1062. static final int tag = 12;
  1063. int memberName;
  1064. int typeDescriptor;
  1065. public NameAndTypeInfo(int name, int type) {
  1066. memberName = name;
  1067. typeDescriptor = type;
  1068. }
  1069. public NameAndTypeInfo(DataInputStream in) throws IOException {
  1070. memberName = in.readUnsignedShort();
  1071. typeDescriptor = in.readUnsignedShort();
  1072. }
  1073. public int getTag() { return tag; }
  1074. public void renameClass(ConstPool cp, String oldName, String newName) {
  1075. String type = cp.getUtf8Info(typeDescriptor);
  1076. String type2 = Descriptor.rename(type, oldName, newName);
  1077. if (type != type2)
  1078. typeDescriptor = cp.addUtf8Info(type2);
  1079. }
  1080. public void renameClass(ConstPool cp, Map map) {
  1081. String type = cp.getUtf8Info(typeDescriptor);
  1082. String type2 = Descriptor.rename(type, map);
  1083. if (type != type2)
  1084. typeDescriptor = cp.addUtf8Info(type2);
  1085. }
  1086. public int copy(ConstPool src, ConstPool dest, Map map) {
  1087. String mname = src.getUtf8Info(memberName);
  1088. String tdesc = src.getUtf8Info(typeDescriptor);
  1089. tdesc = Descriptor.rename(tdesc, map);
  1090. return dest.addNameAndTypeInfo(dest.addUtf8Info(mname),
  1091. dest.addUtf8Info(tdesc));
  1092. }
  1093. public void write(DataOutputStream out) throws IOException {
  1094. out.writeByte(tag);
  1095. out.writeShort(memberName);
  1096. out.writeShort(typeDescriptor);
  1097. }
  1098. public void print(PrintWriter out) {
  1099. out.print("NameAndType #");
  1100. out.print(memberName);
  1101. out.print(", type #");
  1102. out.println(typeDescriptor);
  1103. }
  1104. }
  1105. abstract class MemberrefInfo extends ConstInfo {
  1106. int classIndex;
  1107. int nameAndTypeIndex;
  1108. public MemberrefInfo(int cindex, int ntindex) {
  1109. classIndex = cindex;
  1110. nameAndTypeIndex = ntindex;
  1111. }
  1112. public MemberrefInfo(DataInputStream in) throws IOException {
  1113. classIndex = in.readUnsignedShort();
  1114. nameAndTypeIndex = in.readUnsignedShort();
  1115. }
  1116. public int copy(ConstPool src, ConstPool dest, Map map) {
  1117. int classIndex2 = src.getItem(classIndex).copy(src, dest, map);
  1118. int ntIndex2 = src.getItem(nameAndTypeIndex).copy(src, dest, map);
  1119. return copy2(dest, classIndex2, ntIndex2);
  1120. }
  1121. abstract protected int copy2(ConstPool dest, int cindex, int ntindex);
  1122. public void write(DataOutputStream out) throws IOException {
  1123. out.writeByte(getTag());
  1124. out.writeShort(classIndex);
  1125. out.writeShort(nameAndTypeIndex);
  1126. }
  1127. public void print(PrintWriter out) {
  1128. out.print(getTagName() + " #");
  1129. out.print(classIndex);
  1130. out.print(", name&type #");
  1131. out.println(nameAndTypeIndex);
  1132. }
  1133. public abstract String getTagName();
  1134. }
  1135. class FieldrefInfo extends MemberrefInfo {
  1136. static final int tag = 9;
  1137. public FieldrefInfo(int cindex, int ntindex) {
  1138. super(cindex, ntindex);
  1139. }
  1140. public FieldrefInfo(DataInputStream in) throws IOException {
  1141. super(in);
  1142. }
  1143. public int getTag() { return tag; }
  1144. public String getTagName() { return "Field"; }
  1145. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1146. return dest.addFieldrefInfo(cindex, ntindex);
  1147. }
  1148. }
  1149. class MethodrefInfo extends MemberrefInfo {
  1150. static final int tag = 10;
  1151. public MethodrefInfo(int cindex, int ntindex) {
  1152. super(cindex, ntindex);
  1153. }
  1154. public MethodrefInfo(DataInputStream in) throws IOException {
  1155. super(in);
  1156. }
  1157. public int getTag() { return tag; }
  1158. public String getTagName() { return "Method"; }
  1159. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1160. return dest.addMethodrefInfo(cindex, ntindex);
  1161. }
  1162. }
  1163. class InterfaceMethodrefInfo extends MemberrefInfo {
  1164. static final int tag = 11;
  1165. public InterfaceMethodrefInfo(int cindex, int ntindex) {
  1166. super(cindex, ntindex);
  1167. }
  1168. public InterfaceMethodrefInfo(DataInputStream in) throws IOException {
  1169. super(in);
  1170. }
  1171. public int getTag() { return tag; }
  1172. public String getTagName() { return "Interface"; }
  1173. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1174. return dest.addInterfaceMethodrefInfo(cindex, ntindex);
  1175. }
  1176. }
  1177. class StringInfo extends ConstInfo {
  1178. static final int tag = 8;
  1179. int string;
  1180. public StringInfo(int str) {
  1181. string = str;
  1182. }
  1183. public StringInfo(DataInputStream in) throws IOException {
  1184. string = in.readUnsignedShort();
  1185. }
  1186. public int getTag() { return tag; }
  1187. public int copy(ConstPool src, ConstPool dest, Map map) {
  1188. return dest.addStringInfo(src.getUtf8Info(string));
  1189. }
  1190. public void write(DataOutputStream out) throws IOException {
  1191. out.writeByte(tag);
  1192. out.writeShort(string);
  1193. }
  1194. public void print(PrintWriter out) {
  1195. out.print("String #");
  1196. out.println(string);
  1197. }
  1198. }
  1199. class IntegerInfo extends ConstInfo {
  1200. static final int tag = 3;
  1201. int value;
  1202. public IntegerInfo(int i) {
  1203. value = i;
  1204. }
  1205. public IntegerInfo(DataInputStream in) throws IOException {
  1206. value = in.readInt();
  1207. }
  1208. public int getTag() { return tag; }
  1209. public int copy(ConstPool src, ConstPool dest, Map map) {
  1210. return dest.addIntegerInfo(value);
  1211. }
  1212. public void write(DataOutputStream out) throws IOException {
  1213. out.writeByte(tag);
  1214. out.writeInt(value);
  1215. }
  1216. public void print(PrintWriter out) {
  1217. out.print("Integer ");
  1218. out.println(value);
  1219. }
  1220. }
  1221. class FloatInfo extends ConstInfo {
  1222. static final int tag = 4;
  1223. float value;
  1224. public FloatInfo(float f) {
  1225. value = f;
  1226. }
  1227. public FloatInfo(DataInputStream in) throws IOException {
  1228. value = in.readFloat();
  1229. }
  1230. public int getTag() { return tag; }
  1231. public int copy(ConstPool src, ConstPool dest, Map map) {
  1232. return dest.addFloatInfo(value);
  1233. }
  1234. public void write(DataOutputStream out) throws IOException {
  1235. out.writeByte(tag);
  1236. out.writeFloat(value);
  1237. }
  1238. public void print(PrintWriter out) {
  1239. out.print("Float ");
  1240. out.println(value);
  1241. }
  1242. }
  1243. class LongInfo extends ConstInfo {
  1244. static final int tag = 5;
  1245. long value;
  1246. public LongInfo(long l) {
  1247. value = l;
  1248. }
  1249. public LongInfo(DataInputStream in) throws IOException {
  1250. value = in.readLong();
  1251. }
  1252. public int getTag() { return tag; }
  1253. public int copy(ConstPool src, ConstPool dest, Map map) {
  1254. return dest.addLongInfo(value);
  1255. }
  1256. public void write(DataOutputStream out) throws IOException {
  1257. out.writeByte(tag);
  1258. out.writeLong(value);
  1259. }
  1260. public void print(PrintWriter out) {
  1261. out.print("Long ");
  1262. out.println(value);
  1263. }
  1264. }
  1265. class DoubleInfo extends ConstInfo {
  1266. static final int tag = 6;
  1267. double value;
  1268. public DoubleInfo(double d) {
  1269. value = d;
  1270. }
  1271. public DoubleInfo(DataInputStream in) throws IOException {
  1272. value = in.readDouble();
  1273. }
  1274. public int getTag() { return tag; }
  1275. public int copy(ConstPool src, ConstPool dest, Map map) {
  1276. return dest.addDoubleInfo(value);
  1277. }
  1278. public void write(DataOutputStream out) throws IOException {
  1279. out.writeByte(tag);
  1280. out.writeDouble(value);
  1281. }
  1282. public void print(PrintWriter out) {
  1283. out.print("Double ");
  1284. out.println(value);
  1285. }
  1286. }
  1287. class Utf8Info extends ConstInfo {
  1288. static final int tag = 1;
  1289. String string;
  1290. int index;
  1291. public Utf8Info(String utf8, int i) {
  1292. string = utf8;
  1293. index = i;
  1294. }
  1295. public Utf8Info(DataInputStream in, int i) throws IOException {
  1296. string = in.readUTF();
  1297. index = i;
  1298. }
  1299. public int getTag() { return tag; }
  1300. public int copy(ConstPool src, ConstPool dest, Map map) {
  1301. return dest.addUtf8Info(string);
  1302. }
  1303. public void write(DataOutputStream out) throws IOException {
  1304. out.writeByte(tag);
  1305. out.writeUTF(string);
  1306. }
  1307. public void print(PrintWriter out) {
  1308. out.print("UTF8 \"");
  1309. out.print(string);
  1310. out.println("\"");
  1311. }
  1312. }