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   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  /**
28   * represents one parameter annotation in the parameter annotation table
29   *
30   * @since 6.0
31   */
32  public class ParameterAnnotationEntry implements Node {
33  
34      private final AnnotationEntry[] annotationTable;
35  
36  
37      /**
38       * Construct object from input stream.
39       *
40       * @param input Input stream
41       * @throws IOException
42       */
43      ParameterAnnotationEntry(final DataInput input, final ConstantPool constant_pool) throws IOException {
44          final int annotation_table_length = input.readUnsignedShort();
45          annotationTable = new AnnotationEntry[annotation_table_length];
46          for (int i = 0; i < annotation_table_length; i++) {
47              // TODO isRuntimeVisible
48              annotationTable[i] = AnnotationEntry.read(input, constant_pool, false);
49          }
50      }
51  
52  
53      /**
54       * Called by objects that are traversing the nodes of the tree implicitely
55       * defined by the contents of a Java class. I.e., the hierarchy of methods,
56       * fields, attributes, etc. spawns a tree of objects.
57       *
58       * @param v Visitor object
59       */
60      @Override
61      public void accept( final Visitor v ) {
62          v.visitParameterAnnotationEntry(this);
63      }
64  
65      /**
66       * returns the array of annotation entries in this annotation
67       */
68      public AnnotationEntry[] getAnnotationEntries() {
69          return annotationTable;
70      }
71  
72      public void dump(final DataOutputStream dos) throws IOException {
73          dos.writeShort(annotationTable.length);
74          for (final AnnotationEntry entry : annotationTable) {
75              entry.dump(dos);
76          }
77      }
78  
79    public static ParameterAnnotationEntry[] createParameterAnnotationEntries(final Attribute[] attrs) {
80        // Find attributes that contain parameter annotation data
81        final List<ParameterAnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);
82        for (final Attribute attribute : attrs) {
83            if (attribute instanceof ParameterAnnotations) {
84                final ParameterAnnotationsl/classfile/ParameterAnnotations.html#ParameterAnnotations">ParameterAnnotations runtimeAnnotations = (ParameterAnnotations)attribute;
85                Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getParameterAnnotationEntries());
86            }
87        }
88        return accumulatedAnnotations.toArray(new ParameterAnnotationEntry[accumulatedAnnotations.size()]);
89    }
90  }
91