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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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 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<String, Integer>();
  71. private Map<String, Integer> methodCache = new HashMap<String, Integer>();
  72. private Map<String, Integer> fieldCache = new HashMap<String, Integer>();
  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. default: // Never reached
  193. throw new RuntimeException("Unknown constant type " + c.tag);
  194. }
  195. return str;
  196. }
  197. private static final String escape(String str) {
  198. int len = str.length();
  199. StringBuffer buf = new StringBuffer(len + 5);
  200. char[] ch = str.toCharArray();
  201. for (int i = 0; i < len; i++) {
  202. switch (ch[i]) {
  203. case '\n':
  204. buf.append("\\n");
  205. break;
  206. case '\r':
  207. buf.append("\\r");
  208. break;
  209. case '\t':
  210. buf.append("\\t");
  211. break;
  212. case '\b':
  213. buf.append("\\b");
  214. break;
  215. case '"':
  216. buf.append("\\\"");
  217. break;
  218. default:
  219. buf.append(ch[i]);
  220. }
  221. }
  222. return buf.toString();
  223. }
  224. public String constantToString(int index, byte tag) {
  225. Constant c = getConstant(index, tag);
  226. return constantToString(c);
  227. }
  228. public String constantToString(int index) {
  229. return constantToString(getConstant(index));
  230. }
  231. public void accept(ClassVisitor v) {
  232. v.visitConstantPool(this);
  233. }
  234. public Constant[] getConstantPool() {
  235. return pool;
  236. } // TEMPORARY, DONT LIKE PASSING THIS DATA OUT!
  237. public void dump(DataOutputStream file) throws IOException {
  238. file.writeShort(poolSize);
  239. for (int i = 1; i < poolSize; i++)
  240. if (pool[i] != null)
  241. pool[i].dump(file);
  242. }
  243. public ConstantUtf8 getConstantUtf8(int index) {
  244. Constant c = getConstant(index);
  245. assert c != null;
  246. assert c.tag == Constants.CONSTANT_Utf8;
  247. return (ConstantUtf8) c;
  248. }
  249. public ConstantModule getConstantModule(int index) {
  250. Constant c = getConstant(index);
  251. assert c != null;
  252. assert c.tag == Constants.CONSTANT_Module;
  253. return (ConstantModule)c;
  254. }
  255. public ConstantPackage getConstantPackage(int index) {
  256. Constant c = getConstant(index);
  257. assert c != null;
  258. assert c.tag == Constants.CONSTANT_Package;
  259. return (ConstantPackage)c;
  260. }
  261. public String getConstantString_CONSTANTClass(int index) {
  262. ConstantClass c = (ConstantClass) getConstant(index, Constants.CONSTANT_Class);
  263. index = c.getNameIndex();
  264. return ((ConstantUtf8) getConstant(index, Constants.CONSTANT_Utf8)).getValue();
  265. }
  266. public int getLength() {
  267. return poolSize;
  268. }
  269. @Override
  270. public String toString() {
  271. StringBuffer buf = new StringBuffer();
  272. for (int i = 1; i < poolSize; i++)
  273. buf.append(i + ")" + pool[i] + "\n");
  274. return buf.toString();
  275. }
  276. public int lookupInteger(int n) {
  277. for (int i = 1; i < poolSize; i++) {
  278. if (pool[i] instanceof ConstantInteger) {
  279. ConstantInteger c = (ConstantInteger) pool[i];
  280. if (c.getValue() == n)
  281. return i;
  282. }
  283. }
  284. return -1;
  285. }
  286. public int lookupUtf8(String string) {
  287. Integer pos = utf8Cache.get(string);
  288. if (pos != null) {
  289. return pos;
  290. }
  291. for (int i = 1; i < poolSize; i++) {
  292. Constant c = pool[i];
  293. if (c != null && c.tag == Constants.CONSTANT_Utf8) {
  294. if (((ConstantUtf8) c).getValue().equals(string)) {
  295. utf8Cache.put(string, i);
  296. return i;
  297. }
  298. }
  299. }
  300. return -1;
  301. }
  302. public int lookupClass(String classname) {
  303. for (int i = 1; i < poolSize; i++) {
  304. Constant c = pool[i];
  305. if (c != null && c.tag == Constants.CONSTANT_Class) {
  306. int cIndex = ((ConstantClass) c).getNameIndex();
  307. String cName = ((ConstantUtf8) pool[cIndex]).getValue();
  308. if (cName.equals(classname))
  309. return i;
  310. }
  311. }
  312. return -1;
  313. }
  314. public int addUtf8(String n) {
  315. int ret = lookupUtf8(n);
  316. if (ret != -1)
  317. return ret;
  318. adjustSize();
  319. ret = poolSize;
  320. pool[poolSize++] = new ConstantUtf8(n);
  321. return ret;
  322. }
  323. public int addInteger(int n) {
  324. int ret = lookupInteger(n);
  325. if (ret != -1)
  326. return ret;
  327. adjustSize();
  328. ret = poolSize;
  329. pool[poolSize++] = new ConstantInteger(n);
  330. return ret;
  331. }
  332. public int addArrayClass(ArrayType type) {
  333. return addClass(type.getSignature());
  334. }
  335. public int addClass(ObjectType type) {
  336. return addClass(type.getClassName());
  337. }
  338. public int addClass(String classname) {
  339. String toAdd = classname.replace('.', '/');
  340. int ret = lookupClass(toAdd);
  341. if (ret != -1)
  342. return ret;
  343. adjustSize();
  344. ConstantClass c = new ConstantClass(addUtf8(toAdd));
  345. ret = poolSize;
  346. pool[poolSize++] = c;
  347. return ret;
  348. }
  349. private void adjustSize() {
  350. if (poolSize + 3 >= pool.length) {
  351. Constant[] cs = pool;
  352. pool = new Constant[cs.length + 8];
  353. System.arraycopy(cs, 0, pool, 0, cs.length);
  354. }
  355. if (poolSize == 0)
  356. poolSize = 1; // someone about to do something in here!
  357. }
  358. public int addFieldref(String class_name, String field_name, String signature) {
  359. int ret = lookupFieldref(class_name, field_name, signature);
  360. int class_index, name_and_type_index;
  361. if (ret != -1)
  362. return ret;
  363. adjustSize();
  364. class_index = addClass(class_name);
  365. name_and_type_index = addNameAndType(field_name, signature);
  366. ret = poolSize;
  367. pool[poolSize++] = new ConstantFieldref(class_index, name_and_type_index);
  368. return ret;
  369. }
  370. public int lookupFieldref(String searchClassname, String searchFieldname, String searchSignature) {
  371. searchClassname = searchClassname.replace('.', '/');
  372. String k = new StringBuffer().append(searchClassname).append(searchFieldname).append(searchSignature).toString();
  373. Integer pos = fieldCache.get(k);
  374. if (pos != null)
  375. return pos.intValue();
  376. for (int i = 1; i < poolSize; i++) {
  377. Constant c = pool[i];
  378. if (c != null && c.tag == Constants.CONSTANT_Fieldref) {
  379. ConstantFieldref cfr = (ConstantFieldref) c;
  380. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  381. // check the class
  382. int cIndex = cfr.getClassIndex();
  383. ConstantClass cc = (ConstantClass) pool[cIndex];
  384. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  385. if (!cName.equals(searchClassname))
  386. continue;
  387. // check the name and type
  388. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  389. if (!name.equals(searchFieldname))
  390. continue; // not this one
  391. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  392. if (!typeSignature.equals(searchSignature))
  393. continue;
  394. fieldCache.put(k, new Integer(i));
  395. return i;
  396. }
  397. }
  398. return -1;
  399. }
  400. public int addNameAndType(String name, String signature) {
  401. int ret = lookupNameAndType(name, signature);
  402. if (ret != -1)
  403. return ret;
  404. adjustSize();
  405. int name_index = addUtf8(name);
  406. int signature_index = addUtf8(signature);
  407. ret = poolSize;
  408. pool[poolSize++] = new ConstantNameAndType(name_index, signature_index);
  409. return ret;
  410. }
  411. public int lookupNameAndType(String searchName, String searchTypeSignature) {
  412. for (int i = 1; i < poolSize; i++) {
  413. Constant c = pool[i];
  414. if (c != null && c.tag == Constants.CONSTANT_NameAndType) {
  415. ConstantNameAndType cnat = (ConstantNameAndType) c;
  416. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  417. if (!name.equals(searchName))
  418. continue; // not this one
  419. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  420. if (!typeSignature.equals(searchTypeSignature))
  421. continue;
  422. return i;
  423. }
  424. }
  425. return -1;
  426. }
  427. public int addFloat(float f) {
  428. int ret = lookupFloat(f);
  429. if (ret != -1)
  430. return ret;
  431. adjustSize();
  432. ret = poolSize;
  433. pool[poolSize++] = new ConstantFloat(f);
  434. return ret;
  435. }
  436. public int lookupFloat(float f) {
  437. int bits = Float.floatToIntBits(f);
  438. for (int i = 1; i < poolSize; i++) {
  439. Constant c = pool[i];
  440. if (c != null && c.tag == Constants.CONSTANT_Float) {
  441. ConstantFloat cf = (ConstantFloat) c;
  442. if (Float.floatToIntBits(cf.getValue()) == bits)
  443. return i;
  444. }
  445. }
  446. return -1;
  447. }
  448. public int addDouble(double d) {
  449. int ret = lookupDouble(d);
  450. if (ret != -1)
  451. return ret;
  452. adjustSize();
  453. ret = poolSize;
  454. pool[poolSize] = new ConstantDouble(d);
  455. poolSize += 2;
  456. return ret;
  457. }
  458. public int lookupDouble(double d) {
  459. long bits = Double.doubleToLongBits(d);
  460. for (int i = 1; i < poolSize; i++) {
  461. Constant c = pool[i];
  462. if (c != null && c.tag == Constants.CONSTANT_Double) {
  463. ConstantDouble cf = (ConstantDouble) c;
  464. if (Double.doubleToLongBits(cf.getValue()) == bits)
  465. return i;
  466. }
  467. }
  468. return -1;
  469. }
  470. public int addLong(long l) {
  471. int ret = lookupLong(l);
  472. if (ret != -1)
  473. return ret;
  474. adjustSize();
  475. ret = poolSize;
  476. pool[poolSize] = new ConstantLong(l);
  477. poolSize += 2;
  478. return ret;
  479. }
  480. public int lookupString(String s) {
  481. for (int i = 1; i < poolSize; i++) {
  482. Constant c = pool[i];
  483. if (c != null && c.tag == Constants.CONSTANT_String) {
  484. ConstantString cs = (ConstantString) c;
  485. ConstantUtf8 cu8 = (ConstantUtf8) pool[cs.getStringIndex()];
  486. if (cu8.getValue().equals(s))
  487. return i;
  488. }
  489. }
  490. return -1;
  491. }
  492. public int addString(String str) {
  493. int ret = lookupString(str);
  494. if (ret != -1)
  495. return ret;
  496. int utf8 = addUtf8(str);
  497. adjustSize();
  498. ConstantString s = new ConstantString(utf8);
  499. ret = poolSize;
  500. pool[poolSize++] = s;
  501. return ret;
  502. }
  503. public int lookupLong(long l) {
  504. for (int i = 1; i < poolSize; i++) {
  505. Constant c = pool[i];
  506. if (c != null && c.tag == Constants.CONSTANT_Long) {
  507. ConstantLong cf = (ConstantLong) c;
  508. if (cf.getValue() == l)
  509. return i;
  510. }
  511. }
  512. return -1;
  513. }
  514. public int addConstant(Constant c, ConstantPool cp) {
  515. Constant[] constants = cp.getConstantPool();
  516. switch (c.getTag()) {
  517. case Constants.CONSTANT_String: {
  518. ConstantString s = (ConstantString) c;
  519. ConstantUtf8 u8 = (ConstantUtf8) constants[s.getStringIndex()];
  520. return addString(u8.getValue());
  521. }
  522. case Constants.CONSTANT_Class: {
  523. ConstantClass s = (ConstantClass) c;
  524. ConstantUtf8 u8 = (ConstantUtf8) constants[s.getNameIndex()];
  525. return addClass(u8.getValue());
  526. }
  527. case Constants.CONSTANT_NameAndType: {
  528. ConstantNameAndType n = (ConstantNameAndType) c;
  529. ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()];
  530. ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()];
  531. return addNameAndType(u8.getValue(), u8_2.getValue());
  532. }
  533. case Constants.CONSTANT_InvokeDynamic: {
  534. ConstantInvokeDynamic cid = (ConstantInvokeDynamic)c;
  535. int index1 = cid.getBootstrapMethodAttrIndex();
  536. ConstantNameAndType cnat = (ConstantNameAndType)constants[cid.getNameAndTypeIndex()];
  537. ConstantUtf8 name = (ConstantUtf8) constants[cnat.getNameIndex()];
  538. ConstantUtf8 signature = (ConstantUtf8) constants[cnat.getSignatureIndex()];
  539. int index2 = addNameAndType(name.getValue(), signature.getValue());
  540. return addInvokeDynamic(index1,index2);
  541. }
  542. case Constants.CONSTANT_MethodHandle:
  543. ConstantMethodHandle cmh = (ConstantMethodHandle)c;
  544. return addMethodHandle(cmh.getReferenceKind(),addConstant(constants[cmh.getReferenceIndex()],cp));
  545. case Constants.CONSTANT_Utf8:
  546. return addUtf8(((ConstantUtf8) c).getValue());
  547. case Constants.CONSTANT_Double:
  548. return addDouble(((ConstantDouble) c).getValue());
  549. case Constants.CONSTANT_Float:
  550. return addFloat(((ConstantFloat) c).getValue());
  551. case Constants.CONSTANT_Long:
  552. return addLong(((ConstantLong) c).getValue());
  553. case Constants.CONSTANT_Integer:
  554. return addInteger(((ConstantInteger) c).getValue());
  555. case Constants.CONSTANT_MethodType:
  556. ConstantMethodType cmt = (ConstantMethodType)c;
  557. return addMethodType(addConstant(constants[cmt.getDescriptorIndex()],cp));
  558. case Constants.CONSTANT_InterfaceMethodref:
  559. case Constants.CONSTANT_Methodref:
  560. case Constants.CONSTANT_Fieldref: {
  561. ConstantCP m = (ConstantCP) c;
  562. ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
  563. ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
  564. ConstantUtf8 u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
  565. String class_name = u8.getValue().replace('/', '.');
  566. u8 = (ConstantUtf8) constants[n.getNameIndex()];
  567. String name = u8.getValue();
  568. u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
  569. String signature = u8.getValue();
  570. switch (c.getTag()) {
  571. case Constants.CONSTANT_InterfaceMethodref:
  572. return addInterfaceMethodref(class_name, name, signature);
  573. case Constants.CONSTANT_Methodref:
  574. return addMethodref(class_name, name, signature); // OPTIMIZE indicate it should be cached!
  575. case Constants.CONSTANT_Fieldref:
  576. return addFieldref(class_name, name, signature);
  577. default: // Never reached
  578. throw new RuntimeException("Unknown constant type " + c);
  579. }
  580. }
  581. default: // Never reached
  582. throw new RuntimeException("Unknown constant type " + c);
  583. }
  584. }
  585. public int addMethodHandle(byte referenceKind, int referenceIndex) {
  586. adjustSize();
  587. int ret = poolSize;
  588. pool[poolSize++] = new ConstantMethodHandle(referenceKind, referenceIndex);
  589. return ret;
  590. }
  591. public int addMethodType(int descriptorIndex) {
  592. adjustSize();
  593. int ret = poolSize;
  594. pool[poolSize++] = new ConstantMethodType(descriptorIndex);
  595. return ret;
  596. }
  597. // OPTIMIZE should put it in the cache now
  598. public int addMethodref(String class_name, String method_name, String signature) {
  599. int ret, class_index, name_and_type_index;
  600. if ((ret = lookupMethodref(class_name, method_name, signature)) != -1)
  601. return ret; // Already in CP
  602. adjustSize();
  603. name_and_type_index = addNameAndType(method_name, signature);
  604. class_index = addClass(class_name);
  605. ret = poolSize;
  606. pool[poolSize++] = new ConstantMethodref(class_index, name_and_type_index);
  607. return ret;
  608. }
  609. public int addInvokeDynamic(int bootstrapMethodIndex, int constantNameAndTypeIndex) {
  610. adjustSize();
  611. int ret = poolSize;
  612. pool[poolSize++] = new ConstantInvokeDynamic(bootstrapMethodIndex, constantNameAndTypeIndex);
  613. return ret;
  614. }
  615. public int addInterfaceMethodref(String class_name, String method_name, String signature) {
  616. int ret = lookupInterfaceMethodref(class_name, method_name, signature);
  617. int class_index, name_and_type_index;
  618. if (ret != -1)
  619. return ret;
  620. adjustSize();
  621. class_index = addClass(class_name);
  622. name_and_type_index = addNameAndType(method_name, signature);
  623. ret = poolSize;
  624. pool[poolSize++] = new ConstantInterfaceMethodref(class_index, name_and_type_index);
  625. return ret;
  626. }
  627. public int lookupInterfaceMethodref(String searchClassname, String searchMethodName, String searchSignature) {
  628. searchClassname = searchClassname.replace('.', '/');
  629. for (int i = 1; i < poolSize; i++) {
  630. Constant c = pool[i];
  631. if (c != null && c.tag == Constants.CONSTANT_InterfaceMethodref) {
  632. ConstantInterfaceMethodref cfr = (ConstantInterfaceMethodref) c;
  633. ConstantClass cc = (ConstantClass) pool[cfr.getClassIndex()];
  634. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  635. if (!cName.equals(searchClassname))
  636. continue;
  637. // check the name and type
  638. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  639. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  640. if (!name.equals(searchMethodName))
  641. continue; // not this one
  642. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  643. if (!typeSignature.equals(searchSignature))
  644. continue;
  645. return i;
  646. }
  647. }
  648. return -1;
  649. }
  650. public int lookupMethodref(String searchClassname, String searchMethodName, String searchSignature) {
  651. String key = new StringBuffer().append(searchClassname).append(searchMethodName).append(searchSignature).toString();
  652. Integer cached = methodCache.get(key);
  653. if (cached != null)
  654. return cached.intValue();
  655. searchClassname = searchClassname.replace('.', '/');
  656. for (int i = 1; i < poolSize; i++) {
  657. Constant c = pool[i];
  658. if (c != null && c.tag == Constants.CONSTANT_Methodref) {
  659. ConstantMethodref cfr = (ConstantMethodref) c;
  660. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  661. // check the class
  662. int cIndex = cfr.getClassIndex();
  663. ConstantClass cc = (ConstantClass) pool[cIndex];
  664. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  665. if (!cName.equals(searchClassname))
  666. continue;
  667. // check the name and type
  668. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  669. if (!name.equals(searchMethodName))
  670. continue; // not this one
  671. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  672. if (!typeSignature.equals(searchSignature))
  673. continue;
  674. methodCache.put(key, new Integer(i));
  675. return i;
  676. }
  677. }
  678. return -1;
  679. }
  680. public ConstantPool getFinalConstantPool() {
  681. Constant[] cs = new Constant[poolSize]; // create it the exact size we need
  682. System.arraycopy(pool, 0, cs, 0, poolSize);
  683. return new ConstantPool(cs);
  684. }
  685. public String getModuleName(int moduleIndex) {
  686. return getConstantModule(moduleIndex).getModuleName(this);
  687. }
  688. public String getPackageName(int packageIndex) {
  689. return getConstantPackage(packageIndex).getPackageName(this);
  690. }
  691. }