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.Iterator;
25  import java.util.stream.Stream;
26  
27  /**
28   * base class for parameter annotations
29   *
30   * @since 6.0
31   */
32  public abstract class ParameterAnnotations extends Attribute implements Iterable<ParameterAnnotationEntry> {
33  
34      private static final ParameterAnnotationEntry[] EMPTY_ARRAY = {};
35  
36      /** Table of parameter annotations */
37      private ParameterAnnotationEntry[] parameterAnnotationTable;
38  
39      /**
40       * Constructs a new instance.
41       *
42       * @param parameterAnnotationType the subclass type of the parameter annotation
43       * @param nameIndex Index pointing to the name <em>Code</em>
44       * @param length Content length in bytes
45       * @param input Input stream
46       * @param constantPool Array of constants
47       */
48      ParameterAnnotations(final byte parameterAnnotationType, final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
49          throws IOException {
50          this(parameterAnnotationType, nameIndex, length, (ParameterAnnotationEntry[]) null, constantPool);
51          final int numParameters = input.readUnsignedByte();
52          parameterAnnotationTable = new ParameterAnnotationEntry[numParameters];
53          for (int i = 0; i < numParameters; i++) {
54              parameterAnnotationTable[i] = new ParameterAnnotationEntry(input, constantPool);
55          }
56      }
57  
58      /**
59       * Constructs a new instance.
60       *
61       * @param parameterAnnotationType the subclass type of the parameter annotation
62       * @param nameIndex Index pointing to the name <em>Code</em>
63       * @param length Content length in bytes
64       * @param parameterAnnotationTable the actual parameter annotations
65       * @param constantPool Array of constants
66       */
67      public ParameterAnnotations(final byte parameterAnnotationType, final int nameIndex, final int length,
68          final ParameterAnnotationEntry[] parameterAnnotationTable, final ConstantPool constantPool) {
69          super(parameterAnnotationType, nameIndex, length, constantPool);
70          this.parameterAnnotationTable = parameterAnnotationTable;
71      }
72  
73      /**
74       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
75       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
76       *
77       * @param v Visitor object
78       */
79      @Override
80      public void accept(final Visitor v) {
81          v.visitParameterAnnotation(this);
82      }
83  
84      /**
85       * @return deep copy of this attribute
86       */
87      @Override
88      public Attribute copy(final ConstantPool constantPool) {
89          return (Attribute) clone();
90      }
91  
92      @Override
93      public void dump(final DataOutputStream dos) throws IOException {
94          super.dump(dos);
95          dos.writeByte(parameterAnnotationTable.length);
96  
97          for (final ParameterAnnotationEntry element : parameterAnnotationTable) {
98              element.dump(dos);
99          }
100 
101     }
102 
103     /**
104      * returns the array of parameter annotation entries in this parameter annotation
105      */
106     public ParameterAnnotationEntry[] getParameterAnnotationEntries() {
107         return parameterAnnotationTable;
108     }
109 
110     /**
111      * @return the parameter annotation entry table
112      */
113     public final ParameterAnnotationEntry[] getParameterAnnotationTable() {
114         return parameterAnnotationTable;
115     }
116 
117     @Override
118     public Iterator<ParameterAnnotationEntry> iterator() {
119         return Stream.of(parameterAnnotationTable).iterator();
120     }
121 
122     /**
123      * @param parameterAnnotationTable the entries to set in this parameter annotation
124      */
125     public final void setParameterAnnotationTable(final ParameterAnnotationEntry[] parameterAnnotationTable) {
126         this.parameterAnnotationTable = parameterAnnotationTable != null ? parameterAnnotationTable : EMPTY_ARRAY;
127     }
128 }