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