| 1 16 17 package org.cojen.classfile.attribute; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.io.DataInput ; 22 import java.io.DataOutput ; 23 import java.io.IOException ; 24 import org.cojen.classfile.Attribute; 25 import org.cojen.classfile.ConstantPool; 26 27 33 public abstract class AnnotationsAttr extends Attribute { 34 35 private List mAnnotations; 36 37 public AnnotationsAttr(ConstantPool cp, String name) { 38 super(cp, name); 39 mAnnotations = new ArrayList (2); 40 } 41 42 public AnnotationsAttr(ConstantPool cp, String name, int length, DataInput din) 43 throws IOException  44 { 45 super(cp, name); 46 47 int size = din.readUnsignedShort(); 48 mAnnotations = new ArrayList (size); 49 50 for (int i=0; i<size; i++) { 51 addAnnotation(new Annotation(cp, din)); 52 } 53 } 54 55 public Annotation[] getAnnotations() { 56 Annotation[] copy = new Annotation[mAnnotations.size()]; 57 return (Annotation[])mAnnotations.toArray(copy); 58 } 59 60 public void addAnnotation(Annotation annotation) { 61 mAnnotations.add(annotation); 62 } 63 64 public int getLength() { 65 int length = 2; 66 for (int i=mAnnotations.size(); --i>=0; ) { 67 length += ((Annotation)mAnnotations.get(i)).getLength(); 68 } 69 return length; 70 } 71 72 public void writeDataTo(DataOutput dout) throws IOException { 73 int size = mAnnotations.size(); 74 dout.writeShort(size); 75 for (int i=0; i<size; i++) { 76 ((Annotation)mAnnotations.get(i)).writeTo(dout); 77 } 78 } 79 } 80 81 | Popular Tags |