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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.DataInputStream;
  18. import java.io.DataOutputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.PrintWriter;
  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import javassist.CtClass;
  27. /**
  28. * Constant pool table.
  29. */
  30. public final class ConstPool {
  31. LongVector items;
  32. int numOfItems;
  33. int thisClassInfo;
  34. HashMap itemsCache;
  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. * <code>CONSTANT_MethodHandle</code>
  82. */
  83. public static final int CONST_MethodHandle = MethodHandleInfo.tag;
  84. /**
  85. * <code>CONSTANT_MethodHandle</code>
  86. */
  87. public static final int CONST_MethodType = MethodTypeInfo.tag;
  88. /**
  89. * <code>CONSTANT_MethodHandle</code>
  90. */
  91. public static final int CONST_InvokeDynamic = InvokeDynamicInfo.tag;
  92. /**
  93. * <code>CONSTANT_Module</code>
  94. */
  95. public static final int CONST_Module = ModuleInfo.tag;
  96. /**
  97. * <code>CONSTANT_Package</code>
  98. */
  99. public static final int CONST_Package = PackageInfo.tag;
  100. /**
  101. * Represents the class using this constant pool table.
  102. */
  103. public static final CtClass THIS = null;
  104. /**
  105. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  106. */
  107. public static final int REF_getField = 1;
  108. /**
  109. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  110. */
  111. public static final int REF_getStatic = 2;
  112. /**
  113. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  114. */
  115. public static final int REF_putField = 3;
  116. /**
  117. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  118. */
  119. public static final int REF_putStatic = 4;
  120. /**
  121. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  122. */
  123. public static final int REF_invokeVirtual = 5;
  124. /**
  125. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  126. */
  127. public static final int REF_invokeStatic = 6;
  128. /**
  129. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  130. */
  131. public static final int REF_invokeSpecial = 7;
  132. /**
  133. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  134. */
  135. public static final int REF_newInvokeSpecial = 8;
  136. /**
  137. * <code>reference_kind</code> of <code>CONSTANT_MethodHandle_info</code>.
  138. */
  139. public static final int REF_invokeInterface = 9;
  140. /**
  141. * Constructs a constant pool table.
  142. *
  143. * @param thisclass the name of the class using this constant
  144. * pool table
  145. */
  146. public ConstPool(String thisclass) {
  147. items = new LongVector();
  148. itemsCache = null;
  149. numOfItems = 0;
  150. addItem0(null); // index 0 is reserved by the JVM.
  151. thisClassInfo = addClassInfo(thisclass);
  152. }
  153. /**
  154. * Constructs a constant pool table from the given byte stream.
  155. *
  156. * @param in byte stream.
  157. */
  158. public ConstPool(DataInputStream in) throws IOException {
  159. itemsCache = null;
  160. thisClassInfo = 0;
  161. /* read() initializes items and numOfItems, and do addItem(null).
  162. */
  163. read(in);
  164. }
  165. void prune() {
  166. itemsCache = null;
  167. }
  168. /**
  169. * Returns the number of entries in this table.
  170. */
  171. public int getSize() {
  172. return numOfItems;
  173. }
  174. /**
  175. * Returns the name of the class using this constant pool table.
  176. */
  177. public String getClassName() {
  178. return getClassInfo(thisClassInfo);
  179. }
  180. /**
  181. * Returns the index of <code>CONSTANT_Class_info</code> structure
  182. * specifying the class using this constant pool table.
  183. */
  184. public int getThisClassInfo() {
  185. return thisClassInfo;
  186. }
  187. void setThisClassInfo(int i) {
  188. thisClassInfo = i;
  189. }
  190. ConstInfo getItem(int n) {
  191. return items.elementAt(n);
  192. }
  193. /**
  194. * Returns the <code>tag</code> field of the constant pool table
  195. * entry at the given index.
  196. *
  197. * @return either <code>CONST_Class</code>, <code>CONST_Fieldref</code>,
  198. * <code>CONST_Methodref</code>, or ...
  199. */
  200. public int getTag(int index) {
  201. return getItem(index).getTag();
  202. }
  203. /**
  204. * Reads <code>CONSTANT_Class_info</code> structure
  205. * at the given index.
  206. *
  207. * @return a fully-qualified class or interface name specified
  208. * by <code>name_index</code>. If the type is an array
  209. * type, this method returns an encoded name like
  210. * <code>[Ljava.lang.Object;</code> (note that the separators
  211. * are not slashes but dots).
  212. * @see javassist.ClassPool#getCtClass(String)
  213. */
  214. public String getClassInfo(int index) {
  215. ClassInfo c = (ClassInfo)getItem(index);
  216. if (c == null)
  217. return null;
  218. else
  219. return Descriptor.toJavaName(getUtf8Info(c.name));
  220. }
  221. /**
  222. * Reads <code>CONSTANT_Class_info</code> structure
  223. * at the given index.
  224. *
  225. * @return the descriptor of the type specified
  226. * by <code>name_index</code>.
  227. * @see javassist.ClassPool#getCtClass(String)
  228. * @since 3.15
  229. */
  230. public String getClassInfoByDescriptor(int index) {
  231. ClassInfo c = (ClassInfo)getItem(index);
  232. if (c == null)
  233. return null;
  234. else {
  235. String className = getUtf8Info(c.name);
  236. if (className.charAt(0) == '[')
  237. return className;
  238. else
  239. return Descriptor.of(className);
  240. }
  241. }
  242. /**
  243. * Reads the <code>name_index</code> field of the
  244. * <code>CONSTANT_NameAndType_info</code> structure
  245. * at the given index.
  246. */
  247. public int getNameAndTypeName(int index) {
  248. NameAndTypeInfo ntinfo = (NameAndTypeInfo)getItem(index);
  249. return ntinfo.memberName;
  250. }
  251. /**
  252. * Reads the <code>descriptor_index</code> field of the
  253. * <code>CONSTANT_NameAndType_info</code> structure
  254. * at the given index.
  255. */
  256. public int getNameAndTypeDescriptor(int index) {
  257. NameAndTypeInfo ntinfo = (NameAndTypeInfo)getItem(index);
  258. return ntinfo.typeDescriptor;
  259. }
  260. /**
  261. * Reads the <code>class_index</code> field of the
  262. * <code>CONSTANT_Fieldref_info</code>,
  263. * <code>CONSTANT_Methodref_info</code>,
  264. * or <code>CONSTANT_Interfaceref_info</code>,
  265. * structure at the given index.
  266. *
  267. * @since 3.6
  268. */
  269. public int getMemberClass(int index) {
  270. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  271. return minfo.classIndex;
  272. }
  273. /**
  274. * Reads the <code>name_and_type_index</code> field of the
  275. * <code>CONSTANT_Fieldref_info</code>,
  276. * <code>CONSTANT_Methodref_info</code>,
  277. * or <code>CONSTANT_Interfaceref_info</code>,
  278. * structure at the given index.
  279. *
  280. * @since 3.6
  281. */
  282. public int getMemberNameAndType(int index) {
  283. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  284. return minfo.nameAndTypeIndex;
  285. }
  286. /**
  287. * Reads the <code>class_index</code> field of the
  288. * <code>CONSTANT_Fieldref_info</code> structure
  289. * at the given index.
  290. */
  291. public int getFieldrefClass(int index) {
  292. FieldrefInfo finfo = (FieldrefInfo)getItem(index);
  293. return finfo.classIndex;
  294. }
  295. /**
  296. * Reads the <code>class_index</code> field of the
  297. * <code>CONSTANT_Fieldref_info</code> structure
  298. * at the given index.
  299. *
  300. * @return the name of the class at that <code>class_index</code>.
  301. */
  302. public String getFieldrefClassName(int index) {
  303. FieldrefInfo f = (FieldrefInfo)getItem(index);
  304. if (f == null)
  305. return null;
  306. else
  307. return getClassInfo(f.classIndex);
  308. }
  309. /**
  310. * Reads the <code>name_and_type_index</code> field of the
  311. * <code>CONSTANT_Fieldref_info</code> structure
  312. * at the given index.
  313. */
  314. public int getFieldrefNameAndType(int index) {
  315. FieldrefInfo finfo = (FieldrefInfo)getItem(index);
  316. return finfo.nameAndTypeIndex;
  317. }
  318. /**
  319. * Reads the <code>name_index</code> field of the
  320. * <code>CONSTANT_NameAndType_info</code> structure
  321. * indirectly specified by the given index.
  322. *
  323. * @param index an index to a <code>CONSTANT_Fieldref_info</code>.
  324. * @return the name of the field.
  325. */
  326. public String getFieldrefName(int index) {
  327. FieldrefInfo f = (FieldrefInfo)getItem(index);
  328. if (f == null)
  329. return null;
  330. else {
  331. NameAndTypeInfo n = (NameAndTypeInfo)getItem(f.nameAndTypeIndex);
  332. if(n == null)
  333. return null;
  334. else
  335. return getUtf8Info(n.memberName);
  336. }
  337. }
  338. /**
  339. * Reads the <code>descriptor_index</code> field of the
  340. * <code>CONSTANT_NameAndType_info</code> structure
  341. * indirectly specified by the given index.
  342. *
  343. * @param index an index to a <code>CONSTANT_Fieldref_info</code>.
  344. * @return the type descriptor of the field.
  345. */
  346. public String getFieldrefType(int index) {
  347. FieldrefInfo f = (FieldrefInfo)getItem(index);
  348. if (f == null)
  349. return null;
  350. else {
  351. NameAndTypeInfo n = (NameAndTypeInfo)getItem(f.nameAndTypeIndex);
  352. if(n == null)
  353. return null;
  354. else
  355. return getUtf8Info(n.typeDescriptor);
  356. }
  357. }
  358. /**
  359. * Reads the <code>class_index</code> field of the
  360. * <code>CONSTANT_Methodref_info</code> structure
  361. * at the given index.
  362. */
  363. public int getMethodrefClass(int index) {
  364. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  365. return minfo.classIndex;
  366. }
  367. /**
  368. * Reads the <code>class_index</code> field of the
  369. * <code>CONSTANT_Methodref_info</code> structure
  370. * at the given index.
  371. *
  372. * @return the name of the class at that <code>class_index</code>.
  373. */
  374. public String getMethodrefClassName(int index) {
  375. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  376. if (minfo == null)
  377. return null;
  378. else
  379. return getClassInfo(minfo.classIndex);
  380. }
  381. /**
  382. * Reads the <code>name_and_type_index</code> field of the
  383. * <code>CONSTANT_Methodref_info</code> structure
  384. * at the given index.
  385. */
  386. public int getMethodrefNameAndType(int index) {
  387. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  388. return minfo.nameAndTypeIndex;
  389. }
  390. /**
  391. * Reads the <code>name_index</code> field of the
  392. * <code>CONSTANT_NameAndType_info</code> structure
  393. * indirectly specified by the given index.
  394. *
  395. * @param index an index to a <code>CONSTANT_Methodref_info</code>.
  396. * @return the name of the method.
  397. */
  398. public String getMethodrefName(int index) {
  399. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  400. if (minfo == null)
  401. return null;
  402. else {
  403. NameAndTypeInfo n
  404. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  405. if(n == null)
  406. return null;
  407. else
  408. return getUtf8Info(n.memberName);
  409. }
  410. }
  411. /**
  412. * Reads the <code>descriptor_index</code> field of the
  413. * <code>CONSTANT_NameAndType_info</code> structure
  414. * indirectly specified by the given index.
  415. *
  416. * @param index an index to a <code>CONSTANT_Methodref_info</code>.
  417. * @return the descriptor of the method.
  418. */
  419. public String getMethodrefType(int index) {
  420. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  421. if (minfo == null)
  422. return null;
  423. else {
  424. NameAndTypeInfo n
  425. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  426. if(n == null)
  427. return null;
  428. else
  429. return getUtf8Info(n.typeDescriptor);
  430. }
  431. }
  432. /**
  433. * Reads the <code>class_index</code> field of the
  434. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  435. * at the given index.
  436. */
  437. public int getInterfaceMethodrefClass(int index) {
  438. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  439. return minfo.classIndex;
  440. }
  441. /**
  442. * Reads the <code>class_index</code> field of the
  443. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  444. * at the given index.
  445. *
  446. * @return the name of the class at that <code>class_index</code>.
  447. */
  448. public String getInterfaceMethodrefClassName(int index) {
  449. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  450. return getClassInfo(minfo.classIndex);
  451. }
  452. /**
  453. * Reads the <code>name_and_type_index</code> field of the
  454. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  455. * at the given index.
  456. */
  457. public int getInterfaceMethodrefNameAndType(int index) {
  458. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  459. return minfo.nameAndTypeIndex;
  460. }
  461. /**
  462. * Reads the <code>name_index</code> field of the
  463. * <code>CONSTANT_NameAndType_info</code> structure
  464. * indirectly specified by the given index.
  465. *
  466. * @param index an index to
  467. * a <code>CONSTANT_InterfaceMethodref_info</code>.
  468. * @return the name of the method.
  469. */
  470. public String getInterfaceMethodrefName(int index) {
  471. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  472. if (minfo == null)
  473. return null;
  474. else {
  475. NameAndTypeInfo n
  476. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  477. if(n == null)
  478. return null;
  479. else
  480. return getUtf8Info(n.memberName);
  481. }
  482. }
  483. /**
  484. * Reads the <code>descriptor_index</code> field of the
  485. * <code>CONSTANT_NameAndType_info</code> structure
  486. * indirectly specified by the given index.
  487. *
  488. * @param index an index to
  489. * a <code>CONSTANT_InterfaceMethodref_info</code>.
  490. * @return the descriptor of the method.
  491. */
  492. public String getInterfaceMethodrefType(int index) {
  493. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  494. if (minfo == null)
  495. return null;
  496. else {
  497. NameAndTypeInfo n
  498. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  499. if(n == null)
  500. return null;
  501. else
  502. return getUtf8Info(n.typeDescriptor);
  503. }
  504. }
  505. /**
  506. * Reads <code>CONSTANT_Integer_info</code>, <code>_Float_info</code>,
  507. * <code>_Long_info</code>, <code>_Double_info</code>, or
  508. * <code>_String_info</code> structure.
  509. * These are used with the LDC instruction.
  510. *
  511. * @return a <code>String</code> value or a wrapped primitive-type
  512. * value.
  513. */
  514. public Object getLdcValue(int index) {
  515. ConstInfo constInfo = this.getItem(index);
  516. Object value = null;
  517. if (constInfo instanceof StringInfo)
  518. value = this.getStringInfo(index);
  519. else if (constInfo instanceof FloatInfo)
  520. value = Float.valueOf(getFloatInfo(index));
  521. else if (constInfo instanceof IntegerInfo)
  522. value = Integer.valueOf(getIntegerInfo(index));
  523. else if (constInfo instanceof LongInfo)
  524. value = Long.valueOf(getLongInfo(index));
  525. else if (constInfo instanceof DoubleInfo)
  526. value = Double.valueOf(getDoubleInfo(index));
  527. else
  528. value = null;
  529. return value;
  530. }
  531. /**
  532. * Reads <code>CONSTANT_Integer_info</code> structure
  533. * at the given index.
  534. *
  535. * @return the value specified by this entry.
  536. */
  537. public int getIntegerInfo(int index) {
  538. IntegerInfo i = (IntegerInfo)getItem(index);
  539. return i.value;
  540. }
  541. /**
  542. * Reads <code>CONSTANT_Float_info</code> structure
  543. * at the given index.
  544. *
  545. * @return the value specified by this entry.
  546. */
  547. public float getFloatInfo(int index) {
  548. FloatInfo i = (FloatInfo)getItem(index);
  549. return i.value;
  550. }
  551. /**
  552. * Reads <code>CONSTANT_Long_info</code> structure
  553. * at the given index.
  554. *
  555. * @return the value specified by this entry.
  556. */
  557. public long getLongInfo(int index) {
  558. LongInfo i = (LongInfo)getItem(index);
  559. return i.value;
  560. }
  561. /**
  562. * Reads <code>CONSTANT_Double_info</code> structure
  563. * at the given index.
  564. *
  565. * @return the value specified by this entry.
  566. */
  567. public double getDoubleInfo(int index) {
  568. DoubleInfo i = (DoubleInfo)getItem(index);
  569. return i.value;
  570. }
  571. /**
  572. * Reads <code>CONSTANT_String_info</code> structure
  573. * at the given index.
  574. *
  575. * @return the string specified by <code>string_index</code>.
  576. */
  577. public String getStringInfo(int index) {
  578. StringInfo si = (StringInfo)getItem(index);
  579. return getUtf8Info(si.string);
  580. }
  581. /**
  582. * Reads <code>CONSTANT_utf8_info</code> structure
  583. * at the given index.
  584. *
  585. * @return the string specified by this entry.
  586. */
  587. public String getUtf8Info(int index) {
  588. Utf8Info utf = (Utf8Info)getItem(index);
  589. return utf.string;
  590. }
  591. /**
  592. * Reads the <code>reference_kind</code> field of the
  593. * <code>CONSTANT_MethodHandle_info</code> structure
  594. * at the given index.
  595. *
  596. * @see #REF_getField
  597. * @see #REF_getStatic
  598. * @see #REF_invokeInterface
  599. * @see #REF_invokeSpecial
  600. * @see #REF_invokeStatic
  601. * @see #REF_invokeVirtual
  602. * @see #REF_newInvokeSpecial
  603. * @see #REF_putField
  604. * @see #REF_putStatic
  605. * @since 3.17
  606. */
  607. public int getMethodHandleKind(int index) {
  608. MethodHandleInfo mhinfo = (MethodHandleInfo)getItem(index);
  609. return mhinfo.refKind;
  610. }
  611. /**
  612. * Reads the <code>reference_index</code> field of the
  613. * <code>CONSTANT_MethodHandle_info</code> structure
  614. * at the given index.
  615. *
  616. * @since 3.17
  617. */
  618. public int getMethodHandleIndex(int index) {
  619. MethodHandleInfo mhinfo = (MethodHandleInfo)getItem(index);
  620. return mhinfo.refIndex;
  621. }
  622. /**
  623. * Reads the <code>descriptor_index</code> field of the
  624. * <code>CONSTANT_MethodType_info</code> structure
  625. * at the given index.
  626. *
  627. * @since 3.17
  628. */
  629. public int getMethodTypeInfo(int index) {
  630. MethodTypeInfo mtinfo = (MethodTypeInfo)getItem(index);
  631. return mtinfo.descriptor;
  632. }
  633. /**
  634. * Reads the <code>bootstrap_method_attr_index</code> field of the
  635. * <code>CONSTANT_InvokeDynamic_info</code> structure
  636. * at the given index.
  637. *
  638. * @since 3.17
  639. */
  640. public int getInvokeDynamicBootstrap(int index) {
  641. InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index);
  642. return iv.bootstrap;
  643. }
  644. /**
  645. * Reads the <code>name_and_type_index</code> field of the
  646. * <code>CONSTANT_InvokeDynamic_info</code> structure
  647. * at the given index.
  648. *
  649. * @since 3.17
  650. */
  651. public int getInvokeDynamicNameAndType(int index) {
  652. InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index);
  653. return iv.nameAndType;
  654. }
  655. /**
  656. * Reads the <code>descriptor_index</code> field of the
  657. * <code>CONSTANT_NameAndType_info</code> structure
  658. * indirectly specified by the given index.
  659. *
  660. * @param index an index to a <code>CONSTANT_InvokeDynamic_info</code>.
  661. * @return the descriptor of the method.
  662. * @since 3.17
  663. */
  664. public String getInvokeDynamicType(int index) {
  665. InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index);
  666. if (iv == null)
  667. return null;
  668. else {
  669. NameAndTypeInfo n = (NameAndTypeInfo)getItem(iv.nameAndType);
  670. if(n == null)
  671. return null;
  672. else
  673. return getUtf8Info(n.typeDescriptor);
  674. }
  675. }
  676. /**
  677. * Reads the <code>name_index</code> field of the
  678. * <code>CONSTANT_Module_info</code> structure at the given index.
  679. *
  680. * @return the module name at <code>name_index</code>.
  681. * @since 3.22
  682. */
  683. public String getModuleInfo(int index) {
  684. ModuleInfo mi = (ModuleInfo)getItem(index);
  685. return getUtf8Info(mi.name);
  686. }
  687. /**
  688. * Reads the <code>name_index</code> field of the
  689. * <code>CONSTANT_Package_info</code> structure at the given index.
  690. *
  691. * @return the package name at <code>name_index</code>. It is a slash-separated name
  692. * such as com/oracle/net.
  693. * @since 3.22
  694. */
  695. public String getPackageInfo(int index) {
  696. PackageInfo mi = (PackageInfo)getItem(index);
  697. return getUtf8Info(mi.name);
  698. }
  699. /**
  700. * Determines whether <code>CONSTANT_Methodref_info</code>
  701. * structure at the given index represents the constructor
  702. * of the given class.
  703. *
  704. * @return the <code>descriptor_index</code> specifying
  705. * the type descriptor of the that constructor.
  706. * If it is not that constructor,
  707. * <code>isConstructor()</code> returns 0.
  708. */
  709. public int isConstructor(String classname, int index) {
  710. return isMember(classname, MethodInfo.nameInit, index);
  711. }
  712. /**
  713. * Determines whether <code>CONSTANT_Methodref_info</code>,
  714. * <code>CONSTANT_Fieldref_info</code>, or
  715. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  716. * at the given index represents the member with the specified
  717. * name and declaring class.
  718. *
  719. * @param classname the class declaring the member
  720. * @param membername the member name
  721. * @param index the index into the constant pool table
  722. *
  723. * @return the <code>descriptor_index</code> specifying
  724. * the type descriptor of that member.
  725. * If it is not that member,
  726. * <code>isMember()</code> returns 0.
  727. */
  728. public int isMember(String classname, String membername, int index) {
  729. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  730. if (getClassInfo(minfo.classIndex).equals(classname)) {
  731. NameAndTypeInfo ntinfo
  732. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  733. if (getUtf8Info(ntinfo.memberName).equals(membername))
  734. return ntinfo.typeDescriptor;
  735. }
  736. return 0; // false
  737. }
  738. /**
  739. * Determines whether <code>CONSTANT_Methodref_info</code>,
  740. * <code>CONSTANT_Fieldref_info</code>, or
  741. * <code>CONSTANT_InterfaceMethodref_info</code> structure
  742. * at the given index has the name and the descriptor
  743. * given as the arguments.
  744. *
  745. * @param membername the member name
  746. * @param desc the descriptor of the member.
  747. * @param index the index into the constant pool table
  748. *
  749. * @return the name of the target class specified by
  750. * the <code>..._info</code> structure
  751. * at <code>index</code>.
  752. * Otherwise, null if that structure does not
  753. * match the given member name and descriptor.
  754. */
  755. public String eqMember(String membername, String desc, int index) {
  756. MemberrefInfo minfo = (MemberrefInfo)getItem(index);
  757. NameAndTypeInfo ntinfo
  758. = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex);
  759. if (getUtf8Info(ntinfo.memberName).equals(membername)
  760. && getUtf8Info(ntinfo.typeDescriptor).equals(desc))
  761. return getClassInfo(minfo.classIndex);
  762. else
  763. return null; // false
  764. }
  765. private int addItem0(ConstInfo info) {
  766. items.addElement(info);
  767. return numOfItems++;
  768. }
  769. private int addItem(ConstInfo info) {
  770. if (itemsCache == null)
  771. itemsCache = makeItemsCache(items);
  772. ConstInfo found = (ConstInfo)itemsCache.get(info);
  773. if (found != null)
  774. return found.index;
  775. else {
  776. items.addElement(info);
  777. itemsCache.put(info, info);
  778. return numOfItems++;
  779. }
  780. }
  781. /**
  782. * Copies the n-th item in this ConstPool object into the destination
  783. * ConstPool object.
  784. * The class names that the item refers to are renamed according
  785. * to the given map.
  786. *
  787. * @param n the <i>n</i>-th item
  788. * @param dest destination constant pool table
  789. * @param classnames the map or null.
  790. * @return the index of the copied item into the destination ClassPool.
  791. */
  792. public int copy(int n, ConstPool dest, Map classnames) {
  793. if (n == 0)
  794. return 0;
  795. ConstInfo info = getItem(n);
  796. return info.copy(this, dest, classnames);
  797. }
  798. int addConstInfoPadding() {
  799. return addItem0(new ConstInfoPadding(numOfItems));
  800. }
  801. /**
  802. * Adds a new <code>CONSTANT_Class_info</code> structure.
  803. *
  804. * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure
  805. * for storing the class name.
  806. *
  807. * @return the index of the added entry.
  808. */
  809. public int addClassInfo(CtClass c) {
  810. if (c == THIS)
  811. return thisClassInfo;
  812. else if (!c.isArray())
  813. return addClassInfo(c.getName());
  814. else {
  815. // an array type is recorded in the hashtable with
  816. // the key "[L<classname>;" instead of "<classname>".
  817. //
  818. // note: toJvmName(toJvmName(c)) is equal to toJvmName(c).
  819. return addClassInfo(Descriptor.toJvmName(c));
  820. }
  821. }
  822. /**
  823. * Adds a new <code>CONSTANT_Class_info</code> structure.
  824. *
  825. * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure
  826. * for storing the class name.
  827. *
  828. * @param qname a fully-qualified class name
  829. * (or the JVM-internal representation of that name).
  830. * @return the index of the added entry.
  831. */
  832. public int addClassInfo(String qname) {
  833. int utf8 = addUtf8Info(Descriptor.toJvmName(qname));
  834. return addItem(new ClassInfo(utf8, numOfItems));
  835. }
  836. /**
  837. * Adds a new <code>CONSTANT_NameAndType_info</code> structure.
  838. *
  839. * <p>This also adds <code>CONSTANT_Utf8_info</code> structures.
  840. *
  841. * @param name <code>name_index</code>
  842. * @param type <code>descriptor_index</code>
  843. * @return the index of the added entry.
  844. */
  845. public int addNameAndTypeInfo(String name, String type) {
  846. return addNameAndTypeInfo(addUtf8Info(name), addUtf8Info(type));
  847. }
  848. /**
  849. * Adds a new <code>CONSTANT_NameAndType_info</code> structure.
  850. *
  851. * @param name <code>name_index</code>
  852. * @param type <code>descriptor_index</code>
  853. * @return the index of the added entry.
  854. */
  855. public int addNameAndTypeInfo(int name, int type) {
  856. return addItem(new NameAndTypeInfo(name, type, numOfItems));
  857. }
  858. /**
  859. * Adds a new <code>CONSTANT_Fieldref_info</code> structure.
  860. *
  861. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  862. * structure.
  863. *
  864. * @param classInfo <code>class_index</code>
  865. * @param name <code>name_index</code>
  866. * of <code>CONSTANT_NameAndType_info</code>.
  867. * @param type <code>descriptor_index</code>
  868. * of <code>CONSTANT_NameAndType_info</code>.
  869. * @return the index of the added entry.
  870. */
  871. public int addFieldrefInfo(int classInfo, String name, String type) {
  872. int nt = addNameAndTypeInfo(name, type);
  873. return addFieldrefInfo(classInfo, nt);
  874. }
  875. /**
  876. * Adds a new <code>CONSTANT_Fieldref_info</code> structure.
  877. *
  878. * @param classInfo <code>class_index</code>
  879. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  880. * @return the index of the added entry.
  881. */
  882. public int addFieldrefInfo(int classInfo, int nameAndTypeInfo) {
  883. return addItem(new FieldrefInfo(classInfo, nameAndTypeInfo, numOfItems));
  884. }
  885. /**
  886. * Adds a new <code>CONSTANT_Methodref_info</code> structure.
  887. *
  888. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  889. * structure.
  890. *
  891. * @param classInfo <code>class_index</code>
  892. * @param name <code>name_index</code>
  893. * of <code>CONSTANT_NameAndType_info</code>.
  894. * @param type <code>descriptor_index</code>
  895. * of <code>CONSTANT_NameAndType_info</code>.
  896. * @return the index of the added entry.
  897. */
  898. public int addMethodrefInfo(int classInfo, String name, String type) {
  899. int nt = addNameAndTypeInfo(name, type);
  900. return addMethodrefInfo(classInfo, nt);
  901. }
  902. /**
  903. * Adds a new <code>CONSTANT_Methodref_info</code> structure.
  904. *
  905. * @param classInfo <code>class_index</code>
  906. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  907. * @return the index of the added entry.
  908. */
  909. public int addMethodrefInfo(int classInfo, int nameAndTypeInfo) {
  910. return addItem(new MethodrefInfo(classInfo, nameAndTypeInfo, numOfItems));
  911. }
  912. /**
  913. * Adds a new <code>CONSTANT_InterfaceMethodref_info</code>
  914. * structure.
  915. *
  916. * <p>This also adds a new <code>CONSTANT_NameAndType_info</code>
  917. * structure.
  918. *
  919. * @param classInfo <code>class_index</code>
  920. * @param name <code>name_index</code>
  921. * of <code>CONSTANT_NameAndType_info</code>.
  922. * @param type <code>descriptor_index</code>
  923. * of <code>CONSTANT_NameAndType_info</code>.
  924. * @return the index of the added entry.
  925. */
  926. public int addInterfaceMethodrefInfo(int classInfo, String name,
  927. String type) {
  928. int nt = addNameAndTypeInfo(name, type);
  929. return addInterfaceMethodrefInfo(classInfo, nt);
  930. }
  931. /**
  932. * Adds a new <code>CONSTANT_InterfaceMethodref_info</code>
  933. * structure.
  934. *
  935. * @param classInfo <code>class_index</code>
  936. * @param nameAndTypeInfo <code>name_and_type_index</code>.
  937. * @return the index of the added entry.
  938. */
  939. public int addInterfaceMethodrefInfo(int classInfo,
  940. int nameAndTypeInfo) {
  941. return addItem(new InterfaceMethodrefInfo(classInfo, nameAndTypeInfo,
  942. numOfItems));
  943. }
  944. /**
  945. * Adds a new <code>CONSTANT_String_info</code>
  946. * structure.
  947. *
  948. * <p>This also adds a new <code>CONSTANT_Utf8_info</code>
  949. * structure.
  950. *
  951. * @return the index of the added entry.
  952. */
  953. public int addStringInfo(String str) {
  954. int utf = addUtf8Info(str);
  955. return addItem(new StringInfo(utf, numOfItems));
  956. }
  957. /**
  958. * Adds a new <code>CONSTANT_Integer_info</code>
  959. * structure.
  960. *
  961. * @return the index of the added entry.
  962. */
  963. public int addIntegerInfo(int i) {
  964. return addItem(new IntegerInfo(i, numOfItems));
  965. }
  966. /**
  967. * Adds a new <code>CONSTANT_Float_info</code>
  968. * structure.
  969. *
  970. * @return the index of the added entry.
  971. */
  972. public int addFloatInfo(float f) {
  973. return addItem(new FloatInfo(f, numOfItems));
  974. }
  975. /**
  976. * Adds a new <code>CONSTANT_Long_info</code>
  977. * structure.
  978. *
  979. * @return the index of the added entry.
  980. */
  981. public int addLongInfo(long l) {
  982. int i = addItem(new LongInfo(l, numOfItems));
  983. if (i == numOfItems - 1) // if not existing
  984. addConstInfoPadding();
  985. return i;
  986. }
  987. /**
  988. * Adds a new <code>CONSTANT_Double_info</code>
  989. * structure.
  990. *
  991. * @return the index of the added entry.
  992. */
  993. public int addDoubleInfo(double d) {
  994. int i = addItem(new DoubleInfo(d, numOfItems));
  995. if (i == numOfItems - 1) // if not existing
  996. addConstInfoPadding();
  997. return i;
  998. }
  999. /**
  1000. * Adds a new <code>CONSTANT_Utf8_info</code>
  1001. * structure.
  1002. *
  1003. * @return the index of the added entry.
  1004. */
  1005. public int addUtf8Info(String utf8) {
  1006. return addItem(new Utf8Info(utf8, numOfItems));
  1007. }
  1008. /**
  1009. * Adds a new <code>CONSTANT_MethodHandle_info</code>
  1010. * structure.
  1011. *
  1012. * @param kind <code>reference_kind</code>
  1013. * such as {@link #REF_invokeStatic <code>REF_invokeStatic</code>}.
  1014. * @param index <code>reference_index</code>.
  1015. * @return the index of the added entry.
  1016. *
  1017. * @since 3.17
  1018. */
  1019. public int addMethodHandleInfo(int kind, int index) {
  1020. return addItem(new MethodHandleInfo(kind, index, numOfItems));
  1021. }
  1022. /**
  1023. * Adds a new <code>CONSTANT_MethodType_info</code>
  1024. * structure.
  1025. *
  1026. * @param desc <code>descriptor_index</code>.
  1027. * @return the index of the added entry.
  1028. *
  1029. * @since 3.17
  1030. */
  1031. public int addMethodTypeInfo(int desc) {
  1032. return addItem(new MethodTypeInfo(desc, numOfItems));
  1033. }
  1034. /**
  1035. * Adds a new <code>CONSTANT_InvokeDynamic_info</code>
  1036. * structure.
  1037. *
  1038. * @param bootstrap <code>bootstrap_method_attr_index</code>.
  1039. * @param nameAndType <code>name_and_type_index</code>.
  1040. * @return the index of the added entry.
  1041. *
  1042. * @since 3.17
  1043. */
  1044. public int addInvokeDynamicInfo(int bootstrap, int nameAndType) {
  1045. return addItem(new InvokeDynamicInfo(bootstrap, nameAndType, numOfItems));
  1046. }
  1047. /**
  1048. * Adds a new <code>CONSTANT_Module_info</code>
  1049. * @param nameIndex the index of the Utf8 entry.
  1050. * @return the index of the added entry.
  1051. * @since 3.22
  1052. */
  1053. public int addModuleInfo(int nameIndex) {
  1054. return addItem(new ModuleInfo(nameIndex, numOfItems));
  1055. }
  1056. /**
  1057. * Adds a new <code>CONSTANT_Package_info</code>
  1058. * @param nameIndex the index of the Utf8 entry.
  1059. * @return the index of the added entry.
  1060. * @since 3.22
  1061. */
  1062. public int addPackageInfo(int nameIndex) {
  1063. return addItem(new PackageInfo(nameIndex, numOfItems));
  1064. }
  1065. /**
  1066. * Get all the class names.
  1067. *
  1068. * @return a set of class names (<code>String</code> objects).
  1069. */
  1070. public Set getClassNames() {
  1071. HashSet result = new HashSet();
  1072. LongVector v = items;
  1073. int size = numOfItems;
  1074. for (int i = 1; i < size; ++i) {
  1075. String className = v.elementAt(i).getClassName(this);
  1076. if (className != null)
  1077. result.add(className);
  1078. }
  1079. return result;
  1080. }
  1081. /**
  1082. * Replaces all occurrences of a class name.
  1083. *
  1084. * @param oldName the replaced name (JVM-internal representation).
  1085. * @param newName the substituted name (JVM-internal representation).
  1086. */
  1087. public void renameClass(String oldName, String newName) {
  1088. LongVector v = items;
  1089. int size = numOfItems;
  1090. for (int i = 1; i < size; ++i) {
  1091. ConstInfo ci = v.elementAt(i);
  1092. ci.renameClass(this, oldName, newName, itemsCache);
  1093. }
  1094. }
  1095. /**
  1096. * Replaces all occurrences of class names.
  1097. *
  1098. * @param classnames specifies pairs of replaced and substituted
  1099. * name.
  1100. */
  1101. public void renameClass(Map classnames) {
  1102. LongVector v = items;
  1103. int size = numOfItems;
  1104. for (int i = 1; i < size; ++i) {
  1105. ConstInfo ci = v.elementAt(i);
  1106. ci.renameClass(this, classnames, itemsCache);
  1107. }
  1108. }
  1109. private void read(DataInputStream in) throws IOException {
  1110. int n = in.readUnsignedShort();
  1111. items = new LongVector(n);
  1112. numOfItems = 0;
  1113. addItem0(null); // index 0 is reserved by the JVM.
  1114. while (--n > 0) { // index 0 is reserved by JVM
  1115. int tag = readOne(in);
  1116. if ((tag == LongInfo.tag) || (tag == DoubleInfo.tag)) {
  1117. addConstInfoPadding();
  1118. --n;
  1119. }
  1120. }
  1121. }
  1122. private static HashMap makeItemsCache(LongVector items) {
  1123. HashMap cache = new HashMap();
  1124. int i = 1;
  1125. while (true) {
  1126. ConstInfo info = items.elementAt(i++);
  1127. if (info == null)
  1128. break;
  1129. else
  1130. cache.put(info, info);
  1131. }
  1132. return cache;
  1133. }
  1134. private int readOne(DataInputStream in) throws IOException {
  1135. ConstInfo info;
  1136. int tag = in.readUnsignedByte();
  1137. switch (tag) {
  1138. case Utf8Info.tag : // 1
  1139. info = new Utf8Info(in, numOfItems);
  1140. break;
  1141. case IntegerInfo.tag : // 3
  1142. info = new IntegerInfo(in, numOfItems);
  1143. break;
  1144. case FloatInfo.tag : // 4
  1145. info = new FloatInfo(in, numOfItems);
  1146. break;
  1147. case LongInfo.tag : // 5
  1148. info = new LongInfo(in, numOfItems);
  1149. break;
  1150. case DoubleInfo.tag : // 6
  1151. info = new DoubleInfo(in, numOfItems);
  1152. break;
  1153. case ClassInfo.tag : // 7
  1154. info = new ClassInfo(in, numOfItems);
  1155. break;
  1156. case StringInfo.tag : // 8
  1157. info = new StringInfo(in, numOfItems);
  1158. break;
  1159. case FieldrefInfo.tag : // 9
  1160. info = new FieldrefInfo(in, numOfItems);
  1161. break;
  1162. case MethodrefInfo.tag : // 10
  1163. info = new MethodrefInfo(in, numOfItems);
  1164. break;
  1165. case InterfaceMethodrefInfo.tag : // 11
  1166. info = new InterfaceMethodrefInfo(in, numOfItems);
  1167. break;
  1168. case NameAndTypeInfo.tag : // 12
  1169. info = new NameAndTypeInfo(in, numOfItems);
  1170. break;
  1171. case MethodHandleInfo.tag : // 15
  1172. info = new MethodHandleInfo(in, numOfItems);
  1173. break;
  1174. case MethodTypeInfo.tag : // 16
  1175. info = new MethodTypeInfo(in, numOfItems);
  1176. break;
  1177. case InvokeDynamicInfo.tag : // 18
  1178. info = new InvokeDynamicInfo(in, numOfItems);
  1179. break;
  1180. case ModuleInfo.tag : // 19
  1181. info = new ModuleInfo(in, numOfItems);
  1182. break;
  1183. case PackageInfo.tag : // 20
  1184. info = new PackageInfo(in, numOfItems);
  1185. break;
  1186. default :
  1187. throw new IOException("invalid constant type: " + tag + " at " + numOfItems);
  1188. }
  1189. addItem0(info);
  1190. return tag;
  1191. }
  1192. /**
  1193. * Writes the contents of the constant pool table.
  1194. */
  1195. public void write(DataOutputStream out) throws IOException {
  1196. out.writeShort(numOfItems);
  1197. LongVector v = items;
  1198. int size = numOfItems;
  1199. for (int i = 1; i < size; ++i)
  1200. v.elementAt(i).write(out);
  1201. }
  1202. /**
  1203. * Prints the contents of the constant pool table.
  1204. */
  1205. public void print() {
  1206. print(new PrintWriter(System.out, true));
  1207. }
  1208. /**
  1209. * Prints the contents of the constant pool table.
  1210. */
  1211. public void print(PrintWriter out) {
  1212. int size = numOfItems;
  1213. for (int i = 1; i < size; ++i) {
  1214. out.print(i);
  1215. out.print(" ");
  1216. items.elementAt(i).print(out);
  1217. }
  1218. }
  1219. }
  1220. abstract class ConstInfo {
  1221. int index;
  1222. public ConstInfo(int i) { index = i; }
  1223. public abstract int getTag();
  1224. public String getClassName(ConstPool cp) { return null; }
  1225. public void renameClass(ConstPool cp, String oldName, String newName, HashMap cache) {}
  1226. public void renameClass(ConstPool cp, Map classnames, HashMap cache) {}
  1227. public abstract int copy(ConstPool src, ConstPool dest, Map classnames);
  1228. // ** classnames is a mapping between JVM names.
  1229. public abstract void write(DataOutputStream out) throws IOException;
  1230. public abstract void print(PrintWriter out);
  1231. public String toString() {
  1232. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  1233. PrintWriter out = new PrintWriter(bout);
  1234. print(out);
  1235. return bout.toString();
  1236. }
  1237. }
  1238. /* padding following DoubleInfo or LongInfo.
  1239. */
  1240. class ConstInfoPadding extends ConstInfo {
  1241. public ConstInfoPadding(int i) { super(i); }
  1242. public int getTag() { return 0; }
  1243. public int copy(ConstPool src, ConstPool dest, Map map) {
  1244. return dest.addConstInfoPadding();
  1245. }
  1246. public void write(DataOutputStream out) throws IOException {}
  1247. public void print(PrintWriter out) {
  1248. out.println("padding");
  1249. }
  1250. }
  1251. class ClassInfo extends ConstInfo {
  1252. static final int tag = 7;
  1253. int name;
  1254. public ClassInfo(int className, int index) {
  1255. super(index);
  1256. name = className;
  1257. }
  1258. public ClassInfo(DataInputStream in, int index) throws IOException {
  1259. super(index);
  1260. name = in.readUnsignedShort();
  1261. }
  1262. public int hashCode() { return name; }
  1263. public boolean equals(Object obj) {
  1264. return obj instanceof ClassInfo && ((ClassInfo)obj).name == name;
  1265. }
  1266. public int getTag() { return tag; }
  1267. public String getClassName(ConstPool cp) {
  1268. return cp.getUtf8Info(name);
  1269. }
  1270. public void renameClass(ConstPool cp, String oldName, String newName, HashMap cache) {
  1271. String nameStr = cp.getUtf8Info(name);
  1272. String newNameStr = null;
  1273. if (nameStr.equals(oldName))
  1274. newNameStr = newName;
  1275. else if (nameStr.charAt(0) == '[') {
  1276. String s = Descriptor.rename(nameStr, oldName, newName);
  1277. if (nameStr != s)
  1278. newNameStr = s;
  1279. }
  1280. if (newNameStr != null)
  1281. if (cache == null)
  1282. name = cp.addUtf8Info(newNameStr);
  1283. else {
  1284. cache.remove(this);
  1285. name = cp.addUtf8Info(newNameStr);
  1286. cache.put(this, this);
  1287. }
  1288. }
  1289. public void renameClass(ConstPool cp, Map map, HashMap cache) {
  1290. String oldName = cp.getUtf8Info(name);
  1291. String newName = null;
  1292. if (oldName.charAt(0) == '[') {
  1293. String s = Descriptor.rename(oldName, map);
  1294. if (oldName != s)
  1295. newName = s;
  1296. }
  1297. else {
  1298. String s = (String)map.get(oldName);
  1299. if (s != null && !s.equals(oldName))
  1300. newName = s;
  1301. }
  1302. if (newName != null) {
  1303. if (cache == null)
  1304. name = cp.addUtf8Info(newName);
  1305. else {
  1306. cache.remove(this);
  1307. name = cp.addUtf8Info(newName);
  1308. cache.put(this, this);
  1309. }
  1310. }
  1311. }
  1312. public int copy(ConstPool src, ConstPool dest, Map map) {
  1313. String classname = src.getUtf8Info(name);
  1314. if (map != null) {
  1315. String newname = (String)map.get(classname);
  1316. if (newname != null)
  1317. classname = newname;
  1318. }
  1319. return dest.addClassInfo(classname);
  1320. }
  1321. public void write(DataOutputStream out) throws IOException {
  1322. out.writeByte(tag);
  1323. out.writeShort(name);
  1324. }
  1325. public void print(PrintWriter out) {
  1326. out.print("Class #");
  1327. out.println(name);
  1328. }
  1329. }
  1330. class NameAndTypeInfo extends ConstInfo {
  1331. static final int tag = 12;
  1332. int memberName;
  1333. int typeDescriptor;
  1334. public NameAndTypeInfo(int name, int type, int index) {
  1335. super(index);
  1336. memberName = name;
  1337. typeDescriptor = type;
  1338. }
  1339. public NameAndTypeInfo(DataInputStream in, int index) throws IOException {
  1340. super(index);
  1341. memberName = in.readUnsignedShort();
  1342. typeDescriptor = in.readUnsignedShort();
  1343. }
  1344. public int hashCode() { return (memberName << 16) ^ typeDescriptor; }
  1345. public boolean equals(Object obj) {
  1346. if (obj instanceof NameAndTypeInfo) {
  1347. NameAndTypeInfo nti = (NameAndTypeInfo)obj;
  1348. return nti.memberName == memberName && nti.typeDescriptor == typeDescriptor;
  1349. }
  1350. else
  1351. return false;
  1352. }
  1353. public int getTag() { return tag; }
  1354. public void renameClass(ConstPool cp, String oldName, String newName, HashMap cache) {
  1355. String type = cp.getUtf8Info(typeDescriptor);
  1356. String type2 = Descriptor.rename(type, oldName, newName);
  1357. if (type != type2)
  1358. if (cache == null)
  1359. typeDescriptor = cp.addUtf8Info(type2);
  1360. else {
  1361. cache.remove(this);
  1362. typeDescriptor = cp.addUtf8Info(type2);
  1363. cache.put(this, this);
  1364. }
  1365. }
  1366. public void renameClass(ConstPool cp, Map map, HashMap cache) {
  1367. String type = cp.getUtf8Info(typeDescriptor);
  1368. String type2 = Descriptor.rename(type, map);
  1369. if (type != type2)
  1370. if (cache == null)
  1371. typeDescriptor = cp.addUtf8Info(type2);
  1372. else {
  1373. cache.remove(this);
  1374. typeDescriptor = cp.addUtf8Info(type2);
  1375. cache.put(this, this);
  1376. }
  1377. }
  1378. public int copy(ConstPool src, ConstPool dest, Map map) {
  1379. String mname = src.getUtf8Info(memberName);
  1380. String tdesc = src.getUtf8Info(typeDescriptor);
  1381. tdesc = Descriptor.rename(tdesc, map);
  1382. return dest.addNameAndTypeInfo(dest.addUtf8Info(mname),
  1383. dest.addUtf8Info(tdesc));
  1384. }
  1385. public void write(DataOutputStream out) throws IOException {
  1386. out.writeByte(tag);
  1387. out.writeShort(memberName);
  1388. out.writeShort(typeDescriptor);
  1389. }
  1390. public void print(PrintWriter out) {
  1391. out.print("NameAndType #");
  1392. out.print(memberName);
  1393. out.print(", type #");
  1394. out.println(typeDescriptor);
  1395. }
  1396. }
  1397. abstract class MemberrefInfo extends ConstInfo {
  1398. int classIndex;
  1399. int nameAndTypeIndex;
  1400. public MemberrefInfo(int cindex, int ntindex, int thisIndex) {
  1401. super(thisIndex);
  1402. classIndex = cindex;
  1403. nameAndTypeIndex = ntindex;
  1404. }
  1405. public MemberrefInfo(DataInputStream in, int thisIndex) throws IOException {
  1406. super(thisIndex);
  1407. classIndex = in.readUnsignedShort();
  1408. nameAndTypeIndex = in.readUnsignedShort();
  1409. }
  1410. public int hashCode() { return (classIndex << 16) ^ nameAndTypeIndex; }
  1411. public boolean equals(Object obj) {
  1412. if (obj instanceof MemberrefInfo) {
  1413. MemberrefInfo mri = (MemberrefInfo)obj;
  1414. return mri.classIndex == classIndex && mri.nameAndTypeIndex == nameAndTypeIndex
  1415. && mri.getClass() == this.getClass();
  1416. }
  1417. else
  1418. return false;
  1419. }
  1420. public int copy(ConstPool src, ConstPool dest, Map map) {
  1421. int classIndex2 = src.getItem(classIndex).copy(src, dest, map);
  1422. int ntIndex2 = src.getItem(nameAndTypeIndex).copy(src, dest, map);
  1423. return copy2(dest, classIndex2, ntIndex2);
  1424. }
  1425. abstract protected int copy2(ConstPool dest, int cindex, int ntindex);
  1426. public void write(DataOutputStream out) throws IOException {
  1427. out.writeByte(getTag());
  1428. out.writeShort(classIndex);
  1429. out.writeShort(nameAndTypeIndex);
  1430. }
  1431. public void print(PrintWriter out) {
  1432. out.print(getTagName() + " #");
  1433. out.print(classIndex);
  1434. out.print(", name&type #");
  1435. out.println(nameAndTypeIndex);
  1436. }
  1437. public abstract String getTagName();
  1438. }
  1439. class FieldrefInfo extends MemberrefInfo {
  1440. static final int tag = 9;
  1441. public FieldrefInfo(int cindex, int ntindex, int thisIndex) {
  1442. super(cindex, ntindex, thisIndex);
  1443. }
  1444. public FieldrefInfo(DataInputStream in, int thisIndex) throws IOException {
  1445. super(in, thisIndex);
  1446. }
  1447. public int getTag() { return tag; }
  1448. public String getTagName() { return "Field"; }
  1449. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1450. return dest.addFieldrefInfo(cindex, ntindex);
  1451. }
  1452. }
  1453. class MethodrefInfo extends MemberrefInfo {
  1454. static final int tag = 10;
  1455. public MethodrefInfo(int cindex, int ntindex, int thisIndex) {
  1456. super(cindex, ntindex, thisIndex);
  1457. }
  1458. public MethodrefInfo(DataInputStream in, int thisIndex) throws IOException {
  1459. super(in, thisIndex);
  1460. }
  1461. public int getTag() { return tag; }
  1462. public String getTagName() { return "Method"; }
  1463. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1464. return dest.addMethodrefInfo(cindex, ntindex);
  1465. }
  1466. }
  1467. class InterfaceMethodrefInfo extends MemberrefInfo {
  1468. static final int tag = 11;
  1469. public InterfaceMethodrefInfo(int cindex, int ntindex, int thisIndex) {
  1470. super(cindex, ntindex, thisIndex);
  1471. }
  1472. public InterfaceMethodrefInfo(DataInputStream in, int thisIndex) throws IOException {
  1473. super(in, thisIndex);
  1474. }
  1475. public int getTag() { return tag; }
  1476. public String getTagName() { return "Interface"; }
  1477. protected int copy2(ConstPool dest, int cindex, int ntindex) {
  1478. return dest.addInterfaceMethodrefInfo(cindex, ntindex);
  1479. }
  1480. }
  1481. class StringInfo extends ConstInfo {
  1482. static final int tag = 8;
  1483. int string;
  1484. public StringInfo(int str, int index) {
  1485. super(index);
  1486. string = str;
  1487. }
  1488. public StringInfo(DataInputStream in, int index) throws IOException {
  1489. super(index);
  1490. string = in.readUnsignedShort();
  1491. }
  1492. public int hashCode() { return string; }
  1493. public boolean equals(Object obj) {
  1494. return obj instanceof StringInfo && ((StringInfo)obj).string == string;
  1495. }
  1496. public int getTag() { return tag; }
  1497. public int copy(ConstPool src, ConstPool dest, Map map) {
  1498. return dest.addStringInfo(src.getUtf8Info(string));
  1499. }
  1500. public void write(DataOutputStream out) throws IOException {
  1501. out.writeByte(tag);
  1502. out.writeShort(string);
  1503. }
  1504. public void print(PrintWriter out) {
  1505. out.print("String #");
  1506. out.println(string);
  1507. }
  1508. }
  1509. class IntegerInfo extends ConstInfo {
  1510. static final int tag = 3;
  1511. int value;
  1512. public IntegerInfo(int v, int index) {
  1513. super(index);
  1514. value = v;
  1515. }
  1516. public IntegerInfo(DataInputStream in, int index) throws IOException {
  1517. super(index);
  1518. value = in.readInt();
  1519. }
  1520. public int hashCode() { return value; }
  1521. public boolean equals(Object obj) {
  1522. return obj instanceof IntegerInfo && ((IntegerInfo)obj).value == value;
  1523. }
  1524. public int getTag() { return tag; }
  1525. public int copy(ConstPool src, ConstPool dest, Map map) {
  1526. return dest.addIntegerInfo(value);
  1527. }
  1528. public void write(DataOutputStream out) throws IOException {
  1529. out.writeByte(tag);
  1530. out.writeInt(value);
  1531. }
  1532. public void print(PrintWriter out) {
  1533. out.print("Integer ");
  1534. out.println(value);
  1535. }
  1536. }
  1537. class FloatInfo extends ConstInfo {
  1538. static final int tag = 4;
  1539. float value;
  1540. public FloatInfo(float f, int index) {
  1541. super(index);
  1542. value = f;
  1543. }
  1544. public FloatInfo(DataInputStream in, int index) throws IOException {
  1545. super(index);
  1546. value = in.readFloat();
  1547. }
  1548. public int hashCode() { return Float.floatToIntBits(value); }
  1549. public boolean equals(Object obj) {
  1550. return obj instanceof FloatInfo && ((FloatInfo)obj).value == value;
  1551. }
  1552. public int getTag() { return tag; }
  1553. public int copy(ConstPool src, ConstPool dest, Map map) {
  1554. return dest.addFloatInfo(value);
  1555. }
  1556. public void write(DataOutputStream out) throws IOException {
  1557. out.writeByte(tag);
  1558. out.writeFloat(value);
  1559. }
  1560. public void print(PrintWriter out) {
  1561. out.print("Float ");
  1562. out.println(value);
  1563. }
  1564. }
  1565. class LongInfo extends ConstInfo {
  1566. static final int tag = 5;
  1567. long value;
  1568. public LongInfo(long l, int index) {
  1569. super(index);
  1570. value = l;
  1571. }
  1572. public LongInfo(DataInputStream in, int index) throws IOException {
  1573. super(index);
  1574. value = in.readLong();
  1575. }
  1576. public int hashCode() { return (int)(value ^ (value >>> 32)); }
  1577. public boolean equals(Object obj) {
  1578. return obj instanceof LongInfo && ((LongInfo)obj).value == value;
  1579. }
  1580. public int getTag() { return tag; }
  1581. public int copy(ConstPool src, ConstPool dest, Map map) {
  1582. return dest.addLongInfo(value);
  1583. }
  1584. public void write(DataOutputStream out) throws IOException {
  1585. out.writeByte(tag);
  1586. out.writeLong(value);
  1587. }
  1588. public void print(PrintWriter out) {
  1589. out.print("Long ");
  1590. out.println(value);
  1591. }
  1592. }
  1593. class DoubleInfo extends ConstInfo {
  1594. static final int tag = 6;
  1595. double value;
  1596. public DoubleInfo(double d, int index) {
  1597. super(index);
  1598. value = d;
  1599. }
  1600. public DoubleInfo(DataInputStream in, int index) throws IOException {
  1601. super(index);
  1602. value = in.readDouble();
  1603. }
  1604. public int hashCode() {
  1605. long v = Double.doubleToLongBits(value);
  1606. return (int)(v ^ (v >>> 32));
  1607. }
  1608. public boolean equals(Object obj) {
  1609. return obj instanceof DoubleInfo && ((DoubleInfo)obj).value == value;
  1610. }
  1611. public int getTag() { return tag; }
  1612. public int copy(ConstPool src, ConstPool dest, Map map) {
  1613. return dest.addDoubleInfo(value);
  1614. }
  1615. public void write(DataOutputStream out) throws IOException {
  1616. out.writeByte(tag);
  1617. out.writeDouble(value);
  1618. }
  1619. public void print(PrintWriter out) {
  1620. out.print("Double ");
  1621. out.println(value);
  1622. }
  1623. }
  1624. class Utf8Info extends ConstInfo {
  1625. static final int tag = 1;
  1626. String string;
  1627. public Utf8Info(String utf8, int index) {
  1628. super(index);
  1629. string = utf8;
  1630. }
  1631. public Utf8Info(DataInputStream in, int index) throws IOException {
  1632. super(index);
  1633. string = in.readUTF();
  1634. }
  1635. public int hashCode() {
  1636. return string.hashCode();
  1637. }
  1638. public boolean equals(Object obj) {
  1639. return obj instanceof Utf8Info && ((Utf8Info)obj).string.equals(string);
  1640. }
  1641. public int getTag() { return tag; }
  1642. public int copy(ConstPool src, ConstPool dest, Map map) {
  1643. return dest.addUtf8Info(string);
  1644. }
  1645. public void write(DataOutputStream out) throws IOException {
  1646. out.writeByte(tag);
  1647. out.writeUTF(string);
  1648. }
  1649. public void print(PrintWriter out) {
  1650. out.print("UTF8 \"");
  1651. out.print(string);
  1652. out.println("\"");
  1653. }
  1654. }
  1655. class MethodHandleInfo extends ConstInfo {
  1656. static final int tag = 15;
  1657. int refKind, refIndex;
  1658. public MethodHandleInfo(int kind, int referenceIndex, int index) {
  1659. super(index);
  1660. refKind = kind;
  1661. refIndex = referenceIndex;
  1662. }
  1663. public MethodHandleInfo(DataInputStream in, int index) throws IOException {
  1664. super(index);
  1665. refKind = in.readUnsignedByte();
  1666. refIndex = in.readUnsignedShort();
  1667. }
  1668. public int hashCode() { return (refKind << 16) ^ refIndex; }
  1669. public boolean equals(Object obj) {
  1670. if (obj instanceof MethodHandleInfo) {
  1671. MethodHandleInfo mh = (MethodHandleInfo)obj;
  1672. return mh.refKind == refKind && mh.refIndex == refIndex;
  1673. }
  1674. else
  1675. return false;
  1676. }
  1677. public int getTag() { return tag; }
  1678. public int copy(ConstPool src, ConstPool dest, Map map) {
  1679. return dest.addMethodHandleInfo(refKind,
  1680. src.getItem(refIndex).copy(src, dest, map));
  1681. }
  1682. public void write(DataOutputStream out) throws IOException {
  1683. out.writeByte(tag);
  1684. out.writeByte(refKind);
  1685. out.writeShort(refIndex);
  1686. }
  1687. public void print(PrintWriter out) {
  1688. out.print("MethodHandle #");
  1689. out.print(refKind);
  1690. out.print(", index #");
  1691. out.println(refIndex);
  1692. }
  1693. }
  1694. class MethodTypeInfo extends ConstInfo {
  1695. static final int tag = 16;
  1696. int descriptor;
  1697. public MethodTypeInfo(int desc, int index) {
  1698. super(index);
  1699. descriptor = desc;
  1700. }
  1701. public MethodTypeInfo(DataInputStream in, int index) throws IOException {
  1702. super(index);
  1703. descriptor = in.readUnsignedShort();
  1704. }
  1705. public int hashCode() { return descriptor; }
  1706. public boolean equals(Object obj) {
  1707. if (obj instanceof MethodTypeInfo)
  1708. return ((MethodTypeInfo)obj).descriptor == descriptor;
  1709. else
  1710. return false;
  1711. }
  1712. public int getTag() { return tag; }
  1713. public void renameClass(ConstPool cp, String oldName, String newName, HashMap cache) {
  1714. String desc = cp.getUtf8Info(descriptor);
  1715. String desc2 = Descriptor.rename(desc, oldName, newName);
  1716. if (desc != desc2)
  1717. if (cache == null)
  1718. descriptor = cp.addUtf8Info(desc2);
  1719. else {
  1720. cache.remove(this);
  1721. descriptor = cp.addUtf8Info(desc2);
  1722. cache.put(this, this);
  1723. }
  1724. }
  1725. public void renameClass(ConstPool cp, Map map, HashMap cache) {
  1726. String desc = cp.getUtf8Info(descriptor);
  1727. String desc2 = Descriptor.rename(desc, map);
  1728. if (desc != desc2)
  1729. if (cache == null)
  1730. descriptor = cp.addUtf8Info(desc2);
  1731. else {
  1732. cache.remove(this);
  1733. descriptor = cp.addUtf8Info(desc2);
  1734. cache.put(this, this);
  1735. }
  1736. }
  1737. public int copy(ConstPool src, ConstPool dest, Map map) {
  1738. String desc = src.getUtf8Info(descriptor);
  1739. desc = Descriptor.rename(desc, map);
  1740. return dest.addMethodTypeInfo(dest.addUtf8Info(desc));
  1741. }
  1742. public void write(DataOutputStream out) throws IOException {
  1743. out.writeByte(tag);
  1744. out.writeShort(descriptor);
  1745. }
  1746. public void print(PrintWriter out) {
  1747. out.print("MethodType #");
  1748. out.println(descriptor);
  1749. }
  1750. }
  1751. class InvokeDynamicInfo extends ConstInfo {
  1752. static final int tag = 18;
  1753. int bootstrap, nameAndType;
  1754. public InvokeDynamicInfo(int bootstrapMethod, int ntIndex, int index) {
  1755. super(index);
  1756. bootstrap = bootstrapMethod;
  1757. nameAndType = ntIndex;
  1758. }
  1759. public InvokeDynamicInfo(DataInputStream in, int index) throws IOException {
  1760. super(index);
  1761. bootstrap = in.readUnsignedShort();
  1762. nameAndType = in.readUnsignedShort();
  1763. }
  1764. public int hashCode() { return (bootstrap << 16) ^ nameAndType; }
  1765. public boolean equals(Object obj) {
  1766. if (obj instanceof InvokeDynamicInfo) {
  1767. InvokeDynamicInfo iv = (InvokeDynamicInfo)obj;
  1768. return iv.bootstrap == bootstrap && iv.nameAndType == nameAndType;
  1769. }
  1770. else
  1771. return false;
  1772. }
  1773. public int getTag() { return tag; }
  1774. public int copy(ConstPool src, ConstPool dest, Map map) {
  1775. return dest.addInvokeDynamicInfo(bootstrap,
  1776. src.getItem(nameAndType).copy(src, dest, map));
  1777. }
  1778. public void write(DataOutputStream out) throws IOException {
  1779. out.writeByte(tag);
  1780. out.writeShort(bootstrap);
  1781. out.writeShort(nameAndType);
  1782. }
  1783. public void print(PrintWriter out) {
  1784. out.print("InvokeDynamic #");
  1785. out.print(bootstrap);
  1786. out.print(", name&type #");
  1787. out.println(nameAndType);
  1788. }
  1789. }
  1790. class ModuleInfo extends ConstInfo {
  1791. static final int tag = 19;
  1792. int name;
  1793. public ModuleInfo(int moduleName, int index) {
  1794. super(index);
  1795. name = moduleName;
  1796. }
  1797. public ModuleInfo(DataInputStream in, int index) throws IOException {
  1798. super(index);
  1799. name = in.readUnsignedShort();
  1800. }
  1801. public int hashCode() { return name; }
  1802. public boolean equals(Object obj) {
  1803. return obj instanceof ModuleInfo && ((ModuleInfo)obj).name == name;
  1804. }
  1805. public int getTag() { return tag; }
  1806. public String getModuleName(ConstPool cp) {
  1807. return cp.getUtf8Info(name);
  1808. }
  1809. public int copy(ConstPool src, ConstPool dest, Map map) {
  1810. String moduleName = src.getUtf8Info(name);
  1811. int newName = dest.addUtf8Info(moduleName);
  1812. return dest.addModuleInfo(newName);
  1813. }
  1814. public void write(DataOutputStream out) throws IOException {
  1815. out.writeByte(tag);
  1816. out.writeShort(name);
  1817. }
  1818. public void print(PrintWriter out) {
  1819. out.print("Module #");
  1820. out.println(name);
  1821. }
  1822. }
  1823. class PackageInfo extends ConstInfo {
  1824. static final int tag = 20;
  1825. int name;
  1826. public PackageInfo(int moduleName, int index) {
  1827. super(index);
  1828. name = moduleName;
  1829. }
  1830. public PackageInfo(DataInputStream in, int index) throws IOException {
  1831. super(index);
  1832. name = in.readUnsignedShort();
  1833. }
  1834. public int hashCode() { return name; }
  1835. public boolean equals(Object obj) {
  1836. return obj instanceof PackageInfo && ((PackageInfo)obj).name == name;
  1837. }
  1838. public int getTag() { return tag; }
  1839. public String getPackageName(ConstPool cp) {
  1840. return cp.getUtf8Info(name);
  1841. }
  1842. public int copy(ConstPool src, ConstPool dest, Map map) {
  1843. String packageName = src.getUtf8Info(name);
  1844. int newName = dest.addUtf8Info(packageName);
  1845. return dest.addModuleInfo(newName);
  1846. }
  1847. public void write(DataOutputStream out) throws IOException {
  1848. out.writeByte(tag);
  1849. out.writeShort(name);
  1850. }
  1851. public void print(PrintWriter out) {
  1852. out.print("Package #");
  1853. out.println(name);
  1854. }
  1855. }