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