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  
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      public MethodParameter() {
47      }
48  
49      /**
50       * Constructs an instance from a DataInput.
51       *
52       * @param input Input stream
53       * @throws IOException if an I/O error occurs.
54       * @throws ClassFormatException if a class is malformed or cannot be interpreted as a class file
55       */
56      MethodParameter(final DataInput input) throws IOException {
57          nameIndex = input.readUnsignedShort();
58          accessFlags = input.readUnsignedShort();
59      }
60  
61      @Override
62      public void accept(final Visitor v) {
63          v.visitMethodParameter(this);
64      }
65  
66      /**
67       * @return deep copy of this object
68       */
69      public MethodParameter copy() {
70          try {
71              return (MethodParameter) clone();
72          } catch (final CloneNotSupportedException e) {
73              // TODO should this throw?
74          }
75          return null;
76      }
77  
78      /**
79       * Dumps object to file stream on binary format.
80       *
81       * @param file Output file stream
82       * @throws IOException if an I/O error occurs.
83       */
84      public final void dump(final DataOutputStream file) throws IOException {
85          file.writeShort(nameIndex);
86          file.writeShort(accessFlags);
87      }
88  
89      public int getAccessFlags() {
90          return accessFlags;
91      }
92  
93      public int getNameIndex() {
94          return nameIndex;
95      }
96  
97      /**
98       * Gets the name of the parameter.
99       *
100      * @param constantPool The pool to query.
101      * @return Constant from the given pool.
102      */
103     public String getParameterName(final ConstantPool constantPool) {
104         if (nameIndex == 0) {
105             return null;
106         }
107         return constantPool.getConstantUtf8(nameIndex).getBytes();
108     }
109 
110     public boolean isFinal() {
111         return (accessFlags & Const.ACC_FINAL) != 0;
112     }
113 
114     public boolean isMandated() {
115         return (accessFlags & Const.ACC_MANDATED) != 0;
116     }
117 
118     public boolean isSynthetic() {
119         return (accessFlags & Const.ACC_SYNTHETIC) != 0;
120     }
121 
122     public void setAccessFlags(final int accessFlags) {
123         this.accessFlags = accessFlags;
124     }
125 
126     public void setNameIndex(final int nameIndex) {
127         this.nameIndex = nameIndex;
128     }
129 }