MethodParameter.java

  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. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;

  22. /**
  23.  * Entry of the parameters table.
  24.  * <p>
  25.  * Implements {@link Node} as of 6.7.0.
  26.  * </p>
  27.  *
  28.  * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24"> The class File Format :
  29.  *      The MethodParameters Attribute</a>
  30.  * @since 6.0
  31.  */
  32. public class MethodParameter implements Cloneable, Node {

  33.     /** Index of the CONSTANT_Utf8_info structure in the constant_pool table representing the name of the parameter */
  34.     private int nameIndex;

  35.     /** The access flags */
  36.     private int accessFlags;

  37.     public MethodParameter() {
  38.     }

  39.     /**
  40.      * Constructs an instance from a DataInput.
  41.      *
  42.      * @param input Input stream
  43.      * @throws IOException if an I/O error occurs.
  44.      * @throws ClassFormatException if a class is malformed or cannot be interpreted as a class file
  45.      */
  46.     MethodParameter(final DataInput input) throws IOException {
  47.         nameIndex = input.readUnsignedShort();
  48.         accessFlags = input.readUnsignedShort();
  49.     }

  50.     @Override
  51.     public void accept(final Visitor v) {
  52.         v.visitMethodParameter(this);
  53.     }

  54.     /**
  55.      * @return deep copy of this object
  56.      */
  57.     public MethodParameter copy() {
  58.         try {
  59.             return (MethodParameter) clone();
  60.         } catch (final CloneNotSupportedException e) {
  61.             // TODO should this throw?
  62.         }
  63.         return null;
  64.     }

  65.     /**
  66.      * Dumps object to file stream on binary format.
  67.      *
  68.      * @param file Output file stream
  69.      * @throws IOException if an I/O error occurs.
  70.      */
  71.     public final void dump(final DataOutputStream file) throws IOException {
  72.         file.writeShort(nameIndex);
  73.         file.writeShort(accessFlags);
  74.     }

  75.     public int getAccessFlags() {
  76.         return accessFlags;
  77.     }

  78.     public int getNameIndex() {
  79.         return nameIndex;
  80.     }

  81.     /**
  82.      * Gets the name of the parameter.
  83.      *
  84.      * @param constantPool The pool to query.
  85.      * @return Constant from the given pool.
  86.      */
  87.     public String getParameterName(final ConstantPool constantPool) {
  88.         if (nameIndex == 0) {
  89.             return null;
  90.         }
  91.         return constantPool.getConstantUtf8(nameIndex).getBytes();
  92.     }

  93.     public boolean isFinal() {
  94.         return (accessFlags & Const.ACC_FINAL) != 0;
  95.     }

  96.     public boolean isMandated() {
  97.         return (accessFlags & Const.ACC_MANDATED) != 0;
  98.     }

  99.     public boolean isSynthetic() {
  100.         return (accessFlags & Const.ACC_SYNTHETIC) != 0;
  101.     }

  102.     public void setAccessFlags(final int accessFlags) {
  103.         this.accessFlags = accessFlags;
  104.     }

  105.     public void setNameIndex(final int nameIndex) {
  106.         this.nameIndex = nameIndex;
  107.     }
  108. }