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[] attributes) {
36          if (attributes == null) {
37              return EMPTY_ARRAY;
38          }
39          // Find attributes that contain parameter annotation data
40          final List<ParameterAnnotationEntry> accumulatedAnnotations = new ArrayList<>(attributes.length);
41          for (final Attribute attribute : attributes) {
42              if (attribute instanceof ParameterAnnotations) {
43                  final ParameterAnnotations runtimeAnnotations = (ParameterAnnotations) attribute;
44                  final ParameterAnnotationEntry[] parameterAnnotationEntries = runtimeAnnotations.getParameterAnnotationEntries();
45                  if (parameterAnnotationEntries != null) {
46                      Collections.addAll(accumulatedAnnotations, parameterAnnotationEntries);
47                  }
48              }
49          }
50          return accumulatedAnnotations.toArray(EMPTY_ARRAY);
51      }
52  
53      private final AnnotationEntry[] annotationTable;
54  
55      /**
56       * Constructs object from input stream.
57       *
58       * @param input Input stream
59       * @throws IOException if an I/O error occurs.
60       */
61      ParameterAnnotationEntry(final DataInput input, final ConstantPool constantPool) throws IOException {
62          final int annotationTableLength = input.readUnsignedShort();
63          annotationTable = new AnnotationEntry[annotationTableLength];
64          for (int i = 0; i < annotationTableLength; i++) {
65              // TODO isRuntimeVisible
66              annotationTable[i] = AnnotationEntry.read(input, constantPool, false);
67          }
68      }
69  
70      /**
71       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
72       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
73       *
74       * @param v Visitor object
75       */
76      @Override
77      public void accept(final Visitor v) {
78          v.visitParameterAnnotationEntry(this);
79      }
80  
81      public void dump(final DataOutputStream dos) throws IOException {
82          dos.writeShort(annotationTable.length);
83          for (final AnnotationEntry entry : annotationTable) {
84              entry.dump(dos);
85          }
86      }
87  
88      /**
89       * returns the array of annotation entries in this annotation
90       */
91      public AnnotationEntry[] getAnnotationEntries() {
92          return annotationTable;
93      }
94  }