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