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