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   *
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 MethodParameter implements Cloneable {
34  
35      /** Index of the CONSTANT_Utf8_info structure in the constant_pool table representing the name of the parameter */
36      private int name_index;
37  
38      /** The access flags */
39      private int access_flags;
40  
41      public MethodParameter() {
42      }
43  
44      /**
45       * Construct object from input stream.
46       *
47       * @param input Input stream
48       * @throws java.io.IOException
49       * @throws ClassFormatException
50       */
51      MethodParameter(final DataInput input) throws IOException {
52          name_index = input.readUnsignedShort();
53          access_flags = input.readUnsignedShort();
54      }
55  
56      public int getNameIndex() {
57          return name_index;
58      }
59  
60      public void setNameIndex(final int name_index) {
61          this.name_index = name_index;
62      }
63  
64      /**
65       * Returns the name of the parameter.
66       */
67      public String getParameterName(final ConstantPool constant_pool) {
68          if (name_index == 0) {
69              return null;
70          }
71          return ((ConstantUtf8) constant_pool.getConstant(name_index, Const.CONSTANT_Utf8)).getBytes();
72         }
73  
74      public int getAccessFlags() {
75          return access_flags;
76      }
77  
78      public void setAccessFlags(final int access_flags) {
79          this.access_flags = access_flags;
80      }
81  
82      public boolean isFinal() {
83          return (access_flags & Const.ACC_FINAL) != 0;
84      }
85  
86      public boolean isSynthetic() {
87          return (access_flags & Const.ACC_SYNTHETIC) != 0;
88      }
89  
90      public boolean isMandated() {
91          return (access_flags & Const.ACC_MANDATED) != 0;
92      }
93  
94      /**
95       * Dump object to file stream on binary format.
96       *
97       * @param file Output file stream
98       * @throws IOException
99       */
100     public final void dump(final DataOutputStream file) throws IOException {
101         file.writeShort(name_index);
102         file.writeShort(access_flags);
103     }
104 
105     /**
106      * @return deep copy of this object
107      */
108     public MethodParameter copy() {
109         try {
110             return (MethodParameter) clone();
111         } catch (final CloneNotSupportedException e) {
112             // TODO should this throw?
113         }
114         return null;
115     }
116 }