    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.bcel.classfile;
    018
    019import java.io.DataInput;
    020import java.io.DataOutputStream;
    021import java.io.IOException;
    022import java.util.Iterator;
    023import java.util.stream.Stream;
    024
    025/**
    026 * base class for parameter annotations
    027 *
    028 * @since 6.0
    029 */
    030public abstract class ParameterAnnotations extends Attribute implements Iterable<ParameterAnnotationEntry> {
    031
    032    /** Table of parameter annotations */
    033    private ParameterAnnotationEntry[] parameterAnnotationTable;
    034
    035    /**
    036     * @param parameterAnnotationType the subclass type of the parameter annotation
    037     * @param nameIndex Index pointing to the name <em>Code</em>
    038     * @param length Content length in bytes
    039     * @param input Input stream
    040     * @param constantPool Array of constants
    041     */
    042    ParameterAnnotations(final byte parameterAnnotationType, final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
    043        throws IOException {
    044        this(parameterAnnotationType, nameIndex, length, (ParameterAnnotationEntry[]) null, constantPool);
    045        final int numParameters = input.readUnsignedByte();
    046        parameterAnnotationTable = new ParameterAnnotationEntry[numParameters];
    047        for (int i = 0; i < numParameters; i++) {
    048            parameterAnnotationTable[i] = new ParameterAnnotationEntry(input, constantPool);
    049        }
    050    }
    051
    052    /**
    053     * @param parameterAnnotationType the subclass type of the parameter annotation
    054     * @param nameIndex Index pointing to the name <em>Code</em>
    055     * @param length Content length in bytes
    056     * @param parameterAnnotationTable the actual parameter annotations
    057     * @param constantPool Array of constants
    058     */
    059    public ParameterAnnotations(final byte parameterAnnotationType, final int nameIndex, final int length,
    060        final ParameterAnnotationEntry[] parameterAnnotationTable, final ConstantPool constantPool) {
    061        super(parameterAnnotationType, nameIndex, length, constantPool);
    062        this.parameterAnnotationTable = parameterAnnotationTable;
    063    }
    064
    065    /**
    066     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    067     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    068     *
    069     * @param v Visitor object
    070     */
    071    @Override
    072    public void accept(final Visitor v) {
    073        v.visitParameterAnnotation(this);
    074    }
    075
    076    /**
    077     * @return deep copy of this attribute
    078     */
    079    @Override
    080    public Attribute copy(final ConstantPool constantPool) {
    081        return (Attribute) clone();
    082    }
    083
    084    @Override
    085    public void dump(final DataOutputStream dos) throws IOException {
    086        super.dump(dos);
    087        dos.writeByte(parameterAnnotationTable.length);
    088
    089        for (final ParameterAnnotationEntry element : parameterAnnotationTable) {
    090            element.dump(dos);
    091        }
    092
    093    }
    094
    095    /**
    096     * returns the array of parameter annotation entries in this parameter annotation
    097     */
    098    public ParameterAnnotationEntry[] getParameterAnnotationEntries() {
    099        return parameterAnnotationTable;
    100    }
    101
    102    /**
    103     * @return the parameter annotation entry table
    104     */
    105    public final ParameterAnnotationEntry[] getParameterAnnotationTable() {
    106        return parameterAnnotationTable;
    107    }
    108
    109    @Override
    110    public Iterator<ParameterAnnotationEntry> iterator() {
    111        return Stream.of(parameterAnnotationTable).iterator();
    112    }
    113
    114    /**
    115     * @param parameterAnnotationTable the entries to set in this parameter annotation
    116     */
    117    public final void setParameterAnnotationTable(final ParameterAnnotationEntry[] parameterAnnotationTable) {
    118        this.parameterAnnotationTable = parameterAnnotationTable;
    119    }
    120}