MethodParameters.java

  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. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;
  21. import java.util.Arrays;
  22. import java.util.Iterator;
  23. import java.util.stream.Stream;

  24. import org.apache.bcel.Const;

  25. /**
  26.  * This class represents a MethodParameters attribute.
  27.  *
  28.  * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24"> The class File Format :
  29.  *      The MethodParameters Attribute</a>
  30.  * @since 6.0
  31.  */
  32. public class MethodParameters extends Attribute implements Iterable<MethodParameter> {

  33.     /**
  34.      * Empty array.
  35.      */
  36.     private static final MethodParameter[] EMPTY_ARRAY = {};

  37.     private MethodParameter[] parameters = EMPTY_ARRAY;

  38.     MethodParameters(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
  39.         super(Const.ATTR_METHOD_PARAMETERS, nameIndex, length, constantPool);
  40.         final int parameterCount = input.readUnsignedByte();
  41.         parameters = new MethodParameter[parameterCount];
  42.         for (int i = 0; i < parameterCount; i++) {
  43.             parameters[i] = new MethodParameter(input);
  44.         }
  45.     }

  46.     @Override
  47.     public void accept(final Visitor v) {
  48.         v.visitMethodParameters(this);
  49.     }

  50.     @Override
  51.     public Attribute copy(final ConstantPool constantPool) {
  52.         final MethodParameters c = (MethodParameters) clone();
  53.         c.parameters = new MethodParameter[parameters.length];
  54.         Arrays.setAll(c.parameters, i -> parameters[i].copy());
  55.         c.setConstantPool(constantPool);
  56.         return c;
  57.     }

  58.     /**
  59.      * Dump method parameters attribute to file stream in binary format.
  60.      *
  61.      * @param file Output file stream
  62.      * @throws IOException if an I/O error occurs.
  63.      */
  64.     @Override
  65.     public void dump(final DataOutputStream file) throws IOException {
  66.         super.dump(file);
  67.         file.writeByte(parameters.length);
  68.         for (final MethodParameter parameter : parameters) {
  69.             parameter.dump(file);
  70.         }
  71.     }

  72.     public MethodParameter[] getParameters() {
  73.         return parameters;
  74.     }

  75.     @Override
  76.     public Iterator<MethodParameter> iterator() {
  77.         return Stream.of(parameters).iterator();
  78.     }

  79.     public void setParameters(final MethodParameter[] parameters) {
  80.         this.parameters = parameters != null ? parameters : EMPTY_ARRAY;
  81.     }
  82. }