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