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  
20  package org.apache.bcel.classfile;
21  
22  import java.io.DataInput;
23  import java.io.DataOutputStream;
24  import java.io.IOException;
25  import java.util.Arrays;
26  import java.util.Iterator;
27  import java.util.stream.Stream;
28  
29  import org.apache.bcel.Const;
30  
31  /**
32   * This class represents a MethodParameters attribute.
33   *
34   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24"> The class File Format :
35   *      The MethodParameters Attribute</a>
36   * @since 6.0
37   */
38  public class MethodParameters extends Attribute implements Iterable<MethodParameter> {
39  
40      /**
41       * Empty array.
42       */
43      private static final MethodParameter[] EMPTY_ARRAY = {};
44  
45      private MethodParameter[] parameters = EMPTY_ARRAY;
46  
47      MethodParameters(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
48          super(Const.ATTR_METHOD_PARAMETERS, nameIndex, length, constantPool);
49          final int parameterCount = input.readUnsignedByte();
50          parameters = new MethodParameter[parameterCount];
51          for (int i = 0; i < parameterCount; i++) {
52              parameters[i] = new MethodParameter(input);
53          }
54      }
55  
56      @Override
57      public void accept(final Visitor v) {
58          v.visitMethodParameters(this);
59      }
60  
61      @Override
62      public Attribute copy(final ConstantPool constantPool) {
63          final MethodParameters c = (MethodParameters) clone();
64          c.parameters = new MethodParameter[parameters.length];
65          Arrays.setAll(c.parameters, i -> parameters[i].copy());
66          c.setConstantPool(constantPool);
67          return c;
68      }
69  
70      /**
71       * Dump method parameters attribute to file stream in binary format.
72       *
73       * @param file Output file stream
74       * @throws IOException if an I/O error occurs.
75       */
76      @Override
77      public void dump(final DataOutputStream file) throws IOException {
78          super.dump(file);
79          file.writeByte(parameters.length);
80          for (final MethodParameter parameter : parameters) {
81              parameter.dump(file);
82          }
83      }
84  
85      public MethodParameter[] getParameters() {
86          return parameters;
87      }
88  
89      @Override
90      public Iterator<MethodParameter> iterator() {
91          return Stream.of(parameters).iterator();
92      }
93  
94      public void setParameters(final MethodParameter[] parameters) {
95          this.parameters = parameters != null ? parameters : EMPTY_ARRAY;
96      }
97  }