KickJava   Java API By Example, From Geeks To Geeks.

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


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.Vector JavaDoc;
20 import java.io.DataInput JavaDoc;
21 import java.io.DataOutput JavaDoc;
22 import java.io.IOException JavaDoc;
23 import org.cojen.classfile.Attribute;
24 import org.cojen.classfile.ConstantPool;
25
26 /**
27  * Base class for parameter annotations attributes defined for Java 5.
28  *
29  * @author Brian S O'Neill
30  * @see AnnotationsAttr
31  */

32 public abstract class ParameterAnnotationsAttr extends Attribute {
33
34     /** Contains Vectors of annotations */
35     private Vector JavaDoc mParameterAnnotations;
36     
37     public ParameterAnnotationsAttr(ConstantPool cp, String JavaDoc name) {
38         super(cp, name);
39         mParameterAnnotations = new Vector JavaDoc(2);
40     }
41     
42     public ParameterAnnotationsAttr(ConstantPool cp, String JavaDoc name, int length, DataInput JavaDoc din)
43         throws IOException JavaDoc
44     {
45         super(cp, name);
46
47         int size = din.readUnsignedByte();
48         mParameterAnnotations = new Vector JavaDoc(size);
49
50         for (int i=0; i<size; i++) {
51             int subSize = din.readUnsignedShort();
52             Vector JavaDoc annotations = new Vector JavaDoc(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     /**
63      * First array index is zero-based parameter number.
64      */

65     public Annotation[][] getAnnotations() {
66         Annotation[][] copy = new Annotation[mParameterAnnotations.size()][];
67         for (int i=copy.length; --i>=0; ) {
68             Vector JavaDoc annotations = (Vector JavaDoc)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     /**
83      * @param parameter zero-based parameter number
84      */

85     public Annotation[] getAnnotations(int parameter) {
86         Vector JavaDoc annotations = (Vector JavaDoc)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     /**
95      * @param parameter zero-based parameter number
96      */

97     public void addAnnotation(int parameter, Annotation annotation) {
98         if (parameter >= mParameterAnnotations.size()) {
99             mParameterAnnotations.setSize(parameter);
100         }
101         Vector JavaDoc annotations = (Vector JavaDoc)mParameterAnnotations.get(parameter);
102         if (annotations == null) {
103             annotations = new Vector JavaDoc(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 JavaDoc annotations = (Vector JavaDoc)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 JavaDoc dout) throws IOException JavaDoc {
121         int size = mParameterAnnotations.size();
122         dout.writeByte(size);
123         for (int i=0; i<size; i++) {
124             Vector JavaDoc annotations = (Vector JavaDoc)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