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.

RuntimeAnnos.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package org.aspectj.apache.bcel.classfile.annotation;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import org.aspectj.apache.bcel.classfile.Attribute;
  10. import org.aspectj.apache.bcel.classfile.ConstantPool;
  11. public abstract class RuntimeAnnos extends Attribute {
  12. private List<AnnotationGen> annotations;
  13. private boolean visible;
  14. // Keep just a byte stream of the data until someone actually asks for it
  15. private boolean inflated = false;
  16. private byte[] annotation_data;
  17. public RuntimeAnnos(byte attrid, boolean visible, int nameIdx, int len, ConstantPool cpool) {
  18. super(attrid, nameIdx, len, cpool);
  19. this.visible = visible;
  20. annotations = new ArrayList<AnnotationGen>();
  21. }
  22. public RuntimeAnnos(byte attrid, boolean visible, int nameIdx, int len, byte[] data, ConstantPool cpool) {
  23. super(attrid, nameIdx, len, cpool);
  24. this.visible = visible;
  25. annotations = new ArrayList<AnnotationGen>();
  26. annotation_data = data;
  27. }
  28. public List<AnnotationGen> getAnnotations() {
  29. if (!inflated)
  30. inflate();
  31. return annotations;
  32. }
  33. public boolean areVisible() {
  34. return visible;
  35. }
  36. protected void readAnnotations(DataInputStream dis, ConstantPool cpool) throws IOException {
  37. annotation_data = new byte[length];
  38. dis.read(annotation_data, 0, length);
  39. }
  40. protected void writeAnnotations(DataOutputStream dos) throws IOException {
  41. if (!inflated) {
  42. dos.write(annotation_data, 0, length);
  43. } else {
  44. dos.writeShort(annotations.size());
  45. for (Iterator<AnnotationGen> i = annotations.iterator(); i.hasNext();) {
  46. AnnotationGen ann = i.next();
  47. ann.dump(dos);
  48. }
  49. }
  50. }
  51. private void inflate() {
  52. try {
  53. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data));
  54. int numberOfAnnotations = dis.readUnsignedShort();
  55. for (int i = 0; i < numberOfAnnotations; i++) {
  56. annotations.add(AnnotationGen.read(dis, getConstantPool(), visible));
  57. }
  58. dis.close();
  59. inflated = true;
  60. } catch (IOException ioe) {
  61. throw new RuntimeException("Unabled to inflate annotation data, badly formed? ");
  62. }
  63. }
  64. /** FOR TESTING ONLY: Tells you if the annotations have been inflated to an object graph */
  65. public boolean isInflated() {
  66. return inflated;
  67. }
  68. }