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