KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > attribute > AnnotationsAttr


1 /*
2  * Copyright 2005 Brian S O'Neill
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.cojen.classfile.attribute;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.io.DataInput JavaDoc;
22 import java.io.DataOutput JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.cojen.classfile.Attribute;
25 import org.cojen.classfile.ConstantPool;
26
27 /**
28  * Base class for annotations attributes defined for Java 5.
29  *
30  * @author Brian S O'Neill
31  * @see ParameterAnnotationsAttr
32  */

33 public abstract class AnnotationsAttr extends Attribute {
34
35     private List JavaDoc mAnnotations;
36     
37     public AnnotationsAttr(ConstantPool cp, String JavaDoc name) {
38         super(cp, name);
39         mAnnotations = new ArrayList JavaDoc(2);
40     }
41     
42     public AnnotationsAttr(ConstantPool cp, String JavaDoc name, int length, DataInput JavaDoc din)
43         throws IOException JavaDoc
44     {
45         super(cp, name);
46         
47         int size = din.readUnsignedShort();
48         mAnnotations = new ArrayList JavaDoc(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 JavaDoc dout) throws IOException JavaDoc {
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