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.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* *******************************************************************
  2. * Copyright (c) 2004, 2013 IBM, VMware
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement - initial implementation {date}
  11. * ******************************************************************/
  12. package org.aspectj.apache.bcel.classfile.annotation;
  13. import java.io.ByteArrayInputStream;
  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.classfile.Attribute;
  21. import org.aspectj.apache.bcel.classfile.ConstantPool;
  22. public abstract class RuntimeAnnos extends Attribute {
  23. private List<AnnotationGen> annotations;
  24. private boolean visible;
  25. // Keep just a byte stream of the data until someone actually asks for it
  26. private boolean inflated = false;
  27. private byte[] annotation_data;
  28. public RuntimeAnnos(byte attrid, boolean visible, int nameIdx, int len, ConstantPool cpool) {
  29. super(attrid, nameIdx, len, cpool);
  30. this.visible = visible;
  31. annotations = new ArrayList<AnnotationGen>();
  32. }
  33. public RuntimeAnnos(byte attrid, boolean visible, int nameIdx, int len, byte[] data, ConstantPool cpool) {
  34. super(attrid, nameIdx, len, cpool);
  35. this.visible = visible;
  36. annotations = new ArrayList<AnnotationGen>();
  37. annotation_data = data;
  38. }
  39. public List<AnnotationGen> getAnnotations() {
  40. if (!inflated)
  41. inflate();
  42. return annotations;
  43. }
  44. public boolean areVisible() {
  45. return visible;
  46. }
  47. protected void readAnnotations(DataInputStream dis, ConstantPool cpool) throws IOException {
  48. annotation_data = new byte[length];
  49. dis.readFully(annotation_data, 0, length);
  50. }
  51. protected void writeAnnotations(DataOutputStream dos) throws IOException {
  52. if (!inflated) {
  53. dos.write(annotation_data, 0, length);
  54. } else {
  55. dos.writeShort(annotations.size());
  56. for (AnnotationGen ann : annotations) {
  57. ann.dump(dos);
  58. }
  59. }
  60. }
  61. private void inflate() {
  62. try {
  63. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data));
  64. int numberOfAnnotations = dis.readUnsignedShort();
  65. if (numberOfAnnotations > 0) {
  66. List<AnnotationGen> inflatedAnnotations = new ArrayList<AnnotationGen>();
  67. for (int i = 0; i < numberOfAnnotations; i++) {
  68. inflatedAnnotations.add(AnnotationGen.read(dis, getConstantPool(), visible));
  69. }
  70. annotations = inflatedAnnotations;
  71. }
  72. dis.close();
  73. inflated = true;
  74. } catch (IOException ioe) {
  75. throw new RuntimeException("Unabled to inflate annotation data, badly formed? ");
  76. }
  77. }
  78. /** FOR TESTING ONLY: Tells you if the annotations have been inflated to an object graph */
  79. public boolean isInflated() {
  80. return inflated;
  81. }
  82. }