001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.bcel.classfile; 021 022import java.io.DataInput; 023import java.io.DataOutputStream; 024import java.io.IOException; 025import java.util.Arrays; 026import java.util.Iterator; 027import java.util.stream.Stream; 028 029import org.apache.bcel.Const; 030import org.apache.bcel.util.Args; 031 032/** 033 * This class represents a MethodParameters attribute. 034 * 035 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24"> The class File Format : 036 * The MethodParameters Attribute</a> 037 * @since 6.0 038 */ 039public class MethodParameters extends Attribute implements Iterable<MethodParameter> { 040 041 /** 042 * Empty array. 043 */ 044 private static final MethodParameter[] EMPTY_ARRAY = {}; 045 046 private MethodParameter[] parameters = EMPTY_ARRAY; 047 048 MethodParameters(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException { 049 super(Const.ATTR_METHOD_PARAMETERS, nameIndex, length, constantPool); 050 final int parameterCount = input.readUnsignedByte(); 051 parameters = new MethodParameter[parameterCount]; 052 for (int i = 0; i < parameterCount; i++) { 053 parameters[i] = new MethodParameter(input); 054 } 055 } 056 057 @Override 058 public void accept(final Visitor v) { 059 v.visitMethodParameters(this); 060 } 061 062 @Override 063 public Attribute copy(final ConstantPool constantPool) { 064 final MethodParameters c = (MethodParameters) clone(); 065 c.parameters = new MethodParameter[parameters.length]; 066 Arrays.setAll(c.parameters, i -> parameters[i].copy()); 067 c.setConstantPool(constantPool); 068 return c; 069 } 070 071 /** 072 * Dumps method parameters attribute to file stream in binary format. 073 * 074 * @param file Output file stream. 075 * @throws IOException Thrown if an I/O error occurs. 076 */ 077 @Override 078 public void dump(final DataOutputStream file) throws IOException { 079 super.dump(file); 080 file.writeByte(Args.requireU1(parameters.length, "parameters.length")); 081 for (final MethodParameter parameter : parameters) { 082 parameter.dump(file); 083 } 084 } 085 086 /** 087 * Gets the method parameters. 088 * 089 * @return The method parameters. 090 */ 091 public MethodParameter[] getParameters() { 092 return parameters; 093 } 094 095 @Override 096 public Iterator<MethodParameter> iterator() { 097 return Stream.of(parameters).iterator(); 098 } 099 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}