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.

AnnotationGroup.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 2004 Bill Burke. 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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode.annotation;
  16. import javassist.bytecode.AttributeInfo;
  17. import javassist.bytecode.ConstPool;
  18. import java.io.DataInputStream;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.DataOutputStream;
  22. import java.io.IOException;
  23. import java.util.LinkedHashMap;
  24. import java.util.Collection;
  25. import java.util.Iterator;
  26. /**
  27. * Comment
  28. *
  29. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  30. * @version $Revision: 1.2 $
  31. *
  32. **/
  33. public class AnnotationGroup
  34. {
  35. AttributeInfo parent;
  36. LinkedHashMap annotations = new LinkedHashMap();
  37. public AnnotationGroup(AttributeInfo parent)
  38. {
  39. this.parent = parent;
  40. if (!parent.getName().equals("RuntimeInvisibleAnnotations") &&
  41. !parent.getName().equals("RuntimeVisibleAnnotations"))
  42. throw new RuntimeException("Annotation group must be RuntimeInvisibleAnnotations or RuntimeVisibleAnnotations, it was: " + parent.getName());
  43. try
  44. {
  45. initialize();
  46. }
  47. catch (java.io.IOException e)
  48. {
  49. throw new RuntimeException(e);
  50. }
  51. }
  52. public String getName()
  53. {
  54. return parent.getName();
  55. }
  56. public Collection getAnnotations()
  57. {
  58. if (annotations == null) return null;
  59. return annotations.values();
  60. }
  61. public AnnotationInfo getAnnotation(String type)
  62. {
  63. if (annotations == null) return null;
  64. return (AnnotationInfo)annotations.get(type);
  65. }
  66. private void initialize() throws java.io.IOException
  67. {
  68. ConstPool cp = parent.getConstPool();
  69. byte[] bytes = parent.get();
  70. if (bytes == null) return;
  71. ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  72. DataInputStream di = new DataInputStream(bais);
  73. short num_annotations = di.readShort();
  74. for (int i = 0; i < num_annotations; i++)
  75. {
  76. AnnotationInfo info = AnnotationInfo.readAnnotationInfo(cp, di);
  77. annotations.put(info.getAnnotationType(), info);
  78. }
  79. }
  80. public void addAnnotation(AnnotationInfo info)
  81. {
  82. annotations.put(info.getAnnotationType(), info);
  83. try
  84. {
  85. parent.set(convertToBytes());
  86. }
  87. catch (IOException e)
  88. {
  89. throw new RuntimeException(e);
  90. }
  91. }
  92. public void removeAnnotation(String name)
  93. {
  94. annotations.remove(name);
  95. try
  96. {
  97. parent.set(convertToBytes());
  98. }
  99. catch (IOException e)
  100. {
  101. throw new RuntimeException(e);
  102. }
  103. }
  104. private byte[] convertToBytes() throws IOException
  105. {
  106. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  107. DataOutputStream dos = new DataOutputStream(baos);
  108. short num_annotations = (short)annotations.size();
  109. dos.writeShort(num_annotations);
  110. Iterator it = annotations.values().iterator();
  111. while (it.hasNext())
  112. {
  113. AnnotationInfo info = (AnnotationInfo)it.next();
  114. info.write(dos);
  115. }
  116. dos.flush();
  117. return baos.toByteArray();
  118. }
  119. }