1 16 17 package org.cojen.classfile.attribute; 18 19 import java.util.Vector ; 20 import java.io.DataInput ; 21 import java.io.DataOutput ; 22 import java.io.IOException ; 23 import org.cojen.classfile.Attribute; 24 import org.cojen.classfile.ConstantPool; 25 26 32 public abstract class ParameterAnnotationsAttr extends Attribute { 33 34 35 private Vector mParameterAnnotations; 36 37 public ParameterAnnotationsAttr(ConstantPool cp, String name) { 38 super(cp, name); 39 mParameterAnnotations = new Vector (2); 40 } 41 42 public ParameterAnnotationsAttr(ConstantPool cp, String name, int length, DataInput din) 43 throws IOException 44 { 45 super(cp, name); 46 47 int size = din.readUnsignedByte(); 48 mParameterAnnotations = new Vector (size); 49 50 for (int i=0; i<size; i++) { 51 int subSize = din.readUnsignedShort(); 52 Vector annotations = new Vector (subSize); 53 54 for (int j=0; j<subSize; j++) { 55 annotations.add(new Annotation(cp, din)); 56 } 57 58 mParameterAnnotations.add(annotations); 59 } 60 } 61 62 65 public Annotation[][] getAnnotations() { 66 Annotation[][] copy = new Annotation[mParameterAnnotations.size()][]; 67 for (int i=copy.length; --i>=0; ) { 68 Vector annotations = (Vector )mParameterAnnotations.get(i); 69 if (annotations == null) { 70 copy[i] = new Annotation[0]; 71 } else { 72 copy[i] = (Annotation[])annotations.toArray(new Annotation[annotations.size()]); 73 } 74 } 75 return copy; 76 } 77 78 public int getParameterCount() { 79 return mParameterAnnotations.size(); 80 } 81 82 85 public Annotation[] getAnnotations(int parameter) { 86 Vector annotations = (Vector )mParameterAnnotations.get(parameter); 87 if (annotations == null) { 88 return new Annotation[0]; 89 } else { 90 return (Annotation[])annotations.toArray(new Annotation[annotations.size()]); 91 } 92 } 93 94 97 public void addAnnotation(int parameter, Annotation annotation) { 98 if (parameter >= mParameterAnnotations.size()) { 99 mParameterAnnotations.setSize(parameter); 100 } 101 Vector annotations = (Vector )mParameterAnnotations.get(parameter); 102 if (annotations == null) { 103 annotations = new Vector (2); 104 mParameterAnnotations.set(parameter, annotations); 105 } 106 annotations.add(annotation); 107 } 108 109 public int getLength() { 110 int length = 1; 111 for (int i=mParameterAnnotations.size(); --i>=0; ) { 112 Vector annotations = (Vector )mParameterAnnotations.get(i); 113 for (int j=annotations.size(); --j>=0; ) { 114 length += 2 + ((Annotation)annotations.get(j)).getLength(); 115 } 116 } 117 return length; 118 } 119 120 public void writeDataTo(DataOutput dout) throws IOException { 121 int size = mParameterAnnotations.size(); 122 dout.writeByte(size); 123 for (int i=0; i<size; i++) { 124 Vector annotations = (Vector )mParameterAnnotations.get(i); 125 int subSize = annotations.size(); 126 dout.writeShort(subSize); 127 for (int j=0; j<subSize; j++) { 128 ((Annotation)annotations.get(j)).writeTo(dout); 129 } 130 } 131 } 132 } 133 134 | Popular Tags |