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_ARRAY = {};
42  
43      private MethodParameter[] parameters = EMPTY_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          final int parameterCount = input.readUnsignedByte();
48          parameters = new MethodParameter[parameterCount];
49          for (int i = 0; i < parameterCount; i++) {
50              parameters[i] = new MethodParameter(input);
51          }
52      }
53  
54      @Override
55      public void accept(final Visitor v) {
56          v.visitMethodParameters(this);
57      }
58  
59      @Override
60      public Attribute copy(final ConstantPool constantPool) {
61          final MethodParameters c = (MethodParameters) clone();
62          c.parameters = new MethodParameter[parameters.length];
63          Arrays.setAll(c.parameters, i -> parameters[i].copy());
64          c.setConstantPool(constantPool);
65          return c;
66      }
67  
68      /**
69       * Dump method parameters attribute to file stream in binary format.
70       *
71       * @param file Output file stream
72       * @throws IOException if an I/O error occurs.
73       */
74      @Override
75      public void dump(final DataOutputStream file) throws IOException {
76          super.dump(file);
77          file.writeByte(parameters.length);
78          for (final MethodParameter parameter : parameters) {
79              parameter.dump(file);
80          }
81      }
82  
83      public MethodParameter[] getParameters() {
84          return parameters;
85      }
86  
87      @Override
88      public Iterator<MethodParameter> iterator() {
89          return Stream.of(parameters).iterator();
90      }
91  
92      public void setParameters(final MethodParameter[] parameters) {
93          this.parameters = parameters != null ? parameters : EMPTY_ARRAY;
94      }
95  }