ParameterAnnotationsAttr.java :  » Byte-Code » Cojen » org » cojen » classfile » attribute » Java Open Source

Java Open Source » Byte Code » Cojen 
Cojen » org » cojen » classfile » attribute » ParameterAnnotationsAttr.java
/*
 *  Copyright 2005 Brian S O'Neill
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.cojen.classfile.attribute;

import java.util.Vector;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.cojen.classfile.Attribute;
import org.cojen.classfile.ConstantPool;

/**
 * Base class for parameter annotations attributes defined for Java 5.
 *
 * @author Brian S O'Neill
 * @see AnnotationsAttr
 */
public abstract class ParameterAnnotationsAttr extends Attribute {

    /** Contains Vectors of annotations */
    private Vector mParameterAnnotations;
    
    public ParameterAnnotationsAttr(ConstantPool cp, String name) {
        super(cp, name);
        mParameterAnnotations = new Vector(2);
    }
    
    public ParameterAnnotationsAttr(ConstantPool cp, String name, int length, DataInput din)
        throws IOException
    {
        super(cp, name);

        int size = din.readUnsignedByte();
        mParameterAnnotations = new Vector(size);

        for (int i=0; i<size; i++) {
            int subSize = din.readUnsignedShort();
            Vector annotations = new Vector(subSize);

            for (int j=0; j<subSize; j++) {
                annotations.add(new Annotation(cp, din));
            }

            mParameterAnnotations.add(annotations);
        }
    }

    /**
     * First array index is zero-based parameter number.
     */
    public Annotation[][] getAnnotations() {
        Annotation[][] copy = new Annotation[mParameterAnnotations.size()][];
        for (int i=copy.length; --i>=0; ) {
            Vector annotations = (Vector)mParameterAnnotations.get(i);
            if (annotations == null) {
                copy[i] = new Annotation[0];
            } else {
                copy[i] = (Annotation[])annotations.toArray(new Annotation[annotations.size()]);
            }
        }
        return copy;
    }

    public int getParameterCount() {
        return mParameterAnnotations.size();
    }

    /**
     * @param parameter zero-based parameter number
     */
    public Annotation[] getAnnotations(int parameter) {
        Vector annotations = (Vector)mParameterAnnotations.get(parameter);
        if (annotations == null) {
            return new Annotation[0];
        } else {
            return (Annotation[])annotations.toArray(new Annotation[annotations.size()]);
        }
    }

    /**
     * @param parameter zero-based parameter number
     */
    public void addAnnotation(int parameter, Annotation annotation) {
        if (parameter >= mParameterAnnotations.size()) {
            mParameterAnnotations.setSize(parameter);
        }
        Vector annotations = (Vector)mParameterAnnotations.get(parameter);
        if (annotations == null) {
            annotations = new Vector(2);
            mParameterAnnotations.set(parameter, annotations);
        }
        annotations.add(annotation);
    }
    
    public int getLength() {
        int length = 1;
        for (int i=mParameterAnnotations.size(); --i>=0; ) {
            Vector annotations = (Vector)mParameterAnnotations.get(i);
            for (int j=annotations.size(); --j>=0; ) {
                length += 2 + ((Annotation)annotations.get(j)).getLength();
            }
        }
        return length;
    }
    
    public void writeDataTo(DataOutput dout) throws IOException {
        int size = mParameterAnnotations.size();
        dout.writeByte(size);
        for (int i=0; i<size; i++) {
            Vector annotations = (Vector)mParameterAnnotations.get(i);
            int subSize = annotations.size();
            dout.writeShort(subSize);
            for (int j=0; j<subSize; j++) {
                ((Annotation)annotations.get(j)).writeTo(dout);
            }
        }
    }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.