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.

RuntimeTypeAnnos.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* *******************************************************************
  2. * Copyright (c) 2013 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 org.aspectj.apache.bcel.classfile.Attribute;
  18. import org.aspectj.apache.bcel.classfile.ConstantPool;
  19. public abstract class RuntimeTypeAnnos extends Attribute {
  20. private boolean visible;
  21. private TypeAnnotationGen[] typeAnnotations; // null until inflated
  22. // Keep just a byte stream of the data until someone actually asks for the information within
  23. private byte[] annotation_data;
  24. public RuntimeTypeAnnos(byte attrid, boolean visible, int nameIdx, int len, ConstantPool cpool) {
  25. super(attrid,nameIdx,len,cpool);
  26. this.visible = visible;
  27. }
  28. protected void readTypeAnnotations(DataInputStream dis,ConstantPool cpool) throws IOException {
  29. annotation_data = new byte[length];
  30. dis.readFully(annotation_data,0,length);
  31. }
  32. public final void dump(DataOutputStream dos) throws IOException {
  33. super.dump(dos);
  34. writeTypeAnnotations(dos);
  35. }
  36. protected void writeTypeAnnotations(DataOutputStream dos) throws IOException {
  37. if (typeAnnotations == null) {
  38. dos.write(annotation_data,0,length);
  39. } else {
  40. dos.writeShort(typeAnnotations.length);
  41. for (TypeAnnotationGen typeAnnotation : typeAnnotations) {
  42. typeAnnotation.dump(dos);
  43. }
  44. }
  45. }
  46. // public RuntimeTypeAnnos(byte attrid,boolean visible,int nameIdx,int len,byte[] data,ConstantPool cpool) {
  47. // super(attrid,nameIdx,len,cpool);
  48. // this.visible = visible;
  49. // parameterAnnotations = new ArrayList<AnnotationGen[]>();
  50. // annotation_data = data;
  51. // }
  52. public Attribute copy(ConstantPool constant_pool) {
  53. throw new RuntimeException("Not implemented yet!");
  54. }
  55. public TypeAnnotationGen[] getTypeAnnotations() {
  56. ensureInflated();
  57. return typeAnnotations;
  58. }
  59. public boolean areVisible() {
  60. return visible;
  61. }
  62. private void ensureInflated() {
  63. if (typeAnnotations !=null) {
  64. return;
  65. }
  66. try {
  67. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data));
  68. int numTypeAnnotations = dis.readUnsignedShort();
  69. if (numTypeAnnotations == 0) {
  70. typeAnnotations = TypeAnnotationGen.NO_TYPE_ANNOTATIONS;
  71. } else {
  72. typeAnnotations = new TypeAnnotationGen[numTypeAnnotations];
  73. for (int i=0; i<numTypeAnnotations; i++) {
  74. typeAnnotations[i] = TypeAnnotationGen.read(dis,getConstantPool(),visible);
  75. }
  76. }
  77. } catch (IOException ioe) {
  78. throw new RuntimeException("Unabled to inflate type annotation data, badly formed?");
  79. }
  80. }
  81. public String toString() {
  82. return "Runtime"+(visible?"Visible":"Invisible")+"TypeAnnotations ["+(isInflated()?"inflated":"not yet inflated")+"]";
  83. }
  84. public boolean isInflated() {
  85. return typeAnnotations != null;
  86. }
  87. }