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.

AnnotationsAttribute.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.util.Map;
  18. import java.util.HashMap;
  19. import java.io.IOException;
  20. import java.io.DataInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import javassist.bytecode.annotation.*;
  23. /**
  24. * A class representing
  25. * <code>RuntimeVisibleAnnotations_attribute</code> and
  26. * <code>RuntimeInvisibleAnnotations_attribute</code>.
  27. *
  28. * <p>To obtain an AnnotationAttribute object, invoke
  29. * <code>getAttribute(AnnotationsAttribute.visibleTag)</code>
  30. * in <code>ClassFile</code>, <code>MethodInfo</code>,
  31. * or <code>FieldInfo</code>. The obtained attribute is a
  32. * runtime visible annotations attribute.
  33. * If the parameter is
  34. * <code>AnnotationAttribute.invisibleTag</code>, then the obtained
  35. * attribute is a runtime invisible one.
  36. *
  37. * <p>For example,
  38. *
  39. * <pre>
  40. * import javassist.bytecode.annotation.Annotation;
  41. * :
  42. * CtMethod m = ... ;
  43. * MethodInfo minfo = m.getMethodInfo();
  44. * AnnotationsAttribute attr = (AnnotationsAttribute)
  45. * minfo.getAttribute(AnnotationsAttribute.invisibleTag);
  46. * Annotation an = attr.getAnnotation("Author");
  47. * String s = ((StringMemberValue)an.getMemberValue("name")).getValue();
  48. * System.out.println("@Author(name=" + s + ")");
  49. * </pre>
  50. *
  51. * <p>This code snippet retrieves an annotation of the type <code>Author</code>
  52. * from the <code>MethodInfo</code> object specified by <code>minfo</code>.
  53. * Then, it prints the value of <code>name</code> in <code>Author</code>.
  54. *
  55. * <p>If the annotation type <code>Author</code> is annotated by a meta annotation:
  56. *
  57. * <pre>
  58. * &#64;Retention(RetentionPolicy.RUNTIME)
  59. * </pre>
  60. *
  61. * <p>Then <code>Author</code> is visible at runtime. Therefore, the third
  62. * statement of the code snippet above must be changed into:
  63. *
  64. * <pre>
  65. * AnnotationsAttribute attr = (AnnotationsAttribute)
  66. * minfo.getAttribute(AnnotationsAttribute.visibleTag);
  67. * </pre>
  68. *
  69. * <p>The attribute tag must be <code>visibleTag</code> instead of
  70. * <code>invisibleTag</code>.
  71. *
  72. * <p>If the member value of an annotation is not specified, the default value
  73. * is used as that member value. If so, <code>getMemberValue()</code> in
  74. * <code>Annotation</code> returns <code>null</code>
  75. * since the default value is not included in the
  76. * <code>AnnotationsAttribute</code>. It is included in the
  77. * <code>AnnotationDefaultAttribute</code> of the method declared in the
  78. * annotation type.
  79. *
  80. * <p>If you want to record a new AnnotationAttribute object, execute the
  81. * following snippet:
  82. *
  83. * <pre>
  84. * ClassFile cf = ... ;
  85. * ConstPool cp = cf.getConstPool();
  86. * AnnotationsAttribute attr
  87. * = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
  88. * Annotation a = new Annotation("Author", cp);
  89. * a.addMemberValue("name", new StringMemberValue("Chiba", cp));
  90. * attr.setAnnotation(a);
  91. * cf.addAttribute(attr);
  92. * cf.setVersionToJava5();
  93. * </pre>
  94. *
  95. * <p>The last statement is necessary if the class file was produced by
  96. * <code>javac</code> of JDK 1.4 or earlier. Otherwise, it is not necessary.
  97. *
  98. * @see AnnotationDefaultAttribute
  99. * @see javassist.bytecode.annotation.Annotation
  100. */
  101. public class AnnotationsAttribute extends AttributeInfo {
  102. /**
  103. * The name of the <code>RuntimeVisibleAnnotations</code> attribute.
  104. */
  105. public static final String visibleTag = "RuntimeVisibleAnnotations";
  106. /**
  107. * The name of the <code>RuntimeInvisibleAnnotations</code> attribute.
  108. */
  109. public static final String invisibleTag = "RuntimeInvisibleAnnotations";
  110. /**
  111. * Constructs a <code>Runtime(In)VisibleAnnotations_attribute</code>.
  112. *
  113. * @param cp constant pool
  114. * @param attrname attribute name (<code>visibleTag</code> or
  115. * <code>invisibleTag</code>).
  116. * @param info the contents of this attribute. It does not
  117. * include <code>attribute_name_index</code> or
  118. * <code>attribute_length</code>.
  119. */
  120. public AnnotationsAttribute(ConstPool cp, String attrname, byte[] info) {
  121. super(cp, attrname, info);
  122. }
  123. /**
  124. * Constructs an empty
  125. * <code>Runtime(In)VisibleAnnotations_attribute</code>.
  126. * A new annotation can be later added to the created attribute
  127. * by <code>setAnnotations()</code>.
  128. *
  129. * @param cp constant pool
  130. * @param attrname attribute name (<code>visibleTag</code> or
  131. * <code>invisibleTag</code>).
  132. * @see #setAnnotations(Annotation[])
  133. */
  134. public AnnotationsAttribute(ConstPool cp, String attrname) {
  135. this(cp, attrname, new byte[] { 0, 0 });
  136. }
  137. /**
  138. * @param n the attribute name.
  139. */
  140. AnnotationsAttribute(ConstPool cp, int n, DataInputStream in)
  141. throws IOException
  142. {
  143. super(cp, n, in);
  144. }
  145. /**
  146. * Returns <code>num_annotations</code>.
  147. */
  148. public int numAnnotations() {
  149. return ByteArray.readU16bit(info, 0);
  150. }
  151. /**
  152. * Copies this attribute and returns a new copy.
  153. */
  154. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  155. Copier copier = new Copier(info, constPool, newCp, classnames);
  156. try {
  157. copier.annotationArray();
  158. return new AnnotationsAttribute(newCp, getName(), copier.close());
  159. }
  160. catch (Exception e) {
  161. throw new RuntimeException(e);
  162. }
  163. }
  164. /**
  165. * Parses the annotations and returns a data structure representing
  166. * the annotation with the specified type. See also
  167. * <code>getAnnotations()</code> as to the returned data structure.
  168. *
  169. * @param type the annotation type.
  170. * @return null if the specified annotation type is not included.
  171. * @see #getAnnotations()
  172. */
  173. public Annotation getAnnotation(String type) {
  174. Annotation[] annotations = getAnnotations();
  175. for (int i = 0; i < annotations.length; i++) {
  176. if (annotations[i].getTypeName().equals(type))
  177. return annotations[i];
  178. }
  179. return null;
  180. }
  181. /**
  182. * Adds an annotation. If there is an annotation with the same type,
  183. * it is removed before the new annotation is added.
  184. *
  185. * @param annotation the added annotation.
  186. */
  187. public void addAnnotation(Annotation annotation) {
  188. String type = annotation.getTypeName();
  189. Annotation[] annotations = getAnnotations();
  190. for (int i = 0; i < annotations.length; i++) {
  191. if (annotations[i].getTypeName().equals(type)) {
  192. annotations[i] = annotation;
  193. setAnnotations(annotations);
  194. return;
  195. }
  196. }
  197. Annotation[] newlist = new Annotation[annotations.length + 1];
  198. System.arraycopy(annotations, 0, newlist, 0, annotations.length);
  199. newlist[annotations.length] = annotation;
  200. setAnnotations(newlist);
  201. }
  202. /**
  203. * Removes an annotation by type.
  204. * After removing an annotation, if {@link #numAnnotations()} returns 0,
  205. * this annotations attribute has to be removed.
  206. *
  207. * @param type of annotation to remove
  208. * @return whether an annotation with the given type has been removed
  209. * @since 3.21
  210. */
  211. public boolean removeAnnotation(String type) {
  212. Annotation[] annotations = getAnnotations();
  213. for (int i = 0; i < annotations.length; i++) {
  214. if (annotations[i].getTypeName().equals(type)) {
  215. Annotation[] newlist = new Annotation[annotations.length - 1];
  216. System.arraycopy(annotations, 0, newlist, 0, i);
  217. if (i < annotations.length - 1) {
  218. System.arraycopy(annotations, i + 1, newlist, i,
  219. annotations.length - i - 1);
  220. }
  221. setAnnotations(newlist);
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. /**
  228. * Parses the annotations and returns a data structure representing
  229. * that parsed annotations. Note that changes of the node values of the
  230. * returned tree are not reflected on the annotations represented by
  231. * this object unless the tree is copied back to this object by
  232. * <code>setAnnotations()</code>.
  233. *
  234. * @see #setAnnotations(Annotation[])
  235. */
  236. public Annotation[] getAnnotations() {
  237. try {
  238. return new Parser(info, constPool).parseAnnotations();
  239. }
  240. catch (Exception e) {
  241. throw new RuntimeException(e);
  242. }
  243. }
  244. /**
  245. * Changes the annotations represented by this object according to
  246. * the given array of <code>Annotation</code> objects.
  247. *
  248. * @param annotations the data structure representing the
  249. * new annotations.
  250. */
  251. public void setAnnotations(Annotation[] annotations) {
  252. ByteArrayOutputStream output = new ByteArrayOutputStream();
  253. AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
  254. try {
  255. int n = annotations.length;
  256. writer.numAnnotations(n);
  257. for (int i = 0; i < n; ++i)
  258. annotations[i].write(writer);
  259. writer.close();
  260. }
  261. catch (IOException e) {
  262. throw new RuntimeException(e); // should never reach here.
  263. }
  264. set(output.toByteArray());
  265. }
  266. /**
  267. * Changes the annotations. A call to this method is equivalent to:
  268. * <pre>setAnnotations(new Annotation[] { annotation })</pre>
  269. *
  270. * @param annotation the data structure representing
  271. * the new annotation.
  272. */
  273. public void setAnnotation(Annotation annotation) {
  274. setAnnotations(new Annotation[] { annotation });
  275. }
  276. /**
  277. * @param oldname a JVM class name.
  278. * @param newname a JVM class name.
  279. */
  280. void renameClass(String oldname, String newname) {
  281. HashMap map = new HashMap();
  282. map.put(oldname, newname);
  283. renameClass(map);
  284. }
  285. void renameClass(Map classnames) {
  286. Renamer renamer = new Renamer(info, getConstPool(), classnames);
  287. try {
  288. renamer.annotationArray();
  289. } catch (Exception e) {
  290. throw new RuntimeException(e);
  291. }
  292. }
  293. void getRefClasses(Map classnames) { renameClass(classnames); }
  294. /**
  295. * Returns a string representation of this object.
  296. */
  297. public String toString() {
  298. Annotation[] a = getAnnotations();
  299. StringBuilder sbuf = new StringBuilder();
  300. int i = 0;
  301. while (i < a.length) {
  302. sbuf.append(a[i++].toString());
  303. if (i != a.length)
  304. sbuf.append(", ");
  305. }
  306. return sbuf.toString();
  307. }
  308. static class Walker {
  309. byte[] info;
  310. Walker(byte[] attrInfo) {
  311. info = attrInfo;
  312. }
  313. final void parameters() throws Exception {
  314. int numParam = info[0] & 0xff;
  315. parameters(numParam, 1);
  316. }
  317. void parameters(int numParam, int pos) throws Exception {
  318. for (int i = 0; i < numParam; ++i)
  319. pos = annotationArray(pos);
  320. }
  321. final void annotationArray() throws Exception {
  322. annotationArray(0);
  323. }
  324. final int annotationArray(int pos) throws Exception {
  325. int num = ByteArray.readU16bit(info, pos);
  326. return annotationArray(pos + 2, num);
  327. }
  328. int annotationArray(int pos, int num) throws Exception {
  329. for (int i = 0; i < num; ++i)
  330. pos = annotation(pos);
  331. return pos;
  332. }
  333. final int annotation(int pos) throws Exception {
  334. int type = ByteArray.readU16bit(info, pos);
  335. int numPairs = ByteArray.readU16bit(info, pos + 2);
  336. return annotation(pos + 4, type, numPairs);
  337. }
  338. int annotation(int pos, int type, int numPairs) throws Exception {
  339. for (int j = 0; j < numPairs; ++j)
  340. pos = memberValuePair(pos);
  341. return pos;
  342. }
  343. /**
  344. * {@code element_value_paris}
  345. */
  346. final int memberValuePair(int pos) throws Exception {
  347. int nameIndex = ByteArray.readU16bit(info, pos);
  348. return memberValuePair(pos + 2, nameIndex);
  349. }
  350. /**
  351. * {@code element_value_paris[]}
  352. */
  353. int memberValuePair(int pos, int nameIndex) throws Exception {
  354. return memberValue(pos);
  355. }
  356. /**
  357. * {@code element_value}
  358. */
  359. final int memberValue(int pos) throws Exception {
  360. int tag = info[pos] & 0xff;
  361. if (tag == 'e') {
  362. int typeNameIndex = ByteArray.readU16bit(info, pos + 1);
  363. int constNameIndex = ByteArray.readU16bit(info, pos + 3);
  364. enumMemberValue(pos, typeNameIndex, constNameIndex);
  365. return pos + 5;
  366. }
  367. else if (tag == 'c') {
  368. int index = ByteArray.readU16bit(info, pos + 1);
  369. classMemberValue(pos, index);
  370. return pos + 3;
  371. }
  372. else if (tag == '@')
  373. return annotationMemberValue(pos + 1);
  374. else if (tag == '[') {
  375. int num = ByteArray.readU16bit(info, pos + 1);
  376. return arrayMemberValue(pos + 3, num);
  377. }
  378. else { // primitive types or String.
  379. int index = ByteArray.readU16bit(info, pos + 1);
  380. constValueMember(tag, index);
  381. return pos + 3;
  382. }
  383. }
  384. /**
  385. * {@code const_value_index}
  386. */
  387. void constValueMember(int tag, int index) throws Exception {}
  388. /**
  389. * {@code enum_const_value}
  390. */
  391. void enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
  392. throws Exception {
  393. }
  394. /**
  395. * {@code class_info_index}
  396. */
  397. void classMemberValue(int pos, int index) throws Exception {}
  398. /**
  399. * {@code annotation_value}
  400. */
  401. int annotationMemberValue(int pos) throws Exception {
  402. return annotation(pos);
  403. }
  404. /**
  405. * {@code array_value}
  406. */
  407. int arrayMemberValue(int pos, int num) throws Exception {
  408. for (int i = 0; i < num; ++i) {
  409. pos = memberValue(pos);
  410. }
  411. return pos;
  412. }
  413. }
  414. static class Renamer extends Walker {
  415. ConstPool cpool;
  416. Map classnames;
  417. /**
  418. * Constructs a renamer. It renames some class names
  419. * into the new names specified by <code>map</code>.
  420. *
  421. * @param info the annotations attribute.
  422. * @param cp the constant pool.
  423. * @param map pairs of replaced and substituted class names.
  424. * It can be null.
  425. */
  426. Renamer(byte[] info, ConstPool cp, Map map) {
  427. super(info);
  428. cpool = cp;
  429. classnames = map;
  430. }
  431. int annotation(int pos, int type, int numPairs) throws Exception {
  432. renameType(pos - 4, type);
  433. return super.annotation(pos, type, numPairs);
  434. }
  435. void enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
  436. throws Exception
  437. {
  438. renameType(pos + 1, typeNameIndex);
  439. super.enumMemberValue(pos, typeNameIndex, constNameIndex);
  440. }
  441. void classMemberValue(int pos, int index) throws Exception {
  442. renameType(pos + 1, index);
  443. super.classMemberValue(pos, index);
  444. }
  445. private void renameType(int pos, int index) {
  446. String name = cpool.getUtf8Info(index);
  447. String newName = Descriptor.rename(name, classnames);
  448. if (!name.equals(newName)) {
  449. int index2 = cpool.addUtf8Info(newName);
  450. ByteArray.write16bit(index2, info, pos);
  451. }
  452. }
  453. }
  454. static class Copier extends Walker {
  455. ByteArrayOutputStream output;
  456. AnnotationsWriter writer;
  457. ConstPool srcPool, destPool;
  458. Map classnames;
  459. /**
  460. * Constructs a copier. This copier renames some class names
  461. * into the new names specified by <code>map</code> when it copies
  462. * an annotation attribute.
  463. *
  464. * @param info the source attribute.
  465. * @param src the constant pool of the source class.
  466. * @param dest the constant pool of the destination class.
  467. * @param map pairs of replaced and substituted class names.
  468. * It can be null.
  469. */
  470. Copier(byte[] info, ConstPool src, ConstPool dest, Map map) {
  471. this(info, src, dest, map, true);
  472. }
  473. Copier(byte[] info, ConstPool src, ConstPool dest, Map map, boolean makeWriter) {
  474. super(info);
  475. output = new ByteArrayOutputStream();
  476. if (makeWriter)
  477. writer = new AnnotationsWriter(output, dest);
  478. srcPool = src;
  479. destPool = dest;
  480. classnames = map;
  481. }
  482. byte[] close() throws IOException {
  483. writer.close();
  484. return output.toByteArray();
  485. }
  486. void parameters(int numParam, int pos) throws Exception {
  487. writer.numParameters(numParam);
  488. super.parameters(numParam, pos);
  489. }
  490. int annotationArray(int pos, int num) throws Exception {
  491. writer.numAnnotations(num);
  492. return super.annotationArray(pos, num);
  493. }
  494. int annotation(int pos, int type, int numPairs) throws Exception {
  495. writer.annotation(copyType(type), numPairs);
  496. return super.annotation(pos, type, numPairs);
  497. }
  498. int memberValuePair(int pos, int nameIndex) throws Exception {
  499. writer.memberValuePair(copy(nameIndex));
  500. return super.memberValuePair(pos, nameIndex);
  501. }
  502. void constValueMember(int tag, int index) throws Exception {
  503. writer.constValueIndex(tag, copy(index));
  504. super.constValueMember(tag, index);
  505. }
  506. void enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
  507. throws Exception
  508. {
  509. writer.enumConstValue(copyType(typeNameIndex), copy(constNameIndex));
  510. super.enumMemberValue(pos, typeNameIndex, constNameIndex);
  511. }
  512. void classMemberValue(int pos, int index) throws Exception {
  513. writer.classInfoIndex(copyType(index));
  514. super.classMemberValue(pos, index);
  515. }
  516. int annotationMemberValue(int pos) throws Exception {
  517. writer.annotationValue();
  518. return super.annotationMemberValue(pos);
  519. }
  520. int arrayMemberValue(int pos, int num) throws Exception {
  521. writer.arrayValue(num);
  522. return super.arrayMemberValue(pos, num);
  523. }
  524. /**
  525. * Copies a constant pool entry into the destination constant pool
  526. * and returns the index of the copied entry.
  527. *
  528. * @param srcIndex the index of the copied entry into the source
  529. * constant pool.
  530. * @return the index of the copied item into the destination
  531. * constant pool.
  532. */
  533. int copy(int srcIndex) {
  534. return srcPool.copy(srcIndex, destPool, classnames);
  535. }
  536. /**
  537. * Copies a constant pool entry into the destination constant pool
  538. * and returns the index of the copied entry. That entry must be
  539. * a Utf8Info representing a class name in the L<class name>; form.
  540. *
  541. * @param srcIndex the index of the copied entry into the source
  542. * constant pool.
  543. * @return the index of the copied item into the destination
  544. * constant pool.
  545. */
  546. int copyType(int srcIndex) {
  547. String name = srcPool.getUtf8Info(srcIndex);
  548. String newName = Descriptor.rename(name, classnames);
  549. return destPool.addUtf8Info(newName);
  550. }
  551. }
  552. static class Parser extends Walker {
  553. ConstPool pool;
  554. Annotation[][] allParams; // all parameters
  555. Annotation[] allAnno; // all annotations
  556. Annotation currentAnno; // current annotation
  557. MemberValue currentMember; // current member
  558. /**
  559. * Constructs a parser. This parser constructs a parse tree of
  560. * the annotations.
  561. *
  562. * @param info the attribute.
  563. * @param src the constant pool.
  564. */
  565. Parser(byte[] info, ConstPool cp) {
  566. super(info);
  567. pool = cp;
  568. }
  569. Annotation[][] parseParameters() throws Exception {
  570. parameters();
  571. return allParams;
  572. }
  573. Annotation[] parseAnnotations() throws Exception {
  574. annotationArray();
  575. return allAnno;
  576. }
  577. MemberValue parseMemberValue() throws Exception {
  578. memberValue(0);
  579. return currentMember;
  580. }
  581. void parameters(int numParam, int pos) throws Exception {
  582. Annotation[][] params = new Annotation[numParam][];
  583. for (int i = 0; i < numParam; ++i) {
  584. pos = annotationArray(pos);
  585. params[i] = allAnno;
  586. }
  587. allParams = params;
  588. }
  589. int annotationArray(int pos, int num) throws Exception {
  590. Annotation[] array = new Annotation[num];
  591. for (int i = 0; i < num; ++i) {
  592. pos = annotation(pos);
  593. array[i] = currentAnno;
  594. }
  595. allAnno = array;
  596. return pos;
  597. }
  598. int annotation(int pos, int type, int numPairs) throws Exception {
  599. currentAnno = new Annotation(type, pool);
  600. return super.annotation(pos, type, numPairs);
  601. }
  602. int memberValuePair(int pos, int nameIndex) throws Exception {
  603. pos = super.memberValuePair(pos, nameIndex);
  604. currentAnno.addMemberValue(nameIndex, currentMember);
  605. return pos;
  606. }
  607. void constValueMember(int tag, int index) throws Exception {
  608. MemberValue m;
  609. ConstPool cp = pool;
  610. switch (tag) {
  611. case 'B' :
  612. m = new ByteMemberValue(index, cp);
  613. break;
  614. case 'C' :
  615. m = new CharMemberValue(index, cp);
  616. break;
  617. case 'D' :
  618. m = new DoubleMemberValue(index, cp);
  619. break;
  620. case 'F' :
  621. m = new FloatMemberValue(index, cp);
  622. break;
  623. case 'I' :
  624. m = new IntegerMemberValue(index, cp);
  625. break;
  626. case 'J' :
  627. m = new LongMemberValue(index, cp);
  628. break;
  629. case 'S' :
  630. m = new ShortMemberValue(index, cp);
  631. break;
  632. case 'Z' :
  633. m = new BooleanMemberValue(index, cp);
  634. break;
  635. case 's' :
  636. m = new StringMemberValue(index, cp);
  637. break;
  638. default :
  639. throw new RuntimeException("unknown tag:" + tag);
  640. }
  641. currentMember = m;
  642. super.constValueMember(tag, index);
  643. }
  644. void enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
  645. throws Exception
  646. {
  647. currentMember = new EnumMemberValue(typeNameIndex,
  648. constNameIndex, pool);
  649. super.enumMemberValue(pos, typeNameIndex, constNameIndex);
  650. }
  651. void classMemberValue(int pos, int index) throws Exception {
  652. currentMember = new ClassMemberValue(index, pool);
  653. super.classMemberValue(pos, index);
  654. }
  655. int annotationMemberValue(int pos) throws Exception {
  656. Annotation anno = currentAnno;
  657. pos = super.annotationMemberValue(pos);
  658. currentMember = new AnnotationMemberValue(currentAnno, pool);
  659. currentAnno = anno;
  660. return pos;
  661. }
  662. int arrayMemberValue(int pos, int num) throws Exception {
  663. ArrayMemberValue amv = new ArrayMemberValue(pool);
  664. MemberValue[] elements = new MemberValue[num];
  665. for (int i = 0; i < num; ++i) {
  666. pos = memberValue(pos);
  667. elements[i] = currentMember;
  668. }
  669. amv.setValue(elements);
  670. currentMember = amv;
  671. return pos;
  672. }
  673. }
  674. }