View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.classfile;
18  
19  import java.io.DataInput;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  /**
27   * represents one parameter annotation in the parameter annotation table
28   *
29   * @since 6.0
30   */
31  public class ParameterAnnotationEntry implements Node {
32  
33      static final ParameterAnnotationEntry[] EMPTY_ARRAY = {};
34  
35      public static ParameterAnnotationEntry[] createParameterAnnotationEntries(final Attribute[] attrs) {
36          // Find attributes that contain parameter annotation data
37          final List<ParameterAnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);
38          for (final Attribute attribute : attrs) {
39              if (attribute instanceof ParameterAnnotations) {
40                  final ParameterAnnotations runtimeAnnotations = (ParameterAnnotations) attribute;
41                  Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getParameterAnnotationEntries());
42              }
43          }
44          return accumulatedAnnotations.toArray(ParameterAnnotationEntry.EMPTY_ARRAY);
45      }
46  
47      private final AnnotationEntry[] annotationTable;
48  
49      /**
50       * Constructs object from input stream.
51       *
52       * @param input Input stream
53       * @throws IOException if an I/O error occurs.
54       */
55      ParameterAnnotationEntry(final DataInput input, final ConstantPool constantPool) throws IOException {
56          final int annotationTableLength = input.readUnsignedShort();
57          annotationTable = new AnnotationEntry[annotationTableLength];
58          for (int i = 0; i < annotationTableLength; i++) {
59              // TODO isRuntimeVisible
60              annotationTable[i] = AnnotationEntry.read(input, constantPool, false);
61          }
62      }
63  
64      /**
65       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
66       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
67       *
68       * @param v Visitor object
69       */
70      @Override
71      public void accept(final Visitor v) {
72          v.visitParameterAnnotationEntry(this);
73      }
74  
75      public void dump(final DataOutputStream dos) throws IOException {
76          dos.writeShort(annotationTable.length);
77          for (final AnnotationEntry entry : annotationTable) {
78              entry.dump(dos);
79          }
80      }
81  
82      /**
83       * returns the array of annotation entries in this annotation
84       */
85      public AnnotationEntry[] getAnnotationEntries() {
86          return annotationTable;
87      }
88  }