    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.ArrayList;
    023import java.util.Collections;
    024import java.util.List;
    025
    026/**
    027 * represents one parameter annotation in the parameter annotation table
    028 *
    029 * @since 6.0
    030 */
    031public class ParameterAnnotationEntry implements Node {
    032
    033    static final ParameterAnnotationEntry[] EMPTY_ARRAY = {};
    034
    035    public static ParameterAnnotationEntry[] createParameterAnnotationEntries(final Attribute[] attrs) {
    036        // Find attributes that contain parameter annotation data
    037        final List<ParameterAnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);
    038        for (final Attribute attribute : attrs) {
    039            if (attribute instanceof ParameterAnnotations) {
    040                final ParameterAnnotations runtimeAnnotations = (ParameterAnnotations) attribute;
    041                Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getParameterAnnotationEntries());
    042            }
    043        }
    044        return accumulatedAnnotations.toArray(ParameterAnnotationEntry.EMPTY_ARRAY);
    045    }
    046
    047    private final AnnotationEntry[] annotationTable;
    048
    049    /**
    050     * Construct object from input stream.
    051     *
    052     * @param input Input stream
    053     * @throws IOException if an I/O error occurs.
    054     */
    055    ParameterAnnotationEntry(final DataInput input, final ConstantPool constantPool) throws IOException {
    056        final int annotationTableLength = input.readUnsignedShort();
    057        annotationTable = new AnnotationEntry[annotationTableLength];
    058        for (int i = 0; i < annotationTableLength; i++) {
    059            // TODO isRuntimeVisible
    060            annotationTable[i] = AnnotationEntry.read(input, constantPool, false);
    061        }
    062    }
    063
    064    /**
    065     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    066     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    067     *
    068     * @param v Visitor object
    069     */
    070    @Override
    071    public void accept(final Visitor v) {
    072        v.visitParameterAnnotationEntry(this);
    073    }
    074
    075    public void dump(final DataOutputStream dos) throws IOException {
    076        dos.writeShort(annotationTable.length);
    077        for (final AnnotationEntry entry : annotationTable) {
    078            entry.dump(dos);
    079        }
    080    }
    081
    082    /**
    083     * returns the array of annotation entries in this annotation
    084     */
    085    public AnnotationEntry[] getAnnotationEntries() {
    086        return annotationTable;
    087    }
    088}