001: /*
002: * Copyright 2005 Brian S O'Neill
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.cojen.classfile.attribute;
018:
019: import java.util.Vector;
020: import java.io.DataInput;
021: import java.io.DataOutput;
022: import java.io.IOException;
023: import org.cojen.classfile.Attribute;
024: import org.cojen.classfile.ConstantPool;
025:
026: /**
027: * Base class for parameter annotations attributes defined for Java 5.
028: *
029: * @author Brian S O'Neill
030: * @see AnnotationsAttr
031: */
032: public abstract class ParameterAnnotationsAttr extends Attribute {
033:
034: /** Contains Vectors of annotations */
035: private Vector mParameterAnnotations;
036:
037: public ParameterAnnotationsAttr(ConstantPool cp, String name) {
038: super (cp, name);
039: mParameterAnnotations = new Vector(2);
040: }
041:
042: public ParameterAnnotationsAttr(ConstantPool cp, String name,
043: int length, DataInput din) throws IOException {
044: super (cp, name);
045:
046: int size = din.readUnsignedByte();
047: mParameterAnnotations = new Vector(size);
048:
049: for (int i = 0; i < size; i++) {
050: int subSize = din.readUnsignedShort();
051: Vector annotations = new Vector(subSize);
052:
053: for (int j = 0; j < subSize; j++) {
054: annotations.add(new Annotation(cp, din));
055: }
056:
057: mParameterAnnotations.add(annotations);
058: }
059: }
060:
061: /**
062: * First array index is zero-based parameter number.
063: */
064: public Annotation[][] getAnnotations() {
065: Annotation[][] copy = new Annotation[mParameterAnnotations
066: .size()][];
067: for (int i = copy.length; --i >= 0;) {
068: Vector annotations = (Vector) mParameterAnnotations.get(i);
069: if (annotations == null) {
070: copy[i] = new Annotation[0];
071: } else {
072: copy[i] = (Annotation[]) annotations
073: .toArray(new Annotation[annotations.size()]);
074: }
075: }
076: return copy;
077: }
078:
079: public int getParameterCount() {
080: return mParameterAnnotations.size();
081: }
082:
083: /**
084: * @param parameter zero-based parameter number
085: */
086: public Annotation[] getAnnotations(int parameter) {
087: Vector annotations = (Vector) mParameterAnnotations
088: .get(parameter);
089: if (annotations == null) {
090: return new Annotation[0];
091: } else {
092: return (Annotation[]) annotations
093: .toArray(new Annotation[annotations.size()]);
094: }
095: }
096:
097: /**
098: * @param parameter zero-based parameter number
099: */
100: public void addAnnotation(int parameter, Annotation annotation) {
101: if (parameter >= mParameterAnnotations.size()) {
102: mParameterAnnotations.setSize(parameter);
103: }
104: Vector annotations = (Vector) mParameterAnnotations
105: .get(parameter);
106: if (annotations == null) {
107: annotations = new Vector(2);
108: mParameterAnnotations.set(parameter, annotations);
109: }
110: annotations.add(annotation);
111: }
112:
113: public int getLength() {
114: int length = 1;
115: for (int i = mParameterAnnotations.size(); --i >= 0;) {
116: Vector annotations = (Vector) mParameterAnnotations.get(i);
117: for (int j = annotations.size(); --j >= 0;) {
118: length += 2 + ((Annotation) annotations.get(j))
119: .getLength();
120: }
121: }
122: return length;
123: }
124:
125: public void writeDataTo(DataOutput dout) throws IOException {
126: int size = mParameterAnnotations.size();
127: dout.writeByte(size);
128: for (int i = 0; i < size; i++) {
129: Vector annotations = (Vector) mParameterAnnotations.get(i);
130: int subSize = annotations.size();
131: dout.writeShort(subSize);
132: for (int j = 0; j < subSize; j++) {
133: ((Annotation) annotations.get(j)).writeTo(dout);
134: }
135: }
136: }
137: }
|