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.

ConstantPool.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution,
  20. * if any, must include the following acknowledgment:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (https://www.apache.org/)."
  23. * Alternately, this acknowledgment may appear in the software itself,
  24. * if and wherever such third-party acknowledgments normally appear.
  25. *
  26. * 4. The names "Apache" and "Apache Software Foundation" and
  27. * "Apache BCEL" must not be used to endorse or promote products
  28. * derived from this software without prior written permission. For
  29. * written permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache",
  32. * "Apache BCEL", nor may "Apache" appear in their name, without
  33. * prior written permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <https://www.apache.org/>.
  53. */
  54. package org.aspectj.apache.bcel.classfile;
  55. import java.io.DataInputStream;
  56. import java.io.DataOutputStream;
  57. import java.io.IOException;
  58. import java.util.HashMap;
  59. import java.util.Map;
  60. import org.aspectj.apache.bcel.Constants;
  61. import org.aspectj.apache.bcel.generic.ArrayType;
  62. import org.aspectj.apache.bcel.generic.ObjectType;
  63. /**
  64. * This class represents the constant pool, i.e., a table of constants, of a parsed classfile. It may contain null references, due
  65. * to the JVM specification that skips an entry after an 8-byte constant (double, long) entry.
  66. */
  67. public class ConstantPool implements Node {
  68. private Constant[] pool;
  69. private int poolSize; // number of entries in the pool (could be < pool.length as the array is resized in 'chunks')
  70. private Map<String, Integer> utf8Cache = new HashMap<>();
  71. private Map<String, Integer> methodCache = new HashMap<>();
  72. private Map<String, Integer> fieldCache = new HashMap<>();
  73. public int getSize() {
  74. return poolSize;
  75. }
  76. public ConstantPool() {
  77. pool = new Constant[10];
  78. poolSize = 0;
  79. }
  80. public ConstantPool(Constant[] constants) {
  81. pool = constants;
  82. poolSize = (constants == null ? 0 : constants.length);
  83. }
  84. ConstantPool(DataInputStream file) throws IOException {
  85. byte tag;
  86. poolSize = file.readUnsignedShort();
  87. pool = new Constant[poolSize];
  88. // pool[0] is unused by the compiler and may be used freely by the implementation
  89. for (int i = 1; i < poolSize; i++) {
  90. pool[i] = Constant.readConstant(file);
  91. tag = pool[i].getTag();
  92. if ((tag == Constants.CONSTANT_Double) || (tag == Constants.CONSTANT_Long)) {
  93. i++;
  94. }
  95. }
  96. }
  97. public Constant getConstant(int index, byte tag) {
  98. Constant c = getConstant(index);
  99. // if (c == null) throw new ClassFormatException("Constant pool at index " + index + " is null.");
  100. if (c.tag == tag)
  101. return c;
  102. throw new ClassFormatException("Expected class '" + Constants.CONSTANT_NAMES[tag] + "' at index " + index + " and found "
  103. + c);
  104. }
  105. public Constant getConstant(int index) {
  106. try {
  107. return pool[index];
  108. } catch (ArrayIndexOutOfBoundsException aioobe) {
  109. throw new ClassFormatException("Index " + index + " into constant pool (size:" + poolSize + ") is invalid");
  110. }
  111. }
  112. /**
  113. * @return deep copy of this constant pool
  114. */
  115. public ConstantPool copy() {
  116. Constant[] newConstants = new Constant[poolSize]; // use the correct size
  117. for (int i = 1; i < poolSize; i++) {
  118. if (pool[i] != null) {
  119. newConstants[i] = pool[i].copy();
  120. }
  121. }
  122. return new ConstantPool(newConstants);
  123. }
  124. /**
  125. * Get string from constant pool and bypass the indirection of `ConstantClass' and `ConstantString' objects. I.e. these classes
  126. * have an index field that points to another entry of the constant pool of type `ConstantUtf8' which contains the real data.
  127. *
  128. * @param index Index in constant pool
  129. * @param tag Tag of expected constant, either ConstantClass or ConstantString
  130. * @return Contents of string reference
  131. * @see ConstantClass
  132. * @see ConstantString
  133. * @throws ClassFormatException
  134. */
  135. public String getConstantString(int index, byte tag) throws ClassFormatException {
  136. Constant c = getConstant(index, tag);
  137. int i;
  138. /*
  139. * This switch() is not that elegant, since the two classes have the same contents, they just differ in the name of the
  140. * index field variable. But we want to stick to the JVM naming conventions closely though we could have solved these more
  141. * elegantly by using the same variable name or by subclassing.
  142. */
  143. // OPTIMIZE remove the difference - use the an interface and same index methods for string ref id
  144. switch (tag) {
  145. case Constants.CONSTANT_Class:
  146. i = ((ConstantClass) c).getNameIndex();
  147. break;
  148. case Constants.CONSTANT_String:
  149. i = ((ConstantString) c).getStringIndex();
  150. break;
  151. default:
  152. throw new RuntimeException("getConstantString called with illegal tag " + tag);
  153. }
  154. // Finally get the string from the constant pool
  155. c = getConstant(i, Constants.CONSTANT_Utf8);
  156. return ((ConstantUtf8) c).getValue();
  157. }
  158. /**
  159. * Resolve constant to a string representation.
  160. */
  161. public String constantToString(Constant c) {
  162. String str;
  163. int i;
  164. switch (c.tag) {
  165. case Constants.CONSTANT_Class:
  166. i = ((ConstantClass) c).getNameIndex();
  167. c = getConstant(i, Constants.CONSTANT_Utf8);
  168. str = Utility.compactClassName(((ConstantUtf8) c).getValue(), false);
  169. break;
  170. case Constants.CONSTANT_String:
  171. i = ((ConstantString) c).getStringIndex();
  172. c = getConstant(i, Constants.CONSTANT_Utf8);
  173. str = "\"" + escape(((ConstantUtf8) c).getValue()) + "\"";
  174. break;
  175. case Constants.CONSTANT_Utf8:
  176. case Constants.CONSTANT_Double:
  177. case Constants.CONSTANT_Float:
  178. case Constants.CONSTANT_Long:
  179. case Constants.CONSTANT_Integer:
  180. str = ((SimpleConstant) c).getStringValue();
  181. break;
  182. case Constants.CONSTANT_NameAndType:
  183. str = (constantToString(((ConstantNameAndType) c).getNameIndex(), Constants.CONSTANT_Utf8) + " " + constantToString(
  184. ((ConstantNameAndType) c).getSignatureIndex(), Constants.CONSTANT_Utf8));
  185. break;
  186. case Constants.CONSTANT_InterfaceMethodref:
  187. case Constants.CONSTANT_Methodref:
  188. case Constants.CONSTANT_Fieldref:
  189. str = (constantToString(((ConstantCP) c).getClassIndex(), Constants.CONSTANT_Class) + "." + constantToString(
  190. ((ConstantCP) c).getNameAndTypeIndex(), Constants.CONSTANT_NameAndType));
  191. break;
  192. case Constants.CONSTANT_InvokeDynamic:
  193. ConstantInvokeDynamic cID = ((ConstantInvokeDynamic)c);
  194. return "#"+cID.getBootstrapMethodAttrIndex()+"."+constantToString(cID.getNameAndTypeIndex(), Constants.CONSTANT_NameAndType);
  195. case Constants.CONSTANT_Dynamic:
  196. ConstantDynamic cD = ((ConstantDynamic)c);
  197. return "#"+cD.getBootstrapMethodAttrIndex()+"."+constantToString(cD.getNameAndTypeIndex(), Constants.CONSTANT_NameAndType);
  198. case Constants.CONSTANT_MethodHandle:
  199. ConstantMethodHandle cMH = ((ConstantMethodHandle)c);
  200. return cMH.getReferenceKind()+":"+constantToString(cMH.getReferenceIndex(),Constants.CONSTANT_Methodref);
  201. case Constants.CONSTANT_MethodType:
  202. ConstantMethodType cMT = (ConstantMethodType)c;
  203. return constantToString(cMT.getDescriptorIndex(),Constants.CONSTANT_Utf8);
  204. case Constants.CONSTANT_Module:
  205. ConstantModule cM = (ConstantModule)c;
  206. return "Module:"+constantToString(cM.getNameIndex(),Constants.CONSTANT_Utf8);
  207. case Constants.CONSTANT_Package:
  208. ConstantPackage cP = (ConstantPackage)c;
  209. return "Package:"+constantToString(cP.getNameIndex(),Constants.CONSTANT_Utf8);
  210. default: // Never reached
  211. throw new RuntimeException("Unknown constant type " + c.tag);
  212. }
  213. return str;
  214. }
  215. private static final String escape(String str) {
  216. int len = str.length();
  217. StringBuilder buf = new StringBuilder(len + 5);
  218. char[] ch = str.toCharArray();
  219. for (int i = 0; i < len; i++) {
  220. switch (ch[i]) {
  221. case '\n':
  222. buf.append("\\n");
  223. break;
  224. case '\r':
  225. buf.append("\\r");
  226. break;
  227. case '\t':
  228. buf.append("\\t");
  229. break;
  230. case '\b':
  231. buf.append("\\b");
  232. break;
  233. case '"':
  234. buf.append("\\\"");
  235. break;
  236. default:
  237. buf.append(ch[i]);
  238. }
  239. }
  240. return buf.toString();
  241. }
  242. public String constantToString(int index, byte tag) {
  243. Constant c = getConstant(index, tag);
  244. return constantToString(c);
  245. }
  246. public String constantToString(int index) {
  247. return constantToString(getConstant(index));
  248. }
  249. @Override
  250. public void accept(ClassVisitor v) {
  251. v.visitConstantPool(this);
  252. }
  253. public Constant[] getConstantPool() {
  254. return pool;
  255. } // TEMPORARY, DONT LIKE PASSING THIS DATA OUT!
  256. public void dump(DataOutputStream file) throws IOException {
  257. /*
  258. * Constants over the size of the constant pool shall not be written out.
  259. * This is a redundant measure as the ConstantPoolGen should have already
  260. * reported an error back in the situation.
  261. */
  262. final int size = Math.min(poolSize, Constants.MAX_CP_ENTRIES);
  263. file.writeShort(size);
  264. for (int i = 1; i < size; i++)
  265. if (pool[i] != null)
  266. pool[i].dump(file);
  267. }
  268. public ConstantUtf8 getConstantUtf8(int index) {
  269. Constant c = getConstant(index);
  270. assert c != null;
  271. assert c.tag == Constants.CONSTANT_Utf8;
  272. return (ConstantUtf8) c;
  273. }
  274. public ConstantModule getConstantModule(int index) {
  275. Constant c = getConstant(index);
  276. assert c != null;
  277. assert c.tag == Constants.CONSTANT_Module;
  278. return (ConstantModule)c;
  279. }
  280. public ConstantPackage getConstantPackage(int index) {
  281. Constant c = getConstant(index);
  282. assert c != null;
  283. assert c.tag == Constants.CONSTANT_Package;
  284. return (ConstantPackage)c;
  285. }
  286. public String getConstantString_CONSTANTClass(int index) {
  287. ConstantClass c = (ConstantClass) getConstant(index, Constants.CONSTANT_Class);
  288. index = c.getNameIndex();
  289. return ((ConstantUtf8) getConstant(index, Constants.CONSTANT_Utf8)).getValue();
  290. }
  291. public int getLength() {
  292. return poolSize;
  293. }
  294. @Override
  295. public String toString() {
  296. StringBuilder buf = new StringBuilder();
  297. for (int i = 1; i < poolSize; i++)
  298. buf.append(i).append(") ").append(pool[i]).append("\n");
  299. return buf.toString();
  300. }
  301. public int lookupInteger(int n) {
  302. for (int i = 1; i < poolSize; i++) {
  303. if (pool[i] instanceof ConstantInteger) {
  304. ConstantInteger c = (ConstantInteger) pool[i];
  305. if (c.getValue() == n)
  306. return i;
  307. }
  308. }
  309. return -1;
  310. }
  311. public int lookupUtf8(String string) {
  312. Integer pos = utf8Cache.get(string);
  313. if (pos != null) {
  314. return pos;
  315. }
  316. for (int i = 1; i < poolSize; i++) {
  317. Constant c = pool[i];
  318. if (c != null && c.tag == Constants.CONSTANT_Utf8) {
  319. if (((ConstantUtf8) c).getValue().equals(string)) {
  320. utf8Cache.put(string, i);
  321. return i;
  322. }
  323. }
  324. }
  325. return -1;
  326. }
  327. public int lookupClass(String classname) {
  328. for (int i = 1; i < poolSize; i++) {
  329. Constant c = pool[i];
  330. if (c != null && c.tag == Constants.CONSTANT_Class) {
  331. int cIndex = ((ConstantClass) c).getNameIndex();
  332. String cName = ((ConstantUtf8) pool[cIndex]).getValue();
  333. if (cName.equals(classname))
  334. return i;
  335. }
  336. }
  337. return -1;
  338. }
  339. public int addUtf8(String n) {
  340. int ret = lookupUtf8(n);
  341. if (ret != -1)
  342. return ret;
  343. adjustSize();
  344. ret = poolSize;
  345. pool[poolSize++] = new ConstantUtf8(n);
  346. return ret;
  347. }
  348. public int addInteger(int n) {
  349. int ret = lookupInteger(n);
  350. if (ret != -1)
  351. return ret;
  352. adjustSize();
  353. ret = poolSize;
  354. pool[poolSize++] = new ConstantInteger(n);
  355. return ret;
  356. }
  357. public int addArrayClass(ArrayType type) {
  358. return addClass(type.getSignature());
  359. }
  360. public int addClass(ObjectType type) {
  361. return addClass(type.getClassName());
  362. }
  363. public int addClass(String classname) {
  364. String toAdd = classname.replace('.', '/');
  365. int ret = lookupClass(toAdd);
  366. if (ret != -1)
  367. return ret;
  368. adjustSize();
  369. ConstantClass c = new ConstantClass(addUtf8(toAdd));
  370. ret = poolSize;
  371. pool[poolSize++] = c;
  372. return ret;
  373. }
  374. private void adjustSize() {
  375. // 3 extra spaces are needed as some entries may take 3 slots
  376. if (poolSize + 3 >= Constants.MAX_CP_ENTRIES + 1) {
  377. throw new IllegalStateException(
  378. "The number of constants " + (poolSize + 3) +
  379. " is over the size of the constant pool: " + Constants.MAX_CP_ENTRIES
  380. );
  381. }
  382. if (poolSize + 3 >= pool.length) {
  383. Constant[] cs = pool;
  384. int size = cs.length + 8;
  385. // the constant array shall not exceed the size of the constant pool
  386. size = Math.min(size, Constants.MAX_CP_ENTRIES + 1);
  387. pool = new Constant[size];
  388. System.arraycopy(cs, 0, pool, 0, cs.length);
  389. }
  390. if (poolSize == 0)
  391. poolSize = 1; // someone about to do something in here!
  392. }
  393. public int addFieldref(String class_name, String field_name, String signature) {
  394. int ret = lookupFieldref(class_name, field_name, signature);
  395. int class_index, name_and_type_index;
  396. if (ret != -1)
  397. return ret;
  398. adjustSize();
  399. class_index = addClass(class_name);
  400. name_and_type_index = addNameAndType(field_name, signature);
  401. ret = poolSize;
  402. pool[poolSize++] = new ConstantFieldref(class_index, name_and_type_index);
  403. return ret;
  404. }
  405. public int lookupFieldref(String searchClassname, String searchFieldname, String searchSignature) {
  406. searchClassname = searchClassname.replace('.', '/');
  407. String k = new StringBuilder().append(searchClassname).append(searchFieldname).append(searchSignature).toString();
  408. Integer pos = fieldCache.get(k);
  409. if (pos != null)
  410. return pos;
  411. for (int i = 1; i < poolSize; i++) {
  412. Constant c = pool[i];
  413. if (c != null && c.tag == Constants.CONSTANT_Fieldref) {
  414. ConstantFieldref cfr = (ConstantFieldref) c;
  415. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  416. // check the class
  417. int cIndex = cfr.getClassIndex();
  418. ConstantClass cc = (ConstantClass) pool[cIndex];
  419. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  420. if (!cName.equals(searchClassname))
  421. continue;
  422. // check the name and type
  423. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  424. if (!name.equals(searchFieldname))
  425. continue; // not this one
  426. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  427. if (!typeSignature.equals(searchSignature))
  428. continue;
  429. fieldCache.put(k, i);
  430. return i;
  431. }
  432. }
  433. return -1;
  434. }
  435. public int addNameAndType(String name, String signature) {
  436. int ret = lookupNameAndType(name, signature);
  437. if (ret != -1)
  438. return ret;
  439. adjustSize();
  440. int name_index = addUtf8(name);
  441. int signature_index = addUtf8(signature);
  442. ret = poolSize;
  443. pool[poolSize++] = new ConstantNameAndType(name_index, signature_index);
  444. return ret;
  445. }
  446. public int lookupNameAndType(String searchName, String searchTypeSignature) {
  447. for (int i = 1; i < poolSize; i++) {
  448. Constant c = pool[i];
  449. if (c != null && c.tag == Constants.CONSTANT_NameAndType) {
  450. ConstantNameAndType cnat = (ConstantNameAndType) c;
  451. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  452. if (!name.equals(searchName))
  453. continue; // not this one
  454. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  455. if (!typeSignature.equals(searchTypeSignature))
  456. continue;
  457. return i;
  458. }
  459. }
  460. return -1;
  461. }
  462. public int addFloat(float f) {
  463. int ret = lookupFloat(f);
  464. if (ret != -1)
  465. return ret;
  466. adjustSize();
  467. ret = poolSize;
  468. pool[poolSize++] = new ConstantFloat(f);
  469. return ret;
  470. }
  471. public int lookupFloat(float f) {
  472. int bits = Float.floatToIntBits(f);
  473. for (int i = 1; i < poolSize; i++) {
  474. Constant c = pool[i];
  475. if (c != null && c.tag == Constants.CONSTANT_Float) {
  476. ConstantFloat cf = (ConstantFloat) c;
  477. if (Float.floatToIntBits(cf.getValue()) == bits)
  478. return i;
  479. }
  480. }
  481. return -1;
  482. }
  483. public int addDouble(double d) {
  484. int ret = lookupDouble(d);
  485. if (ret != -1)
  486. return ret;
  487. adjustSize();
  488. ret = poolSize;
  489. pool[poolSize] = new ConstantDouble(d);
  490. poolSize += 2;
  491. return ret;
  492. }
  493. public int lookupDouble(double d) {
  494. long bits = Double.doubleToLongBits(d);
  495. for (int i = 1; i < poolSize; i++) {
  496. Constant c = pool[i];
  497. if (c != null && c.tag == Constants.CONSTANT_Double) {
  498. ConstantDouble cf = (ConstantDouble) c;
  499. if (Double.doubleToLongBits(cf.getValue()) == bits)
  500. return i;
  501. }
  502. }
  503. return -1;
  504. }
  505. public int addLong(long l) {
  506. int ret = lookupLong(l);
  507. if (ret != -1)
  508. return ret;
  509. adjustSize();
  510. ret = poolSize;
  511. pool[poolSize] = new ConstantLong(l);
  512. poolSize += 2;
  513. return ret;
  514. }
  515. public int lookupString(String s) {
  516. for (int i = 1; i < poolSize; i++) {
  517. Constant c = pool[i];
  518. if (c != null && c.tag == Constants.CONSTANT_String) {
  519. ConstantString cs = (ConstantString) c;
  520. ConstantUtf8 cu8 = (ConstantUtf8) pool[cs.getStringIndex()];
  521. if (cu8.getValue().equals(s))
  522. return i;
  523. }
  524. }
  525. return -1;
  526. }
  527. public int addString(String str) {
  528. int ret = lookupString(str);
  529. if (ret != -1)
  530. return ret;
  531. int utf8 = addUtf8(str);
  532. adjustSize();
  533. ConstantString s = new ConstantString(utf8);
  534. ret = poolSize;
  535. pool[poolSize++] = s;
  536. return ret;
  537. }
  538. public int lookupLong(long l) {
  539. for (int i = 1; i < poolSize; i++) {
  540. Constant c = pool[i];
  541. if (c != null && c.tag == Constants.CONSTANT_Long) {
  542. ConstantLong cf = (ConstantLong) c;
  543. if (cf.getValue() == l)
  544. return i;
  545. }
  546. }
  547. return -1;
  548. }
  549. public int addConstant(Constant c, ConstantPool cp) {
  550. Constant[] constants = cp.getConstantPool();
  551. switch (c.getTag()) {
  552. case Constants.CONSTANT_String: {
  553. ConstantString s = (ConstantString) c;
  554. ConstantUtf8 u8 = (ConstantUtf8) constants[s.getStringIndex()];
  555. return addString(u8.getValue());
  556. }
  557. case Constants.CONSTANT_Class: {
  558. ConstantClass s = (ConstantClass) c;
  559. ConstantUtf8 u8 = (ConstantUtf8) constants[s.getNameIndex()];
  560. return addClass(u8.getValue());
  561. }
  562. case Constants.CONSTANT_NameAndType: {
  563. ConstantNameAndType n = (ConstantNameAndType) c;
  564. ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()];
  565. ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()];
  566. return addNameAndType(u8.getValue(), u8_2.getValue());
  567. }
  568. case Constants.CONSTANT_InvokeDynamic: {
  569. ConstantInvokeDynamic cid = (ConstantInvokeDynamic)c;
  570. int index1 = cid.getBootstrapMethodAttrIndex();
  571. ConstantNameAndType cnat = (ConstantNameAndType)constants[cid.getNameAndTypeIndex()];
  572. ConstantUtf8 name = (ConstantUtf8) constants[cnat.getNameIndex()];
  573. ConstantUtf8 signature = (ConstantUtf8) constants[cnat.getSignatureIndex()];
  574. int index2 = addNameAndType(name.getValue(), signature.getValue());
  575. return addInvokeDynamic(index1,index2);
  576. }
  577. case Constants.CONSTANT_Dynamic: {
  578. ConstantDynamic cd = (ConstantDynamic)c;
  579. int index1 = cd.getBootstrapMethodAttrIndex();
  580. ConstantNameAndType cnat = (ConstantNameAndType)constants[cd.getNameAndTypeIndex()];
  581. ConstantUtf8 name = (ConstantUtf8) constants[cnat.getNameIndex()];
  582. ConstantUtf8 signature = (ConstantUtf8) constants[cnat.getSignatureIndex()];
  583. int index2 = addNameAndType(name.getValue(), signature.getValue());
  584. return addConstantDynamic(index1,index2);
  585. }
  586. case Constants.CONSTANT_MethodHandle:
  587. ConstantMethodHandle cmh = (ConstantMethodHandle)c;
  588. return addMethodHandle(cmh.getReferenceKind(),addConstant(constants[cmh.getReferenceIndex()],cp));
  589. case Constants.CONSTANT_Utf8:
  590. return addUtf8(((ConstantUtf8) c).getValue());
  591. case Constants.CONSTANT_Double:
  592. return addDouble(((ConstantDouble) c).getValue());
  593. case Constants.CONSTANT_Float:
  594. return addFloat(((ConstantFloat) c).getValue());
  595. case Constants.CONSTANT_Long:
  596. return addLong(((ConstantLong) c).getValue());
  597. case Constants.CONSTANT_Integer:
  598. return addInteger(((ConstantInteger) c).getValue());
  599. case Constants.CONSTANT_MethodType:
  600. ConstantMethodType cmt = (ConstantMethodType)c;
  601. return addMethodType(addConstant(constants[cmt.getDescriptorIndex()],cp));
  602. case Constants.CONSTANT_InterfaceMethodref:
  603. case Constants.CONSTANT_Methodref:
  604. case Constants.CONSTANT_Fieldref: {
  605. ConstantCP m = (ConstantCP) c;
  606. ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
  607. ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
  608. ConstantUtf8 u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
  609. String class_name = u8.getValue().replace('/', '.');
  610. u8 = (ConstantUtf8) constants[n.getNameIndex()];
  611. String name = u8.getValue();
  612. u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
  613. String signature = u8.getValue();
  614. switch (c.getTag()) {
  615. case Constants.CONSTANT_InterfaceMethodref:
  616. return addInterfaceMethodref(class_name, name, signature);
  617. case Constants.CONSTANT_Methodref:
  618. return addMethodref(class_name, name, signature); // OPTIMIZE indicate it should be cached!
  619. case Constants.CONSTANT_Fieldref:
  620. return addFieldref(class_name, name, signature);
  621. default: // Never reached
  622. throw new RuntimeException("Unknown constant type " + c);
  623. }
  624. }
  625. default: // Never reached
  626. throw new RuntimeException("Unknown constant type " + c);
  627. }
  628. }
  629. public int addMethodHandle(byte referenceKind, int referenceIndex) {
  630. adjustSize();
  631. int ret = poolSize;
  632. pool[poolSize++] = new ConstantMethodHandle(referenceKind, referenceIndex);
  633. return ret;
  634. }
  635. public int addMethodType(int descriptorIndex) {
  636. adjustSize();
  637. int ret = poolSize;
  638. pool[poolSize++] = new ConstantMethodType(descriptorIndex);
  639. return ret;
  640. }
  641. // OPTIMIZE should put it in the cache now
  642. public int addMethodref(String class_name, String method_name, String signature) {
  643. int ret, class_index, name_and_type_index;
  644. if ((ret = lookupMethodref(class_name, method_name, signature)) != -1)
  645. return ret; // Already in CP
  646. adjustSize();
  647. name_and_type_index = addNameAndType(method_name, signature);
  648. class_index = addClass(class_name);
  649. ret = poolSize;
  650. pool[poolSize++] = new ConstantMethodref(class_index, name_and_type_index);
  651. return ret;
  652. }
  653. public int addInvokeDynamic(int bootstrapMethodIndex, int constantNameAndTypeIndex) {
  654. adjustSize();
  655. int ret = poolSize;
  656. pool[poolSize++] = new ConstantInvokeDynamic(bootstrapMethodIndex, constantNameAndTypeIndex);
  657. return ret;
  658. }
  659. public int addConstantDynamic(int bootstrapMethodIndex, int constantNameAndTypeIndex) {
  660. adjustSize();
  661. int ret = poolSize;
  662. pool[poolSize++] = new ConstantDynamic(bootstrapMethodIndex, constantNameAndTypeIndex);
  663. return ret;
  664. }
  665. public int addInterfaceMethodref(String class_name, String method_name, String signature) {
  666. int ret = lookupInterfaceMethodref(class_name, method_name, signature);
  667. int class_index, name_and_type_index;
  668. if (ret != -1)
  669. return ret;
  670. adjustSize();
  671. class_index = addClass(class_name);
  672. name_and_type_index = addNameAndType(method_name, signature);
  673. ret = poolSize;
  674. pool[poolSize++] = new ConstantInterfaceMethodref(class_index, name_and_type_index);
  675. return ret;
  676. }
  677. public int lookupInterfaceMethodref(String searchClassname, String searchMethodName, String searchSignature) {
  678. searchClassname = searchClassname.replace('.', '/');
  679. for (int i = 1; i < poolSize; i++) {
  680. Constant c = pool[i];
  681. if (c != null && c.tag == Constants.CONSTANT_InterfaceMethodref) {
  682. ConstantInterfaceMethodref cfr = (ConstantInterfaceMethodref) c;
  683. ConstantClass cc = (ConstantClass) pool[cfr.getClassIndex()];
  684. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  685. if (!cName.equals(searchClassname))
  686. continue;
  687. // check the name and type
  688. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  689. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  690. if (!name.equals(searchMethodName))
  691. continue; // not this one
  692. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  693. if (!typeSignature.equals(searchSignature))
  694. continue;
  695. return i;
  696. }
  697. }
  698. return -1;
  699. }
  700. public int lookupMethodref(String searchClassname, String searchMethodName, String searchSignature) {
  701. String key = new StringBuilder().append(searchClassname).append(searchMethodName).append(searchSignature).toString();
  702. Integer cached = methodCache.get(key);
  703. if (cached != null)
  704. return cached;
  705. searchClassname = searchClassname.replace('.', '/');
  706. for (int i = 1; i < poolSize; i++) {
  707. Constant c = pool[i];
  708. if (c != null && c.tag == Constants.CONSTANT_Methodref) {
  709. ConstantMethodref cfr = (ConstantMethodref) c;
  710. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  711. // check the class
  712. int cIndex = cfr.getClassIndex();
  713. ConstantClass cc = (ConstantClass) pool[cIndex];
  714. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  715. if (!cName.equals(searchClassname))
  716. continue;
  717. // check the name and type
  718. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  719. if (!name.equals(searchMethodName))
  720. continue; // not this one
  721. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  722. if (!typeSignature.equals(searchSignature))
  723. continue;
  724. methodCache.put(key, i);
  725. return i;
  726. }
  727. }
  728. return -1;
  729. }
  730. public ConstantPool getFinalConstantPool() {
  731. Constant[] cs = new Constant[poolSize]; // create it the exact size we need
  732. System.arraycopy(pool, 0, cs, 0, poolSize);
  733. return new ConstantPool(cs);
  734. }
  735. public String getModuleName(int moduleIndex) {
  736. return getConstantModule(moduleIndex).getModuleName(this);
  737. }
  738. public String getPackageName(int packageIndex) {
  739. return getConstantPackage(packageIndex).getPackageName(this);
  740. }
  741. }