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

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