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.

RuntimeParamAnnos.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM
  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.List;
  19. import org.aspectj.apache.bcel.classfile.Attribute;
  20. import org.aspectj.apache.bcel.classfile.ConstantPool;
  21. public abstract class RuntimeParamAnnos extends Attribute {
  22. private List<AnnotationGen[]> parameterAnnotations;
  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 RuntimeParamAnnos(byte attrid, boolean visible,
  28. int nameIdx, int len, ConstantPool cpool) {
  29. super(attrid,nameIdx,len,cpool);
  30. this.visible = visible;
  31. parameterAnnotations = new ArrayList<AnnotationGen[]>();
  32. }
  33. public RuntimeParamAnnos(byte attrid,boolean visible,int nameIdx,int len,byte[] data,ConstantPool cpool) {
  34. super(attrid,nameIdx,len,cpool);
  35. this.visible = visible;
  36. parameterAnnotations = new ArrayList<AnnotationGen[]>();
  37. annotation_data = data;
  38. }
  39. public final void dump(DataOutputStream dos) throws IOException {
  40. super.dump(dos);
  41. writeAnnotations(dos);
  42. }
  43. public Attribute copy(ConstantPool constant_pool) {
  44. throw new RuntimeException("Not implemented yet!");
  45. }
  46. /** Return a list of Annotation[] - each list entry contains the annotations for one parameter */
  47. public List /*Annotation[]*/<AnnotationGen[]> getParameterAnnotations() {
  48. if (!inflated) inflate();
  49. return parameterAnnotations;
  50. }
  51. public AnnotationGen[] getAnnotationsOnParameter(int parameterIndex) {
  52. if (!inflated) inflate();
  53. // This may happen. In a ctor for a non static inner type the compiler
  54. // may have added an extra parameter to the generated ctor (the parameter
  55. // contains the instance of the outer class) - in this case
  56. // it may appear that there are more parameters than there are entries
  57. // in the parameter annotations array
  58. if (parameterIndex>=parameterAnnotations.size()) {
  59. return AnnotationGen.NO_ANNOTATIONS;
  60. }
  61. return parameterAnnotations.get(parameterIndex);
  62. }
  63. public boolean areVisible() {
  64. return visible;
  65. }
  66. protected void readParameterAnnotations(DataInputStream dis,ConstantPool cpool) throws IOException {
  67. annotation_data = new byte[length];
  68. dis.readFully(annotation_data,0,length);
  69. }
  70. private void inflate() {
  71. try {
  72. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data));
  73. int numParameters = dis.readUnsignedByte();
  74. if (numParameters > 0) {
  75. List<AnnotationGen[]> inflatedParameterAnnotations = new ArrayList<AnnotationGen[]>();
  76. for (int i=0; i<numParameters; i++) {
  77. int numAnnotations = dis.readUnsignedShort();
  78. if (numAnnotations == 0 ) {
  79. inflatedParameterAnnotations.add(AnnotationGen.NO_ANNOTATIONS);
  80. } else {
  81. AnnotationGen[] annotations = new AnnotationGen[numAnnotations];
  82. for (int j=0; j<numAnnotations; j++) {
  83. annotations[j] = AnnotationGen.read(dis,getConstantPool(),visible);
  84. }
  85. inflatedParameterAnnotations.add(annotations);
  86. }
  87. }
  88. parameterAnnotations = inflatedParameterAnnotations;
  89. }
  90. inflated = true;
  91. } catch (IOException ioe) {
  92. throw new RuntimeException("Unabled to inflate annotation data, badly formed?");
  93. }
  94. }
  95. protected void writeAnnotations(DataOutputStream dos) throws IOException {
  96. if (!inflated) {
  97. dos.write(annotation_data,0,length);
  98. } else {
  99. dos.writeByte(parameterAnnotations.size());
  100. for (AnnotationGen[] annotations : parameterAnnotations) {
  101. dos.writeShort(annotations.length);
  102. for (AnnotationGen annotation : annotations) {
  103. annotation.dump(dos);
  104. }
  105. }
  106. }
  107. }
  108. /** FOR TESTING ONLY: Tells you if the annotations have been inflated to an object graph */
  109. public boolean isInflated() {
  110. return inflated;
  111. }
  112. public String toString() {
  113. return "Runtime"+(visible?"Visible":"Invisible")+"ParameterAnnotations ["+(inflated?"inflated":"not yet inflated")+"]";
  114. }
  115. }