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  import org.apache.bcel.util.Args;
29  
30  /**
31   * represents one parameter annotation in the parameter annotation table
32   *
33   * @since 6.0
34   */
35  public class ParameterAnnotationEntry implements Node {
36  
37      static final ParameterAnnotationEntry[] EMPTY_ARRAY = {};
38  
39      /**
40       * Creates parameter annotation entries from attributes.
41       *
42       * @param attributes The attributes.
43       * @return The parameter annotation entries.
44       */
45      public static ParameterAnnotationEntry[] createParameterAnnotationEntries(final Attribute[] attributes) {
46          if (attributes == null) {
47              return EMPTY_ARRAY;
48          }
49          // Find attributes that contain parameter annotation data
50          final List<ParameterAnnotationEntry> accumulatedAnnotations = new ArrayList<>(attributes.length);
51          for (final Attribute attribute : attributes) {
52              if (attribute instanceof ParameterAnnotations) {
53                  final ParameterAnnotations runtimeAnnotations = (ParameterAnnotations) attribute;
54                  final ParameterAnnotationEntry[] parameterAnnotationEntries = runtimeAnnotations.getParameterAnnotationEntries();
55                  if (parameterAnnotationEntries != null) {
56                      Collections.addAll(accumulatedAnnotations, parameterAnnotationEntries);
57                  }
58              }
59          }
60          return accumulatedAnnotations.toArray(EMPTY_ARRAY);
61      }
62  
63      private final AnnotationEntry[] annotationTable;
64  
65      /**
66       * Constructs object from input stream.
67       *
68       * @param input Input stream.
69       * @param constantPool The constant pool.
70       * @param isRuntimeVisible whether the contained annotations are runtime visible.
71       * @throws IOException Thrown if an I/O error occurs.
72       */
73      ParameterAnnotationEntry(final DataInput input, final ConstantPool constantPool, final boolean isRuntimeVisible) throws IOException {
74          final int annotationTableLength = input.readUnsignedShort();
75          annotationTable = new AnnotationEntry[annotationTableLength];
76          for (int i = 0; i < annotationTableLength; i++) {
77              annotationTable[i] = AnnotationEntry.read(input, constantPool, isRuntimeVisible);
78          }
79      }
80  
81      /**
82       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
83       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
84       *
85       * @param v Visitor object.
86       */
87      @Override
88      public void accept(final Visitor v) {
89          v.visitParameterAnnotationEntry(this);
90      }
91  
92      /**
93       * Dumps parameter annotation entry to file stream.
94       *
95       * @param dos Output file stream.
96       * @throws IOException Thrown if an I/O error occurs.
97       */
98      public void dump(final DataOutputStream dos) throws IOException {
99          dos.writeShort(Args.requireU2(annotationTable.length, "annotationTable.length"));
100         for (final AnnotationEntry entry : annotationTable) {
101             entry.dump(dos);
102         }
103     }
104 
105     /**
106      * Gets the annotation entries.
107      *
108      * @return The array of annotation entries in this annotation.
109      */
110     public AnnotationEntry[] getAnnotationEntries() {
111         return annotationTable;
112     }
113 }