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

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