ConstantMethodType.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.  * This class is derived from the abstract {@link Constant} and represents a reference to a method type.
  24.  *
  25.  * @see Constant
  26.  * @since 6.0
  27.  */
  28. public final class ConstantMethodType extends Constant {

  29.     private int descriptorIndex;

  30.     /**
  31.      * Initialize from another object.
  32.      *
  33.      * @param c Source to copy.
  34.      */
  35.     public ConstantMethodType(final ConstantMethodType c) {
  36.         this(c.getDescriptorIndex());
  37.     }

  38.     /**
  39.      * Initialize instance from file data.
  40.      *
  41.      * @param file Input stream
  42.      * @throws IOException if an I/O error occurs.
  43.      */
  44.     ConstantMethodType(final DataInput file) throws IOException {
  45.         this(file.readUnsignedShort());
  46.     }

  47.     public ConstantMethodType(final int descriptorIndex) {
  48.         super(Const.CONSTANT_MethodType);
  49.         this.descriptorIndex = descriptorIndex;
  50.     }

  51.     /**
  52.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
  53.      * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  54.      *
  55.      * @param v Visitor object
  56.      */
  57.     @Override
  58.     public void accept(final Visitor v) {
  59.         v.visitConstantMethodType(this);
  60.     }

  61.     /**
  62.      * Dump name and signature index to file stream in binary format.
  63.      *
  64.      * @param file Output file stream
  65.      * @throws IOException if an I/O error occurs.
  66.      */
  67.     @Override
  68.     public void dump(final DataOutputStream file) throws IOException {
  69.         file.writeByte(super.getTag());
  70.         file.writeShort(descriptorIndex);
  71.     }

  72.     public int getDescriptorIndex() {
  73.         return descriptorIndex;
  74.     }

  75.     public void setDescriptorIndex(final int descriptorIndex) {
  76.         this.descriptorIndex = descriptorIndex;
  77.     }

  78.     /**
  79.      * @return String representation
  80.      */
  81.     @Override
  82.     public String toString() {
  83.         return super.toString() + "(descriptorIndex = " + descriptorIndex + ")";
  84.     }
  85. }