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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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.List;
  19. import org.aspectj.apache.bcel.classfile.Attribute;
  20. import org.aspectj.apache.bcel.classfile.ConstantPool;
  21. public abstract class RuntimeAnnos extends Attribute {
  22. private List<AnnotationGen> annotations;
  23. private boolean visible;
  24. // Keep just a byte stream of the data until someone actually asks for it
  25. private boolean inflated = false;
  26. private byte[] annotation_data;
  27. public RuntimeAnnos(byte attrid, boolean visible, int nameIdx, int len, ConstantPool cpool) {
  28. super(attrid, nameIdx, len, cpool);
  29. this.visible = visible;
  30. annotations = new ArrayList<>();
  31. }
  32. public RuntimeAnnos(byte attrid, boolean visible, int nameIdx, int len, byte[] data, ConstantPool cpool) {
  33. super(attrid, nameIdx, len, cpool);
  34. this.visible = visible;
  35. annotations = new ArrayList<>();
  36. annotation_data = data;
  37. }
  38. public List<AnnotationGen> getAnnotations() {
  39. if (!inflated)
  40. inflate();
  41. return annotations;
  42. }
  43. public boolean areVisible() {
  44. return visible;
  45. }
  46. protected void readAnnotations(DataInputStream dis, ConstantPool cpool) throws IOException {
  47. annotation_data = new byte[length];
  48. dis.readFully(annotation_data, 0, length);
  49. }
  50. protected void writeAnnotations(DataOutputStream dos) throws IOException {
  51. if (!inflated) {
  52. dos.write(annotation_data, 0, length);
  53. } else {
  54. dos.writeShort(annotations.size());
  55. for (AnnotationGen ann : annotations) {
  56. ann.dump(dos);
  57. }
  58. }
  59. }
  60. private void inflate() {
  61. try {
  62. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data));
  63. int numberOfAnnotations = dis.readUnsignedShort();
  64. if (numberOfAnnotations > 0) {
  65. List<AnnotationGen> inflatedAnnotations = new ArrayList<>();
  66. for (int i = 0; i < numberOfAnnotations; i++) {
  67. inflatedAnnotations.add(AnnotationGen.read(dis, getConstantPool(), visible));
  68. }
  69. annotations = inflatedAnnotations;
  70. }
  71. dis.close();
  72. inflated = true;
  73. } catch (IOException ioe) {
  74. throw new RuntimeException("Unabled to inflate annotation data, badly formed? ");
  75. }
  76. }
  77. /** FOR TESTING ONLY: Tells you if the annotations have been inflated to an object graph */
  78. public boolean isInflated() {
  79. return inflated;
  80. }
  81. }