Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Annotation.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. *
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Andy Clement initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.apache.bcel.classfile.annotation;
  14. import java.io.DataInputStream;
  15. import java.io.DataOutputStream;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import org.aspectj.apache.bcel.Constants;
  21. import org.aspectj.apache.bcel.classfile.ConstantPool;
  22. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  23. import org.aspectj.apache.bcel.classfile.Utility;
  24. /**
  25. * An annotation is an immutable object (AnnotationGen is the mutable variant) - it basically contains a list
  26. * of name-value pairs.
  27. */
  28. public class Annotation {
  29. private int typeIndex;
  30. private List /* ElementNameValuePair */ evs;
  31. private ConstantPool cpool;
  32. private boolean isRuntimeVisible;
  33. public String toString() {
  34. StringBuffer sb = new StringBuffer();
  35. sb.append("ANNOTATION ["+getTypeSignature()+"] ["+
  36. (isRuntimeVisible?"runtimeVisible":"runtimeInvisible")+"] [");
  37. for (Iterator iter = evs.iterator(); iter.hasNext();) {
  38. ElementNameValuePair element = (ElementNameValuePair) iter.next();
  39. sb.append(element.toString());
  40. if (iter.hasNext()) sb.append(",");
  41. }
  42. sb.append("]");
  43. return sb.toString();
  44. }
  45. private Annotation(ConstantPool cpool) {
  46. this.cpool = cpool;
  47. }
  48. protected static Annotation read(DataInputStream dis,ConstantPool cpool,boolean isRuntimeVisible) throws IOException {
  49. Annotation a = new Annotation(cpool);
  50. a.typeIndex = dis.readUnsignedShort();
  51. int elemValuePairCount = dis.readUnsignedShort();
  52. for (int i=0;i<elemValuePairCount;i++) {
  53. int nidx = dis.readUnsignedShort();
  54. a.addElementNameValuePair(
  55. new ElementNameValuePair(nidx,ElementValue.readElementValue(dis,cpool),cpool));
  56. }
  57. a.isRuntimeVisible(isRuntimeVisible);
  58. return a;
  59. }
  60. protected void dump(DataOutputStream dos) throws IOException {
  61. dos.writeShort(typeIndex); // u2 index of type name in cpool
  62. dos.writeShort(evs.size()); // u2 element_value pair count
  63. for (int i = 0 ; i<evs.size();i++) {
  64. ElementNameValuePair envp = (ElementNameValuePair) evs.get(i);
  65. envp.dump(dos);
  66. }
  67. }
  68. public void addElementNameValuePair(ElementNameValuePair evp) {
  69. if (evs == null) evs = new ArrayList();
  70. evs.add(evp);
  71. }
  72. public int getTypeIndex() {
  73. return typeIndex;
  74. }
  75. public final String getTypeSignature() {
  76. ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(typeIndex,Constants.CONSTANT_Utf8);
  77. return c.getBytes();
  78. }
  79. public final String getTypeName() {
  80. ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(typeIndex,Constants.CONSTANT_Utf8);
  81. return Utility.signatureToString(c.getBytes());
  82. }
  83. /**
  84. * Returns list of ElementNameValuePair objects
  85. */
  86. public List getValues() {
  87. return evs;
  88. }
  89. protected void isRuntimeVisible(boolean b) {
  90. isRuntimeVisible = b;
  91. }
  92. public boolean isRuntimeVisible() {
  93. return isRuntimeVisible;
  94. }
  95. public String toShortString() {
  96. StringBuffer result = new StringBuffer();
  97. result.append("@");
  98. result.append(getTypeName());
  99. if (getValues().size()>0) {
  100. result.append("(");
  101. for (Iterator iter = getValues().iterator(); iter.hasNext();) {
  102. ElementNameValuePair element = (ElementNameValuePair) iter.next();
  103. result.append(element.toShortString());
  104. }
  105. result.append(")");
  106. }
  107. return result.toString();
  108. }
  109. }