01: /*
02: * Copyright 2005 Brian S O'Neill
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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: /**
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 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,
43: DataInput din) throws IOException {
44: super (cp, name);
45:
46: int size = din.readUnsignedShort();
47: mAnnotations = new ArrayList(size);
48:
49: for (int i = 0; i < size; i++) {
50: addAnnotation(new Annotation(cp, din));
51: }
52: }
53:
54: public Annotation[] getAnnotations() {
55: Annotation[] copy = new Annotation[mAnnotations.size()];
56: return (Annotation[]) mAnnotations.toArray(copy);
57: }
58:
59: public void addAnnotation(Annotation annotation) {
60: mAnnotations.add(annotation);
61: }
62:
63: public int getLength() {
64: int length = 2;
65: for (int i = mAnnotations.size(); --i >= 0;) {
66: length += ((Annotation) mAnnotations.get(i)).getLength();
67: }
68: return length;
69: }
70:
71: public void writeDataTo(DataOutput dout) throws IOException {
72: int size = mAnnotations.size();
73: dout.writeShort(size);
74: for (int i = 0; i < size; i++) {
75: ((Annotation) mAnnotations.get(i)).writeTo(dout);
76: }
77: }
78: }
|