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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  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 bootstrap 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.26
  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.26
  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 bootstrap method.
  726. * @since 3.26
  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.26
  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, int ntIndex, int index) {
  2119. super(index);
  2120. bootstrap = bootstrapMethod;
  2121. nameAndType = ntIndex;
  2122. }
  2123. public DynamicInfo(DataInputStream in, int index) throws IOException {
  2124. super(index);
  2125. bootstrap = in.readUnsignedShort();
  2126. nameAndType = in.readUnsignedShort();
  2127. }
  2128. @Override
  2129. public int hashCode() {
  2130. return (bootstrap << 16) ^ nameAndType;
  2131. }
  2132. @Override
  2133. public boolean equals(Object obj) {
  2134. if (obj instanceof DynamicInfo) {
  2135. DynamicInfo iv = (DynamicInfo)obj;
  2136. return iv.bootstrap == bootstrap && iv.nameAndType == nameAndType;
  2137. }
  2138. return false;
  2139. }
  2140. @Override
  2141. public int getTag() {
  2142. return tag;
  2143. }
  2144. @Override
  2145. public int copy(ConstPool src, ConstPool dest, Map<String, String> map) {
  2146. return dest.addDynamicInfo(bootstrap, src.getItem(nameAndType).copy(src, dest, map));
  2147. }
  2148. @Override
  2149. public void write(DataOutputStream out) throws IOException {
  2150. out.writeByte(tag);
  2151. out.writeShort(bootstrap);
  2152. out.writeShort(nameAndType);
  2153. }
  2154. @Override
  2155. public void print(PrintWriter out) {
  2156. out.print("Dynamic #");
  2157. out.print(bootstrap);
  2158. out.print(", name&type #");
  2159. out.println(nameAndType);
  2160. }
  2161. }
  2162. class ModuleInfo extends ConstInfo
  2163. {
  2164. static final int tag = 19;
  2165. int name;
  2166. public ModuleInfo(int moduleName, int index)
  2167. {
  2168. super(index);
  2169. name = moduleName;
  2170. }
  2171. public ModuleInfo(DataInputStream in, int index)
  2172. throws IOException
  2173. {
  2174. super(index);
  2175. name = in.readUnsignedShort();
  2176. }
  2177. @Override
  2178. public int hashCode() { return name; }
  2179. @Override
  2180. public boolean equals(Object obj)
  2181. {
  2182. return obj instanceof ModuleInfo
  2183. && ((ModuleInfo)obj).name == name;
  2184. }
  2185. @Override
  2186. public int getTag() { return tag; }
  2187. public String getModuleName(ConstPool cp)
  2188. {
  2189. return cp.getUtf8Info(name);
  2190. }
  2191. @Override
  2192. public int copy(ConstPool src, ConstPool dest,
  2193. Map<String,String> map)
  2194. {
  2195. String moduleName = src.getUtf8Info(name);
  2196. int newName = dest.addUtf8Info(moduleName);
  2197. return dest.addModuleInfo(newName);
  2198. }
  2199. @Override
  2200. public void write(DataOutputStream out) throws IOException
  2201. {
  2202. out.writeByte(tag);
  2203. out.writeShort(name);
  2204. }
  2205. @Override
  2206. public void print(PrintWriter out) {
  2207. out.print("Module #");
  2208. out.println(name);
  2209. }
  2210. }
  2211. class PackageInfo extends ConstInfo
  2212. {
  2213. static final int tag = 20;
  2214. int name;
  2215. public PackageInfo(int moduleName, int index)
  2216. {
  2217. super(index);
  2218. name = moduleName;
  2219. }
  2220. public PackageInfo(DataInputStream in, int index)
  2221. throws IOException
  2222. {
  2223. super(index);
  2224. name = in.readUnsignedShort();
  2225. }
  2226. @Override
  2227. public int hashCode() { return name; }
  2228. @Override
  2229. public boolean equals(Object obj) {
  2230. return obj instanceof PackageInfo
  2231. && ((PackageInfo)obj).name == name;
  2232. }
  2233. @Override
  2234. public int getTag() { return tag; }
  2235. public String getPackageName(ConstPool cp)
  2236. {
  2237. return cp.getUtf8Info(name);
  2238. }
  2239. @Override
  2240. public int copy(ConstPool src, ConstPool dest,
  2241. Map<String,String> map)
  2242. {
  2243. String packageName = src.getUtf8Info(name);
  2244. int newName = dest.addUtf8Info(packageName);
  2245. return dest.addModuleInfo(newName);
  2246. }
  2247. @Override
  2248. public void write(DataOutputStream out) throws IOException
  2249. {
  2250. out.writeByte(tag);
  2251. out.writeShort(name);
  2252. }
  2253. @Override
  2254. public void print(PrintWriter out)
  2255. {
  2256. out.print("Package #");
  2257. out.println(name);
  2258. }
  2259. }