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