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