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 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 (Iterator<AnnotationGen> i = annotations.iterator(); i.hasNext();) {
  57. AnnotationGen ann = i.next();
  58. ann.dump(dos);
  59. }
  60. }
  61. }
  62. private void inflate() {
  63. try {
  64. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data));
  65. int numberOfAnnotations = dis.readUnsignedShort();
  66. if (numberOfAnnotations > 0) {
  67. List<AnnotationGen> inflatedAnnotations = new ArrayList<AnnotationGen>();
  68. for (int i = 0; i < numberOfAnnotations; i++) {
  69. inflatedAnnotations.add(AnnotationGen.read(dis, getConstantPool(), visible));
  70. }
  71. annotations = inflatedAnnotations;
  72. }
  73. dis.close();
  74. inflated = true;
  75. } catch (IOException ioe) {
  76. throw new RuntimeException("Unabled to inflate annotation data, badly formed? ");
  77. }
  78. }
  79. /** FOR TESTING ONLY: Tells you if the annotations have been inflated to an object graph */
  80. public boolean isInflated() {
  81. return inflated;
  82. }
  83. }