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

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