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

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