ConstantMethodHandle.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 handle.
  24.  *
  25.  * @see Constant
  26.  * @since 6.0
  27.  */
  28. public final class ConstantMethodHandle extends Constant {

  29.     private int referenceKind;
  30.     private int referenceIndex;

  31.     /**
  32.      * Initialize from another object.
  33.      *
  34.      * @param c Source to copy.
  35.      */
  36.     public ConstantMethodHandle(final ConstantMethodHandle c) {
  37.         this(c.getReferenceKind(), c.getReferenceIndex());
  38.     }

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

  48.     public ConstantMethodHandle(final int referenceKind, final int referenceIndex) {
  49.         super(Const.CONSTANT_MethodHandle);
  50.         this.referenceKind = referenceKind;
  51.         this.referenceIndex = referenceIndex;
  52.     }

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

  63.     /**
  64.      * Dump method kind and index to file stream in binary format.
  65.      *
  66.      * @param file Output file stream
  67.      * @throws IOException if an I/O error occurs.
  68.      */
  69.     @Override
  70.     public void dump(final DataOutputStream file) throws IOException {
  71.         file.writeByte(super.getTag());
  72.         file.writeByte(referenceKind);
  73.         file.writeShort(referenceIndex);
  74.     }

  75.     public int getReferenceIndex() {
  76.         return referenceIndex;
  77.     }

  78.     public int getReferenceKind() {
  79.         return referenceKind;
  80.     }

  81.     public void setReferenceIndex(final int referenceIndex) {
  82.         this.referenceIndex = referenceIndex;
  83.     }

  84.     public void setReferenceKind(final int referenceKind) {
  85.         this.referenceKind = referenceKind;
  86.     }

  87.     /**
  88.      * @return String representation
  89.      */
  90.     @Override
  91.     public String toString() {
  92.         return super.toString() + "(referenceKind = " + referenceKind + ", referenceIndex = " + referenceIndex + ")";
  93.     }
  94. }