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

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