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 
24 import org.apache.bcel.Const;
25 
26 /**
27  * This class represents a MethodParameters attribute.
28  *
29  * @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24">
30  * The class File Format : The MethodParameters Attribute</a>
31  * @since 6.0
32  */
33 public class MethodParameters extends Attribute {
34 
35     private MethodParameter[] parameters = new MethodParameter[0];
36 
MethodParameters(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)37     MethodParameters(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
38         super(Const.ATTR_METHOD_PARAMETERS, name_index, length, constant_pool);
39 
40         final int parameters_count = input.readUnsignedByte();
41         parameters = new MethodParameter[parameters_count];
42         for (int i = 0; i < parameters_count; i++) {
43             parameters[i] = new MethodParameter(input);
44         }
45     }
46 
getParameters()47     public MethodParameter[] getParameters() {
48         return parameters;
49     }
50 
setParameters(final MethodParameter[] parameters)51     public void setParameters(final MethodParameter[] parameters) {
52         this.parameters = parameters;
53     }
54 
55     @Override
accept(final Visitor v)56     public void accept(final Visitor v) {
57         v.visitMethodParameters(this);
58     }
59 
60     @Override
copy(final ConstantPool _constant_pool)61     public Attribute copy(final ConstantPool _constant_pool) {
62         final MethodParameters c = (MethodParameters) clone();
63         c.parameters = new MethodParameter[parameters.length];
64 
65         for (int i = 0; i < parameters.length; i++) {
66             c.parameters[i] = parameters[i].copy();
67         }
68         c.setConstantPool(_constant_pool);
69         return c;
70     }
71 
72     /**
73      * Dump method parameters attribute to file stream in binary format.
74      *
75      * @param file Output file stream
76      * @throws IOException
77      */
78     @Override
dump(final DataOutputStream file)79        public void dump(final DataOutputStream file) throws IOException {
80            super.dump(file);
81            file.writeByte(parameters.length);
82         for (final MethodParameter parameter : parameters) {
83             parameter.dump(file);
84         }
85     }
86 }
87