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

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