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