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
26 import org.apache.bcel.Const;
27
28 /**
29 * Entry of the parameters table.
30 * <p>
31 * Implements {@link Node} as of 6.7.0.
32 * </p>
33 *
34 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24"> The class File Format :
35 * The MethodParameters Attribute</a>
36 * @since 6.0
37 */
38 public class MethodParameter implements Cloneable, Node {
39
40 /** Index of the CONSTANT_Utf8_info structure in the constant_pool table representing the name of the parameter */
41 private int nameIndex;
42
43 /** The access flags */
44 private int accessFlags;
45
46 /**
47 * Constructs a MethodParameter.
48 */
49 public MethodParameter() {
50 }
51
52 /**
53 * Constructs an instance from a DataInput.
54 *
55 * @param input Input stream.
56 * @throws IOException if an I/O error occurs.
57 * @throws ClassFormatException if a class is malformed or cannot be interpreted as a class file
58 */
59 MethodParameter(final DataInput input) throws IOException {
60 nameIndex = input.readUnsignedShort();
61 accessFlags = input.readUnsignedShort();
62 }
63
64 @Override
65 public void accept(final Visitor v) {
66 v.visitMethodParameter(this);
67 }
68
69 /**
70 * Creates a deep copy of this object.
71 *
72 * @return deep copy of this object.
73 */
74 public MethodParameter copy() {
75 try {
76 return (MethodParameter) clone();
77 } catch (final CloneNotSupportedException e) {
78 // TODO should this throw?
79 }
80 return null;
81 }
82
83 /**
84 * Dumps object to file stream on binary format.
85 *
86 * @param file Output file stream.
87 * @throws IOException if an I/O error occurs.
88 */
89 public final void dump(final DataOutputStream file) throws IOException {
90 file.writeShort(nameIndex);
91 file.writeShort(accessFlags);
92 }
93
94 /**
95 * Gets the access flags.
96 *
97 * @return the access flags.
98 */
99 public int getAccessFlags() {
100 return accessFlags;
101 }
102
103 /**
104 * Gets the name index.
105 *
106 * @return the name index.
107 */
108 public int getNameIndex() {
109 return nameIndex;
110 }
111
112 /**
113 * Gets the name of the parameter.
114 *
115 * @param constantPool The pool to query.
116 * @return Constant from the given pool.
117 */
118 public String getParameterName(final ConstantPool constantPool) {
119 if (nameIndex == 0) {
120 return null;
121 }
122 return constantPool.getConstantUtf8(nameIndex).getBytes();
123 }
124
125 /**
126 * Checks if this parameter is final.
127 *
128 * @return true if this parameter is final.
129 */
130 public boolean isFinal() {
131 return (accessFlags & Const.ACC_FINAL) != 0;
132 }
133
134 /**
135 * Checks if this parameter is mandated.
136 *
137 * @return true if this parameter is mandated.
138 */
139 public boolean isMandated() {
140 return (accessFlags & Const.ACC_MANDATED) != 0;
141 }
142
143 /**
144 * Checks if this parameter is synthetic.
145 *
146 * @return true if this parameter is synthetic.
147 */
148 public boolean isSynthetic() {
149 return (accessFlags & Const.ACC_SYNTHETIC) != 0;
150 }
151
152 /**
153 * Sets the access flags.
154 *
155 * @param accessFlags the access flags.
156 */
157 public void setAccessFlags(final int accessFlags) {
158 this.accessFlags = accessFlags;
159 }
160
161 /**
162 * Sets the name index.
163 *
164 * @param nameIndex the name index.
165 */
166 public void setNameIndex(final int nameIndex) {
167 this.nameIndex = nameIndex;
168 }
169 }