    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    private static final ParameterAnnotationEntry[] EMPTY_ARRAY = {};
    033
    034    /** Table of parameter annotations */
    035    private ParameterAnnotationEntry[] parameterAnnotationTable;
    036
    037    /**
    038     * Constructs a new instance.
    039     *
    040     * @param parameterAnnotationType the subclass type of the parameter annotation
    041     * @param nameIndex Index pointing to the name <em>Code</em>
    042     * @param length Content length in bytes
    043     * @param input Input stream
    044     * @param constantPool Array of constants
    045     */
    046    ParameterAnnotations(final byte parameterAnnotationType, final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
    047        throws IOException {
    048        this(parameterAnnotationType, nameIndex, length, (ParameterAnnotationEntry[]) null, constantPool);
    049        final int numParameters = input.readUnsignedByte();
    050        parameterAnnotationTable = new ParameterAnnotationEntry[numParameters];
    051        for (int i = 0; i < numParameters; i++) {
    052            parameterAnnotationTable[i] = new ParameterAnnotationEntry(input, constantPool);
    053        }
    054    }
    055
    056    /**
    057     * Constructs a new instance.
    058     *
    059     * @param parameterAnnotationType the subclass type of the parameter annotation
    060     * @param nameIndex Index pointing to the name <em>Code</em>
    061     * @param length Content length in bytes
    062     * @param parameterAnnotationTable the actual parameter annotations
    063     * @param constantPool Array of constants
    064     */
    065    public ParameterAnnotations(final byte parameterAnnotationType, final int nameIndex, final int length,
    066        final ParameterAnnotationEntry[] parameterAnnotationTable, final ConstantPool constantPool) {
    067        super(parameterAnnotationType, nameIndex, length, constantPool);
    068        this.parameterAnnotationTable = parameterAnnotationTable;
    069    }
    070
    071    /**
    072     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    073     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    074     *
    075     * @param v Visitor object
    076     */
    077    @Override
    078    public void accept(final Visitor v) {
    079        v.visitParameterAnnotation(this);
    080    }
    081
    082    /**
    083     * @return deep copy of this attribute
    084     */
    085    @Override
    086    public Attribute copy(final ConstantPool constantPool) {
    087        return (Attribute) clone();
    088    }
    089
    090    @Override
    091    public void dump(final DataOutputStream dos) throws IOException {
    092        super.dump(dos);
    093        dos.writeByte(parameterAnnotationTable.length);
    094
    095        for (final ParameterAnnotationEntry element : parameterAnnotationTable) {
    096            element.dump(dos);
    097        }
    098
    099    }
    100
    101    /**
    102     * returns the array of parameter annotation entries in this parameter annotation
    103     */
    104    public ParameterAnnotationEntry[] getParameterAnnotationEntries() {
    105        return parameterAnnotationTable;
    106    }
    107
    108    /**
    109     * @return the parameter annotation entry table
    110     */
    111    public final ParameterAnnotationEntry[] getParameterAnnotationTable() {
    112        return parameterAnnotationTable;
    113    }
    114
    115    @Override
    116    public Iterator<ParameterAnnotationEntry> iterator() {
    117        return Stream.of(parameterAnnotationTable).iterator();
    118    }
    119
    120    /**
    121     * @param parameterAnnotationTable the entries to set in this parameter annotation
    122     */
    123    public final void setParameterAnnotationTable(final ParameterAnnotationEntry[] parameterAnnotationTable) {
    124        this.parameterAnnotationTable = parameterAnnotationTable != null ? parameterAnnotationTable : EMPTY_ARRAY;
    125    }
    126}