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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569
  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. if (numOfItems < 0 || Short.MAX_VALUE < numOfItems)
  1312. throw new IOException("too many constant pool items " + numOfItems);
  1313. out.writeShort(numOfItems);
  1314. LongVector v = items;
  1315. int size = numOfItems;
  1316. for (int i = 1; i < size; ++i)
  1317. v.elementAt(i).write(out);
  1318. }
  1319. /**
  1320. * Prints the contents of the constant pool table.
  1321. */
  1322. public void print()
  1323. {
  1324. print(new PrintWriter(System.out, true));
  1325. }
  1326. /**
  1327. * Prints the contents of the constant pool table.
  1328. */
  1329. public void print(PrintWriter out)
  1330. {
  1331. int size = numOfItems;
  1332. for (int i = 1; i < size; ++i) {
  1333. out.print(i);
  1334. out.print(" ");
  1335. items.elementAt(i).print(out);
  1336. }
  1337. }
  1338. }
  1339. abstract class ConstInfo
  1340. {
  1341. int index;
  1342. public ConstInfo(int i) { index = i; }
  1343. public abstract int getTag();
  1344. public String getClassName(ConstPool cp) { return null; }
  1345. public void renameClass(ConstPool cp, String oldName, String newName,
  1346. Map<ConstInfo,ConstInfo> cache) {}
  1347. public void renameClass(ConstPool cp, Map<String,String> classnames,
  1348. Map<ConstInfo,ConstInfo> cache) {}
  1349. public abstract int copy(ConstPool src, ConstPool dest,
  1350. Map<String, String> classnames);
  1351. // ** classnames is a mapping between JVM names.
  1352. public abstract void write(DataOutputStream out) throws IOException;
  1353. public abstract void print(PrintWriter out);
  1354. @Override
  1355. public String toString() {
  1356. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  1357. PrintWriter out = new PrintWriter(bout);
  1358. print(out);
  1359. return bout.toString();
  1360. }
  1361. }
  1362. /* padding following DoubleInfo or LongInfo.
  1363. */
  1364. class ConstInfoPadding extends ConstInfo
  1365. {
  1366. public ConstInfoPadding(int i) { super(i); }
  1367. @Override
  1368. public int getTag() { return 0; }
  1369. @Override
  1370. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1371. {
  1372. return dest.addConstInfoPadding();
  1373. }
  1374. @Override
  1375. public void write(DataOutputStream out) throws IOException {}
  1376. @Override
  1377. public void print(PrintWriter out)
  1378. {
  1379. out.println("padding");
  1380. }
  1381. }
  1382. class ClassInfo extends ConstInfo
  1383. {
  1384. static final int tag = 7;
  1385. int name;
  1386. public ClassInfo(int className, int index)
  1387. {
  1388. super(index);
  1389. name = className;
  1390. }
  1391. public ClassInfo(DataInputStream in, int index) throws IOException
  1392. {
  1393. super(index);
  1394. name = in.readUnsignedShort();
  1395. }
  1396. @Override
  1397. public int hashCode() { return name; }
  1398. @Override
  1399. public boolean equals(Object obj)
  1400. {
  1401. return obj instanceof ClassInfo && ((ClassInfo)obj).name == name;
  1402. }
  1403. @Override
  1404. public int getTag() { return tag; }
  1405. @Override
  1406. public String getClassName(ConstPool cp)
  1407. {
  1408. return cp.getUtf8Info(name);
  1409. }
  1410. @Override
  1411. public void renameClass(ConstPool cp, String oldName, String newName,
  1412. Map<ConstInfo,ConstInfo> cache)
  1413. {
  1414. String nameStr = cp.getUtf8Info(name);
  1415. String newNameStr = null;
  1416. if (nameStr.equals(oldName))
  1417. newNameStr = newName;
  1418. else if (nameStr.charAt(0) == '[') {
  1419. String s = Descriptor.rename(nameStr, oldName, newName);
  1420. if (nameStr != s)
  1421. newNameStr = s;
  1422. }
  1423. if (newNameStr != null)
  1424. if (cache == null)
  1425. name = cp.addUtf8Info(newNameStr);
  1426. else {
  1427. cache.remove(this);
  1428. name = cp.addUtf8Info(newNameStr);
  1429. cache.put(this, this);
  1430. }
  1431. }
  1432. @Override
  1433. public void renameClass(ConstPool cp, Map<String,String> map,
  1434. Map<ConstInfo,ConstInfo> cache)
  1435. {
  1436. String oldName = cp.getUtf8Info(name);
  1437. String newName = null;
  1438. if (oldName.charAt(0) == '[') {
  1439. String s = Descriptor.rename(oldName, map);
  1440. if (oldName != s)
  1441. newName = s;
  1442. }
  1443. else {
  1444. String s = map.get(oldName);
  1445. if (s != null && !s.equals(oldName))
  1446. newName = s;
  1447. }
  1448. if (newName != null) {
  1449. if (cache == null)
  1450. name = cp.addUtf8Info(newName);
  1451. else {
  1452. cache.remove(this);
  1453. name = cp.addUtf8Info(newName);
  1454. cache.put(this, this);
  1455. }
  1456. }
  1457. }
  1458. @Override
  1459. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1460. {
  1461. String classname = src.getUtf8Info(name);
  1462. if (map != null) {
  1463. String newname = map.get(classname);
  1464. if (newname != null)
  1465. classname = newname;
  1466. }
  1467. return dest.addClassInfo(classname);
  1468. }
  1469. @Override
  1470. public void write(DataOutputStream out) throws IOException
  1471. {
  1472. out.writeByte(tag);
  1473. out.writeShort(name);
  1474. }
  1475. @Override
  1476. public void print(PrintWriter out)
  1477. {
  1478. out.print("Class #");
  1479. out.println(name);
  1480. }
  1481. }
  1482. class NameAndTypeInfo extends ConstInfo
  1483. {
  1484. static final int tag = 12;
  1485. int memberName;
  1486. int typeDescriptor;
  1487. public NameAndTypeInfo(int name, int type, int index)
  1488. {
  1489. super(index);
  1490. memberName = name;
  1491. typeDescriptor = type;
  1492. }
  1493. public NameAndTypeInfo(DataInputStream in, int index) throws IOException
  1494. {
  1495. super(index);
  1496. memberName = in.readUnsignedShort();
  1497. typeDescriptor = in.readUnsignedShort();
  1498. }
  1499. @Override
  1500. public int hashCode() { return (memberName << 16) ^ typeDescriptor; }
  1501. @Override
  1502. public boolean equals(Object obj)
  1503. {
  1504. if (obj instanceof NameAndTypeInfo) {
  1505. NameAndTypeInfo nti = (NameAndTypeInfo)obj;
  1506. return nti.memberName == memberName
  1507. && nti.typeDescriptor == typeDescriptor;
  1508. }
  1509. return false;
  1510. }
  1511. @Override
  1512. public int getTag() { return tag; }
  1513. @Override
  1514. public void renameClass(ConstPool cp, String oldName, String newName,
  1515. Map<ConstInfo,ConstInfo> cache)
  1516. {
  1517. String type = cp.getUtf8Info(typeDescriptor);
  1518. String type2 = Descriptor.rename(type, oldName, newName);
  1519. if (type != type2)
  1520. if (cache == null)
  1521. typeDescriptor = cp.addUtf8Info(type2);
  1522. else {
  1523. cache.remove(this);
  1524. typeDescriptor = cp.addUtf8Info(type2);
  1525. cache.put(this, this);
  1526. }
  1527. }
  1528. @Override
  1529. public void renameClass(ConstPool cp, Map<String,String> map,
  1530. Map<ConstInfo,ConstInfo> cache)
  1531. {
  1532. String type = cp.getUtf8Info(typeDescriptor);
  1533. String type2 = Descriptor.rename(type, map);
  1534. if (type != type2)
  1535. if (cache == null)
  1536. typeDescriptor = cp.addUtf8Info(type2);
  1537. else {
  1538. cache.remove(this);
  1539. typeDescriptor = cp.addUtf8Info(type2);
  1540. cache.put(this, this);
  1541. }
  1542. }
  1543. @Override
  1544. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1545. {
  1546. String mname = src.getUtf8Info(memberName);
  1547. String tdesc = src.getUtf8Info(typeDescriptor);
  1548. tdesc = Descriptor.rename(tdesc, map);
  1549. return dest.addNameAndTypeInfo(dest.addUtf8Info(mname),
  1550. dest.addUtf8Info(tdesc));
  1551. }
  1552. @Override
  1553. public void write(DataOutputStream out) throws IOException {
  1554. out.writeByte(tag);
  1555. out.writeShort(memberName);
  1556. out.writeShort(typeDescriptor);
  1557. }
  1558. @Override
  1559. public void print(PrintWriter out) {
  1560. out.print("NameAndType #");
  1561. out.print(memberName);
  1562. out.print(", type #");
  1563. out.println(typeDescriptor);
  1564. }
  1565. }
  1566. abstract class MemberrefInfo extends ConstInfo
  1567. {
  1568. int classIndex;
  1569. int nameAndTypeIndex;
  1570. public MemberrefInfo(int cindex, int ntindex, int thisIndex)
  1571. {
  1572. super(thisIndex);
  1573. classIndex = cindex;
  1574. nameAndTypeIndex = ntindex;
  1575. }
  1576. public MemberrefInfo(DataInputStream in, int thisIndex)
  1577. throws IOException
  1578. {
  1579. super(thisIndex);
  1580. classIndex = in.readUnsignedShort();
  1581. nameAndTypeIndex = in.readUnsignedShort();
  1582. }
  1583. @Override
  1584. public int hashCode() { return (classIndex << 16) ^ nameAndTypeIndex; }
  1585. @Override
  1586. public boolean equals(Object obj) {
  1587. if (obj instanceof MemberrefInfo) {
  1588. MemberrefInfo mri = (MemberrefInfo)obj;
  1589. return mri.classIndex == classIndex
  1590. && mri.nameAndTypeIndex == nameAndTypeIndex
  1591. && mri.getClass() == this.getClass();
  1592. }
  1593. return false;
  1594. }
  1595. @Override
  1596. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1597. {
  1598. int classIndex2 = src.getItem(classIndex).copy(src, dest, map);
  1599. int ntIndex2 = src.getItem(nameAndTypeIndex).copy(src, dest, map);
  1600. return copy2(dest, classIndex2, ntIndex2);
  1601. }
  1602. abstract protected int copy2(ConstPool dest, int cindex, int ntindex);
  1603. @Override
  1604. public void write(DataOutputStream out) throws IOException
  1605. {
  1606. out.writeByte(getTag());
  1607. out.writeShort(classIndex);
  1608. out.writeShort(nameAndTypeIndex);
  1609. }
  1610. @Override
  1611. public void print(PrintWriter out)
  1612. {
  1613. out.print(getTagName() + " #");
  1614. out.print(classIndex);
  1615. out.print(", name&type #");
  1616. out.println(nameAndTypeIndex);
  1617. }
  1618. public abstract String getTagName();
  1619. }
  1620. class FieldrefInfo extends MemberrefInfo
  1621. {
  1622. static final int tag = 9;
  1623. public FieldrefInfo(int cindex, int ntindex, int thisIndex)
  1624. {
  1625. super(cindex, ntindex, thisIndex);
  1626. }
  1627. public FieldrefInfo(DataInputStream in, int thisIndex)
  1628. throws IOException
  1629. {
  1630. super(in, thisIndex);
  1631. }
  1632. @Override
  1633. public int getTag() { return tag; }
  1634. @Override
  1635. public String getTagName() { return "Field"; }
  1636. @Override
  1637. protected int copy2(ConstPool dest, int cindex, int ntindex)
  1638. {
  1639. return dest.addFieldrefInfo(cindex, ntindex);
  1640. }
  1641. }
  1642. class MethodrefInfo extends MemberrefInfo
  1643. {
  1644. static final int tag = 10;
  1645. public MethodrefInfo(int cindex, int ntindex, int thisIndex)
  1646. {
  1647. super(cindex, ntindex, thisIndex);
  1648. }
  1649. public MethodrefInfo(DataInputStream in, int thisIndex)
  1650. throws IOException
  1651. {
  1652. super(in, thisIndex);
  1653. }
  1654. @Override
  1655. public int getTag() { return tag; }
  1656. @Override
  1657. public String getTagName() { return "Method"; }
  1658. @Override
  1659. protected int copy2(ConstPool dest, int cindex, int ntindex)
  1660. {
  1661. return dest.addMethodrefInfo(cindex, ntindex);
  1662. }
  1663. }
  1664. class InterfaceMethodrefInfo extends MemberrefInfo
  1665. {
  1666. static final int tag = 11;
  1667. public InterfaceMethodrefInfo(int cindex, int ntindex, int thisIndex)
  1668. {
  1669. super(cindex, ntindex, thisIndex);
  1670. }
  1671. public InterfaceMethodrefInfo(DataInputStream in, int thisIndex)
  1672. throws IOException
  1673. {
  1674. super(in, thisIndex);
  1675. }
  1676. @Override
  1677. public int getTag() { return tag; }
  1678. @Override
  1679. public String getTagName() { return "Interface"; }
  1680. @Override
  1681. protected int copy2(ConstPool dest, int cindex, int ntindex)
  1682. {
  1683. return dest.addInterfaceMethodrefInfo(cindex, ntindex);
  1684. }
  1685. }
  1686. class StringInfo extends ConstInfo
  1687. {
  1688. static final int tag = 8;
  1689. int string;
  1690. public StringInfo(int str, int index)
  1691. {
  1692. super(index);
  1693. string = str;
  1694. }
  1695. public StringInfo(DataInputStream in, int index) throws IOException
  1696. {
  1697. super(index);
  1698. string = in.readUnsignedShort();
  1699. }
  1700. @Override
  1701. public int hashCode() { return string; }
  1702. @Override
  1703. public boolean equals(Object obj)
  1704. {
  1705. return obj instanceof StringInfo && ((StringInfo)obj).string == string;
  1706. }
  1707. @Override
  1708. public int getTag() { return tag; }
  1709. @Override
  1710. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1711. {
  1712. return dest.addStringInfo(src.getUtf8Info(string));
  1713. }
  1714. @Override
  1715. public void write(DataOutputStream out) throws IOException
  1716. {
  1717. out.writeByte(tag);
  1718. out.writeShort(string);
  1719. }
  1720. @Override
  1721. public void print(PrintWriter out)
  1722. {
  1723. out.print("String #");
  1724. out.println(string);
  1725. }
  1726. }
  1727. class IntegerInfo extends ConstInfo
  1728. {
  1729. static final int tag = 3;
  1730. int value;
  1731. public IntegerInfo(int v, int index)
  1732. {
  1733. super(index);
  1734. value = v;
  1735. }
  1736. public IntegerInfo(DataInputStream in, int index) throws IOException
  1737. {
  1738. super(index);
  1739. value = in.readInt();
  1740. }
  1741. @Override
  1742. public int hashCode() { return value; }
  1743. @Override
  1744. public boolean equals(Object obj)
  1745. {
  1746. return obj instanceof IntegerInfo && ((IntegerInfo)obj).value == value;
  1747. }
  1748. @Override
  1749. public int getTag() { return tag; }
  1750. @Override
  1751. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1752. {
  1753. return dest.addIntegerInfo(value);
  1754. }
  1755. @Override
  1756. public void write(DataOutputStream out) throws IOException
  1757. {
  1758. out.writeByte(tag);
  1759. out.writeInt(value);
  1760. }
  1761. @Override
  1762. public void print(PrintWriter out)
  1763. {
  1764. out.print("Integer ");
  1765. out.println(value);
  1766. }
  1767. }
  1768. class FloatInfo extends ConstInfo
  1769. {
  1770. static final int tag = 4;
  1771. float value;
  1772. public FloatInfo(float f, int index)
  1773. {
  1774. super(index);
  1775. value = f;
  1776. }
  1777. public FloatInfo(DataInputStream in, int index) throws IOException
  1778. {
  1779. super(index);
  1780. value = in.readFloat();
  1781. }
  1782. @Override
  1783. public int hashCode() { return Float.floatToIntBits(value); }
  1784. @Override
  1785. public boolean equals(Object obj)
  1786. {
  1787. return obj instanceof FloatInfo && ((FloatInfo)obj).value == value;
  1788. }
  1789. @Override
  1790. public int getTag() { return tag; }
  1791. @Override
  1792. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1793. {
  1794. return dest.addFloatInfo(value);
  1795. }
  1796. @Override
  1797. public void write(DataOutputStream out) throws IOException
  1798. {
  1799. out.writeByte(tag);
  1800. out.writeFloat(value);
  1801. }
  1802. @Override
  1803. public void print(PrintWriter out)
  1804. {
  1805. out.print("Float ");
  1806. out.println(value);
  1807. }
  1808. }
  1809. class LongInfo extends ConstInfo
  1810. {
  1811. static final int tag = 5;
  1812. long value;
  1813. public LongInfo(long l, int index)
  1814. {
  1815. super(index);
  1816. value = l;
  1817. }
  1818. public LongInfo(DataInputStream in, int index) throws IOException
  1819. {
  1820. super(index);
  1821. value = in.readLong();
  1822. }
  1823. @Override
  1824. public int hashCode() { return (int)(value ^ (value >>> 32)); }
  1825. @Override
  1826. public boolean equals(Object obj) {
  1827. return obj instanceof LongInfo && ((LongInfo)obj).value == value;
  1828. }
  1829. @Override
  1830. public int getTag() { return tag; }
  1831. @Override
  1832. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1833. {
  1834. return dest.addLongInfo(value);
  1835. }
  1836. @Override
  1837. public void write(DataOutputStream out) throws IOException
  1838. {
  1839. out.writeByte(tag);
  1840. out.writeLong(value);
  1841. }
  1842. @Override
  1843. public void print(PrintWriter out)
  1844. {
  1845. out.print("Long ");
  1846. out.println(value);
  1847. }
  1848. }
  1849. class DoubleInfo extends ConstInfo
  1850. {
  1851. static final int tag = 6;
  1852. double value;
  1853. public DoubleInfo(double d, int index)
  1854. {
  1855. super(index);
  1856. value = d;
  1857. }
  1858. public DoubleInfo(DataInputStream in, int index) throws IOException
  1859. {
  1860. super(index);
  1861. value = in.readDouble();
  1862. }
  1863. @Override
  1864. public int hashCode() {
  1865. long v = Double.doubleToLongBits(value);
  1866. return (int)(v ^ (v >>> 32));
  1867. }
  1868. @Override
  1869. public boolean equals(Object obj)
  1870. {
  1871. return obj instanceof DoubleInfo
  1872. && ((DoubleInfo)obj).value == value;
  1873. }
  1874. @Override
  1875. public int getTag() { return tag; }
  1876. @Override
  1877. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  1878. {
  1879. return dest.addDoubleInfo(value);
  1880. }
  1881. @Override
  1882. public void write(DataOutputStream out) throws IOException
  1883. {
  1884. out.writeByte(tag);
  1885. out.writeDouble(value);
  1886. }
  1887. @Override
  1888. public void print(PrintWriter out)
  1889. {
  1890. out.print("Double ");
  1891. out.println(value);
  1892. }
  1893. }
  1894. class Utf8Info extends ConstInfo
  1895. {
  1896. static final int tag = 1;
  1897. String string;
  1898. public Utf8Info(String utf8, int index)
  1899. {
  1900. super(index);
  1901. string = utf8;
  1902. }
  1903. public Utf8Info(DataInputStream in, int index)
  1904. throws IOException
  1905. {
  1906. super(index);
  1907. string = in.readUTF();
  1908. }
  1909. @Override
  1910. public int hashCode() {
  1911. return string.hashCode();
  1912. }
  1913. @Override
  1914. public boolean equals(Object obj) {
  1915. return obj instanceof Utf8Info
  1916. && ((Utf8Info)obj).string.equals(string);
  1917. }
  1918. @Override
  1919. public int getTag() { return tag; }
  1920. @Override
  1921. public int copy(ConstPool src, ConstPool dest,
  1922. Map<String,String> map)
  1923. {
  1924. return dest.addUtf8Info(string);
  1925. }
  1926. @Override
  1927. public void write(DataOutputStream out)
  1928. throws IOException
  1929. {
  1930. out.writeByte(tag);
  1931. out.writeUTF(string);
  1932. }
  1933. @Override
  1934. public void print(PrintWriter out) {
  1935. out.print("UTF8 \"");
  1936. out.print(string);
  1937. out.println("\"");
  1938. }
  1939. }
  1940. class MethodHandleInfo extends ConstInfo {
  1941. static final int tag = 15;
  1942. int refKind, refIndex;
  1943. public MethodHandleInfo(int kind, int referenceIndex, int index) {
  1944. super(index);
  1945. refKind = kind;
  1946. refIndex = referenceIndex;
  1947. }
  1948. public MethodHandleInfo(DataInputStream in, int index)
  1949. throws IOException
  1950. {
  1951. super(index);
  1952. refKind = in.readUnsignedByte();
  1953. refIndex = in.readUnsignedShort();
  1954. }
  1955. @Override
  1956. public int hashCode() { return (refKind << 16) ^ refIndex; }
  1957. @Override
  1958. public boolean equals(Object obj)
  1959. {
  1960. if (obj instanceof MethodHandleInfo) {
  1961. MethodHandleInfo mh = (MethodHandleInfo)obj;
  1962. return mh.refKind == refKind && mh.refIndex == refIndex;
  1963. }
  1964. return false;
  1965. }
  1966. @Override
  1967. public int getTag() { return tag; }
  1968. @Override
  1969. public int copy(ConstPool src, ConstPool dest,
  1970. Map<String,String> map)
  1971. {
  1972. return dest.addMethodHandleInfo(refKind,
  1973. src.getItem(refIndex).copy(src, dest, map));
  1974. }
  1975. @Override
  1976. public void write(DataOutputStream out) throws IOException
  1977. {
  1978. out.writeByte(tag);
  1979. out.writeByte(refKind);
  1980. out.writeShort(refIndex);
  1981. }
  1982. @Override
  1983. public void print(PrintWriter out) {
  1984. out.print("MethodHandle #");
  1985. out.print(refKind);
  1986. out.print(", index #");
  1987. out.println(refIndex);
  1988. }
  1989. }
  1990. class MethodTypeInfo extends ConstInfo
  1991. {
  1992. static final int tag = 16;
  1993. int descriptor;
  1994. public MethodTypeInfo(int desc, int index)
  1995. {
  1996. super(index);
  1997. descriptor = desc;
  1998. }
  1999. public MethodTypeInfo(DataInputStream in, int index)
  2000. throws IOException
  2001. {
  2002. super(index);
  2003. descriptor = in.readUnsignedShort();
  2004. }
  2005. @Override
  2006. public int hashCode() { return descriptor; }
  2007. @Override
  2008. public boolean equals(Object obj)
  2009. {
  2010. if (obj instanceof MethodTypeInfo)
  2011. return ((MethodTypeInfo)obj).descriptor == descriptor;
  2012. return false;
  2013. }
  2014. @Override
  2015. public int getTag() { return tag; }
  2016. @Override
  2017. public void renameClass(ConstPool cp, String oldName, String newName,
  2018. Map<ConstInfo,ConstInfo> cache)
  2019. {
  2020. String desc = cp.getUtf8Info(descriptor);
  2021. String desc2 = Descriptor.rename(desc, oldName, newName);
  2022. if (desc != desc2)
  2023. if (cache == null)
  2024. descriptor = cp.addUtf8Info(desc2);
  2025. else {
  2026. cache.remove(this);
  2027. descriptor = cp.addUtf8Info(desc2);
  2028. cache.put(this, this);
  2029. }
  2030. }
  2031. @Override
  2032. public void renameClass(ConstPool cp, Map<String,String> map,
  2033. Map<ConstInfo,ConstInfo> cache)
  2034. {
  2035. String desc = cp.getUtf8Info(descriptor);
  2036. String desc2 = Descriptor.rename(desc, map);
  2037. if (desc != desc2)
  2038. if (cache == null)
  2039. descriptor = cp.addUtf8Info(desc2);
  2040. else {
  2041. cache.remove(this);
  2042. descriptor = cp.addUtf8Info(desc2);
  2043. cache.put(this, this);
  2044. }
  2045. }
  2046. @Override
  2047. public int copy(ConstPool src, ConstPool dest, Map<String,String> map)
  2048. {
  2049. String desc = src.getUtf8Info(descriptor);
  2050. desc = Descriptor.rename(desc, map);
  2051. return dest.addMethodTypeInfo(dest.addUtf8Info(desc));
  2052. }
  2053. @Override
  2054. public void write(DataOutputStream out) throws IOException
  2055. {
  2056. out.writeByte(tag);
  2057. out.writeShort(descriptor);
  2058. }
  2059. @Override
  2060. public void print(PrintWriter out) {
  2061. out.print("MethodType #");
  2062. out.println(descriptor);
  2063. }
  2064. }
  2065. class InvokeDynamicInfo extends ConstInfo
  2066. {
  2067. static final int tag = 18;
  2068. int bootstrap, nameAndType;
  2069. public InvokeDynamicInfo(int bootstrapMethod,
  2070. int ntIndex, int index)
  2071. {
  2072. super(index);
  2073. bootstrap = bootstrapMethod;
  2074. nameAndType = ntIndex;
  2075. }
  2076. public InvokeDynamicInfo(DataInputStream in, int index)
  2077. throws IOException
  2078. {
  2079. super(index);
  2080. bootstrap = in.readUnsignedShort();
  2081. nameAndType = in.readUnsignedShort();
  2082. }
  2083. @Override
  2084. public int hashCode() { return (bootstrap << 16) ^ nameAndType; }
  2085. @Override
  2086. public boolean equals(Object obj)
  2087. {
  2088. if (obj instanceof InvokeDynamicInfo) {
  2089. InvokeDynamicInfo iv = (InvokeDynamicInfo)obj;
  2090. return iv.bootstrap == bootstrap
  2091. && iv.nameAndType == nameAndType;
  2092. }
  2093. return false;
  2094. }
  2095. @Override
  2096. public int getTag() { return tag; }
  2097. @Override
  2098. public int copy(ConstPool src, ConstPool dest,
  2099. Map<String,String> map)
  2100. {
  2101. return dest.addInvokeDynamicInfo(bootstrap,
  2102. src.getItem(nameAndType).copy(src, dest, map));
  2103. }
  2104. @Override
  2105. public void write(DataOutputStream out) throws IOException
  2106. {
  2107. out.writeByte(tag);
  2108. out.writeShort(bootstrap);
  2109. out.writeShort(nameAndType);
  2110. }
  2111. @Override
  2112. public void print(PrintWriter out) {
  2113. out.print("InvokeDynamic #");
  2114. out.print(bootstrap);
  2115. out.print(", name&type #");
  2116. out.println(nameAndType);
  2117. }
  2118. }
  2119. class DynamicInfo extends ConstInfo {
  2120. static final int tag = 17;
  2121. int bootstrap, nameAndType;
  2122. public DynamicInfo(int bootstrapMethod, int ntIndex, int index) {
  2123. super(index);
  2124. bootstrap = bootstrapMethod;
  2125. nameAndType = ntIndex;
  2126. }
  2127. public DynamicInfo(DataInputStream in, int index) throws IOException {
  2128. super(index);
  2129. bootstrap = in.readUnsignedShort();
  2130. nameAndType = in.readUnsignedShort();
  2131. }
  2132. @Override
  2133. public int hashCode() {
  2134. return (bootstrap << 16) ^ nameAndType;
  2135. }
  2136. @Override
  2137. public boolean equals(Object obj) {
  2138. if (obj instanceof DynamicInfo) {
  2139. DynamicInfo iv = (DynamicInfo)obj;
  2140. return iv.bootstrap == bootstrap && iv.nameAndType == nameAndType;
  2141. }
  2142. return false;
  2143. }
  2144. @Override
  2145. public int getTag() {
  2146. return tag;
  2147. }
  2148. @Override
  2149. public int copy(ConstPool src, ConstPool dest, Map<String, String> map) {
  2150. return dest.addDynamicInfo(bootstrap, src.getItem(nameAndType).copy(src, dest, map));
  2151. }
  2152. @Override
  2153. public void write(DataOutputStream out) throws IOException {
  2154. out.writeByte(tag);
  2155. out.writeShort(bootstrap);
  2156. out.writeShort(nameAndType);
  2157. }
  2158. @Override
  2159. public void print(PrintWriter out) {
  2160. out.print("Dynamic #");
  2161. out.print(bootstrap);
  2162. out.print(", name&type #");
  2163. out.println(nameAndType);
  2164. }
  2165. }
  2166. class ModuleInfo extends ConstInfo
  2167. {
  2168. static final int tag = 19;
  2169. int name;
  2170. public ModuleInfo(int moduleName, int index)
  2171. {
  2172. super(index);
  2173. name = moduleName;
  2174. }
  2175. public ModuleInfo(DataInputStream in, int index)
  2176. throws IOException
  2177. {
  2178. super(index);
  2179. name = in.readUnsignedShort();
  2180. }
  2181. @Override
  2182. public int hashCode() { return name; }
  2183. @Override
  2184. public boolean equals(Object obj)
  2185. {
  2186. return obj instanceof ModuleInfo
  2187. && ((ModuleInfo)obj).name == name;
  2188. }
  2189. @Override
  2190. public int getTag() { return tag; }
  2191. public String getModuleName(ConstPool cp)
  2192. {
  2193. return cp.getUtf8Info(name);
  2194. }
  2195. @Override
  2196. public int copy(ConstPool src, ConstPool dest,
  2197. Map<String,String> map)
  2198. {
  2199. String moduleName = src.getUtf8Info(name);
  2200. int newName = dest.addUtf8Info(moduleName);
  2201. return dest.addModuleInfo(newName);
  2202. }
  2203. @Override
  2204. public void write(DataOutputStream out) throws IOException
  2205. {
  2206. out.writeByte(tag);
  2207. out.writeShort(name);
  2208. }
  2209. @Override
  2210. public void print(PrintWriter out) {
  2211. out.print("Module #");
  2212. out.println(name);
  2213. }
  2214. }
  2215. class PackageInfo extends ConstInfo
  2216. {
  2217. static final int tag = 20;
  2218. int name;
  2219. public PackageInfo(int moduleName, int index)
  2220. {
  2221. super(index);
  2222. name = moduleName;
  2223. }
  2224. public PackageInfo(DataInputStream in, int index)
  2225. throws IOException
  2226. {
  2227. super(index);
  2228. name = in.readUnsignedShort();
  2229. }
  2230. @Override
  2231. public int hashCode() { return name; }
  2232. @Override
  2233. public boolean equals(Object obj) {
  2234. return obj instanceof PackageInfo
  2235. && ((PackageInfo)obj).name == name;
  2236. }
  2237. @Override
  2238. public int getTag() { return tag; }
  2239. public String getPackageName(ConstPool cp)
  2240. {
  2241. return cp.getUtf8Info(name);
  2242. }
  2243. @Override
  2244. public int copy(ConstPool src, ConstPool dest,
  2245. Map<String,String> map)
  2246. {
  2247. String packageName = src.getUtf8Info(name);
  2248. int newName = dest.addUtf8Info(packageName);
  2249. return dest.addModuleInfo(newName);
  2250. }
  2251. @Override
  2252. public void write(DataOutputStream out) throws IOException
  2253. {
  2254. out.writeByte(tag);
  2255. out.writeShort(name);
  2256. }
  2257. @Override
  2258. public void print(PrintWriter out)
  2259. {
  2260. out.print("Package #");
  2261. out.println(name);
  2262. }
  2263. }