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