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  
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Iterator;
25  import java.util.stream.Stream;
26  
27  import org.apache.bcel.Const;
28  
29  /**
30   * This class represents a MethodParameters attribute.
31   *
32   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24"> The class File Format :
33   *      The MethodParameters Attribute</a>
34   * @since 6.0
35   */
36  public class MethodParameters extends Attribute implements Iterable<MethodParameter> {
37  
38      /**
39       * Empty array.
40       */
41      private static final MethodParameter[] EMPTY_METHOD_PARAMETER_ARRAY = {};
42  
43      private MethodParameter[] parameters = EMPTY_METHOD_PARAMETER_ARRAY;
44  
45      MethodParameters(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
46          super(Const.ATTR_METHOD_PARAMETERS, nameIndex, length, constantPool);
47  
48          final int parameterCount = input.readUnsignedByte();
49          parameters = new MethodParameter[parameterCount];
50          for (int i = 0; i < parameterCount; i++) {
51              parameters[i] = new MethodParameter(input);
52          }
53      }
54  
55      @Override
56      public void accept(final Visitor v) {
57          v.visitMethodParameters(this);
58      }
59  
60      @Override
61      public Attribute copy(final ConstantPool constantPool) {
62          final MethodParameters c = (MethodParameters) clone();
63          c.parameters = new MethodParameter[parameters.length];
64  
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;
96      }
97  }