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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 String getConstantString_CONSTANTClass(int index) {
  250. ConstantClass c = (ConstantClass) getConstant(index, Constants.CONSTANT_Class);
  251. index = c.getNameIndex();
  252. return ((ConstantUtf8) getConstant(index, Constants.CONSTANT_Utf8)).getValue();
  253. }
  254. public int getLength() {
  255. return poolSize;
  256. }
  257. @Override
  258. public String toString() {
  259. StringBuffer buf = new StringBuffer();
  260. for (int i = 1; i < poolSize; i++)
  261. buf.append(i + ")" + pool[i] + "\n");
  262. return buf.toString();
  263. }
  264. public int lookupInteger(int n) {
  265. for (int i = 1; i < poolSize; i++) {
  266. if (pool[i] instanceof ConstantInteger) {
  267. ConstantInteger c = (ConstantInteger) pool[i];
  268. if (c.getValue() == n)
  269. return i;
  270. }
  271. }
  272. return -1;
  273. }
  274. public int lookupUtf8(String string) {
  275. Integer pos = utf8Cache.get(string);
  276. if (pos != null) {
  277. return pos;
  278. }
  279. for (int i = 1; i < poolSize; i++) {
  280. Constant c = pool[i];
  281. if (c != null && c.tag == Constants.CONSTANT_Utf8) {
  282. if (((ConstantUtf8) c).getValue().equals(string)) {
  283. utf8Cache.put(string, i);
  284. return i;
  285. }
  286. }
  287. }
  288. return -1;
  289. }
  290. public int lookupClass(String classname) {
  291. for (int i = 1; i < poolSize; i++) {
  292. Constant c = pool[i];
  293. if (c != null && c.tag == Constants.CONSTANT_Class) {
  294. int cIndex = ((ConstantClass) c).getNameIndex();
  295. String cName = ((ConstantUtf8) pool[cIndex]).getValue();
  296. if (cName.equals(classname))
  297. return i;
  298. }
  299. }
  300. return -1;
  301. }
  302. public int addUtf8(String n) {
  303. int ret = lookupUtf8(n);
  304. if (ret != -1)
  305. return ret;
  306. adjustSize();
  307. ret = poolSize;
  308. pool[poolSize++] = new ConstantUtf8(n);
  309. return ret;
  310. }
  311. public int addInteger(int n) {
  312. int ret = lookupInteger(n);
  313. if (ret != -1)
  314. return ret;
  315. adjustSize();
  316. ret = poolSize;
  317. pool[poolSize++] = new ConstantInteger(n);
  318. return ret;
  319. }
  320. public int addArrayClass(ArrayType type) {
  321. return addClass(type.getSignature());
  322. }
  323. public int addClass(ObjectType type) {
  324. return addClass(type.getClassName());
  325. }
  326. public int addClass(String classname) {
  327. String toAdd = classname.replace('.', '/');
  328. int ret = lookupClass(toAdd);
  329. if (ret != -1)
  330. return ret;
  331. adjustSize();
  332. ConstantClass c = new ConstantClass(addUtf8(toAdd));
  333. ret = poolSize;
  334. pool[poolSize++] = c;
  335. return ret;
  336. }
  337. private void adjustSize() {
  338. if (poolSize + 3 >= pool.length) {
  339. Constant[] cs = pool;
  340. pool = new Constant[cs.length + 8];
  341. System.arraycopy(cs, 0, pool, 0, cs.length);
  342. }
  343. if (poolSize == 0)
  344. poolSize = 1; // someone about to do something in here!
  345. }
  346. public int addFieldref(String class_name, String field_name, String signature) {
  347. int ret = lookupFieldref(class_name, field_name, signature);
  348. int class_index, name_and_type_index;
  349. if (ret != -1)
  350. return ret;
  351. adjustSize();
  352. class_index = addClass(class_name);
  353. name_and_type_index = addNameAndType(field_name, signature);
  354. ret = poolSize;
  355. pool[poolSize++] = new ConstantFieldref(class_index, name_and_type_index);
  356. return ret;
  357. }
  358. public int lookupFieldref(String searchClassname, String searchFieldname, String searchSignature) {
  359. searchClassname = searchClassname.replace('.', '/');
  360. String k = new StringBuffer().append(searchClassname).append(searchFieldname).append(searchSignature).toString();
  361. Integer pos = fieldCache.get(k);
  362. if (pos != null)
  363. return pos.intValue();
  364. for (int i = 1; i < poolSize; i++) {
  365. Constant c = pool[i];
  366. if (c != null && c.tag == Constants.CONSTANT_Fieldref) {
  367. ConstantFieldref cfr = (ConstantFieldref) c;
  368. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  369. // check the class
  370. int cIndex = cfr.getClassIndex();
  371. ConstantClass cc = (ConstantClass) pool[cIndex];
  372. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  373. if (!cName.equals(searchClassname))
  374. continue;
  375. // check the name and type
  376. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  377. if (!name.equals(searchFieldname))
  378. continue; // not this one
  379. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  380. if (!typeSignature.equals(searchSignature))
  381. continue;
  382. fieldCache.put(k, new Integer(i));
  383. return i;
  384. }
  385. }
  386. return -1;
  387. }
  388. public int addNameAndType(String name, String signature) {
  389. int ret = lookupNameAndType(name, signature);
  390. if (ret != -1)
  391. return ret;
  392. adjustSize();
  393. int name_index = addUtf8(name);
  394. int signature_index = addUtf8(signature);
  395. ret = poolSize;
  396. pool[poolSize++] = new ConstantNameAndType(name_index, signature_index);
  397. return ret;
  398. }
  399. public int lookupNameAndType(String searchName, String searchTypeSignature) {
  400. for (int i = 1; i < poolSize; i++) {
  401. Constant c = pool[i];
  402. if (c != null && c.tag == Constants.CONSTANT_NameAndType) {
  403. ConstantNameAndType cnat = (ConstantNameAndType) c;
  404. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  405. if (!name.equals(searchName))
  406. continue; // not this one
  407. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  408. if (!typeSignature.equals(searchTypeSignature))
  409. continue;
  410. return i;
  411. }
  412. }
  413. return -1;
  414. }
  415. public int addFloat(float f) {
  416. int ret = lookupFloat(f);
  417. if (ret != -1)
  418. return ret;
  419. adjustSize();
  420. ret = poolSize;
  421. pool[poolSize++] = new ConstantFloat(f);
  422. return ret;
  423. }
  424. public int lookupFloat(float f) {
  425. int bits = Float.floatToIntBits(f);
  426. for (int i = 1; i < poolSize; i++) {
  427. Constant c = pool[i];
  428. if (c != null && c.tag == Constants.CONSTANT_Float) {
  429. ConstantFloat cf = (ConstantFloat) c;
  430. if (Float.floatToIntBits(cf.getValue()) == bits)
  431. return i;
  432. }
  433. }
  434. return -1;
  435. }
  436. public int addDouble(double d) {
  437. int ret = lookupDouble(d);
  438. if (ret != -1)
  439. return ret;
  440. adjustSize();
  441. ret = poolSize;
  442. pool[poolSize] = new ConstantDouble(d);
  443. poolSize += 2;
  444. return ret;
  445. }
  446. public int lookupDouble(double d) {
  447. long bits = Double.doubleToLongBits(d);
  448. for (int i = 1; i < poolSize; i++) {
  449. Constant c = pool[i];
  450. if (c != null && c.tag == Constants.CONSTANT_Double) {
  451. ConstantDouble cf = (ConstantDouble) c;
  452. if (Double.doubleToLongBits(cf.getValue()) == bits)
  453. return i;
  454. }
  455. }
  456. return -1;
  457. }
  458. public int addLong(long l) {
  459. int ret = lookupLong(l);
  460. if (ret != -1)
  461. return ret;
  462. adjustSize();
  463. ret = poolSize;
  464. pool[poolSize] = new ConstantLong(l);
  465. poolSize += 2;
  466. return ret;
  467. }
  468. public int lookupString(String s) {
  469. for (int i = 1; i < poolSize; i++) {
  470. Constant c = pool[i];
  471. if (c != null && c.tag == Constants.CONSTANT_String) {
  472. ConstantString cs = (ConstantString) c;
  473. ConstantUtf8 cu8 = (ConstantUtf8) pool[cs.getStringIndex()];
  474. if (cu8.getValue().equals(s))
  475. return i;
  476. }
  477. }
  478. return -1;
  479. }
  480. public int addString(String str) {
  481. int ret = lookupString(str);
  482. if (ret != -1)
  483. return ret;
  484. int utf8 = addUtf8(str);
  485. adjustSize();
  486. ConstantString s = new ConstantString(utf8);
  487. ret = poolSize;
  488. pool[poolSize++] = s;
  489. return ret;
  490. }
  491. public int lookupLong(long l) {
  492. for (int i = 1; i < poolSize; i++) {
  493. Constant c = pool[i];
  494. if (c != null && c.tag == Constants.CONSTANT_Long) {
  495. ConstantLong cf = (ConstantLong) c;
  496. if (cf.getValue() == l)
  497. return i;
  498. }
  499. }
  500. return -1;
  501. }
  502. public int addConstant(Constant c, ConstantPool cp) {
  503. Constant[] constants = cp.getConstantPool();
  504. switch (c.getTag()) {
  505. case Constants.CONSTANT_String: {
  506. ConstantString s = (ConstantString) c;
  507. ConstantUtf8 u8 = (ConstantUtf8) constants[s.getStringIndex()];
  508. return addString(u8.getValue());
  509. }
  510. case Constants.CONSTANT_Class: {
  511. ConstantClass s = (ConstantClass) c;
  512. ConstantUtf8 u8 = (ConstantUtf8) constants[s.getNameIndex()];
  513. return addClass(u8.getValue());
  514. }
  515. case Constants.CONSTANT_NameAndType: {
  516. ConstantNameAndType n = (ConstantNameAndType) c;
  517. ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()];
  518. ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()];
  519. return addNameAndType(u8.getValue(), u8_2.getValue());
  520. }
  521. case Constants.CONSTANT_Utf8:
  522. return addUtf8(((ConstantUtf8) c).getValue());
  523. case Constants.CONSTANT_Double:
  524. return addDouble(((ConstantDouble) c).getValue());
  525. case Constants.CONSTANT_Float:
  526. return addFloat(((ConstantFloat) c).getValue());
  527. case Constants.CONSTANT_Long:
  528. return addLong(((ConstantLong) c).getValue());
  529. case Constants.CONSTANT_Integer:
  530. return addInteger(((ConstantInteger) c).getValue());
  531. case Constants.CONSTANT_InterfaceMethodref:
  532. case Constants.CONSTANT_Methodref:
  533. case Constants.CONSTANT_Fieldref: {
  534. ConstantCP m = (ConstantCP) c;
  535. ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
  536. ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
  537. ConstantUtf8 u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
  538. String class_name = u8.getValue().replace('/', '.');
  539. u8 = (ConstantUtf8) constants[n.getNameIndex()];
  540. String name = u8.getValue();
  541. u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
  542. String signature = u8.getValue();
  543. switch (c.getTag()) {
  544. case Constants.CONSTANT_InterfaceMethodref:
  545. return addInterfaceMethodref(class_name, name, signature);
  546. case Constants.CONSTANT_Methodref:
  547. return addMethodref(class_name, name, signature); // OPTIMIZE indicate it should be cached!
  548. case Constants.CONSTANT_Fieldref:
  549. return addFieldref(class_name, name, signature);
  550. default: // Never reached
  551. throw new RuntimeException("Unknown constant type " + c);
  552. }
  553. }
  554. default: // Never reached
  555. throw new RuntimeException("Unknown constant type " + c);
  556. }
  557. }
  558. // OPTIMIZE should put it in the cache now
  559. public int addMethodref(String class_name, String method_name, String signature) {
  560. int ret, class_index, name_and_type_index;
  561. if ((ret = lookupMethodref(class_name, method_name, signature)) != -1)
  562. return ret; // Already in CP
  563. adjustSize();
  564. name_and_type_index = addNameAndType(method_name, signature);
  565. class_index = addClass(class_name);
  566. ret = poolSize;
  567. pool[poolSize++] = new ConstantMethodref(class_index, name_and_type_index);
  568. return ret;
  569. }
  570. public int addInterfaceMethodref(String class_name, String method_name, String signature) {
  571. int ret = lookupInterfaceMethodref(class_name, method_name, signature);
  572. int class_index, name_and_type_index;
  573. if (ret != -1)
  574. return ret;
  575. adjustSize();
  576. class_index = addClass(class_name);
  577. name_and_type_index = addNameAndType(method_name, signature);
  578. ret = poolSize;
  579. pool[poolSize++] = new ConstantInterfaceMethodref(class_index, name_and_type_index);
  580. return ret;
  581. }
  582. public int lookupInterfaceMethodref(String searchClassname, String searchMethodName, String searchSignature) {
  583. searchClassname = searchClassname.replace('.', '/');
  584. for (int i = 1; i < poolSize; i++) {
  585. Constant c = pool[i];
  586. if (c != null && c.tag == Constants.CONSTANT_InterfaceMethodref) {
  587. ConstantInterfaceMethodref cfr = (ConstantInterfaceMethodref) c;
  588. ConstantClass cc = (ConstantClass) pool[cfr.getClassIndex()];
  589. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  590. if (!cName.equals(searchClassname))
  591. continue;
  592. // check the name and type
  593. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  594. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  595. if (!name.equals(searchMethodName))
  596. continue; // not this one
  597. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  598. if (!typeSignature.equals(searchSignature))
  599. continue;
  600. return i;
  601. }
  602. }
  603. return -1;
  604. }
  605. public int lookupMethodref(String searchClassname, String searchMethodName, String searchSignature) {
  606. String key = new StringBuffer().append(searchClassname).append(searchMethodName).append(searchSignature).toString();
  607. Integer cached = methodCache.get(key);
  608. if (cached != null)
  609. return cached.intValue();
  610. searchClassname = searchClassname.replace('.', '/');
  611. for (int i = 1; i < poolSize; i++) {
  612. Constant c = pool[i];
  613. if (c != null && c.tag == Constants.CONSTANT_Methodref) {
  614. ConstantMethodref cfr = (ConstantMethodref) c;
  615. ConstantNameAndType cnat = (ConstantNameAndType) pool[cfr.getNameAndTypeIndex()];
  616. // check the class
  617. int cIndex = cfr.getClassIndex();
  618. ConstantClass cc = (ConstantClass) pool[cIndex];
  619. String cName = ((ConstantUtf8) pool[cc.getNameIndex()]).getValue();
  620. if (!cName.equals(searchClassname))
  621. continue;
  622. // check the name and type
  623. String name = ((ConstantUtf8) pool[cnat.getNameIndex()]).getValue();
  624. if (!name.equals(searchMethodName))
  625. continue; // not this one
  626. String typeSignature = ((ConstantUtf8) pool[cnat.getSignatureIndex()]).getValue();
  627. if (!typeSignature.equals(searchSignature))
  628. continue;
  629. methodCache.put(key, new Integer(i));
  630. return i;
  631. }
  632. }
  633. return -1;
  634. }
  635. public ConstantPool getFinalConstantPool() {
  636. Constant[] cs = new Constant[poolSize]; // create it the exact size we need
  637. System.arraycopy(pool, 0, cs, 0, poolSize);
  638. return new ConstantPool(cs);
  639. }
  640. }