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