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