1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import java.io.DataInput;
  25 import java.io.DataOutputStream;
  26 import java.io.IOException;
  27 
  28 import com.sun.org.apache.bcel.internal.Const;
  29 
  30 /**
  31  * This class is derived from the abstract {@link Constant}
  32  * and represents a reference to a method handle.
  33  *
  34  * @see     Constant
  35  * @since 6.0
  36  */
  37 public final class ConstantMethodHandle extends Constant {
  38 
  39     private int reference_kind;
  40     private int reference_index;
  41 
  42 
  43     /**
  44      * Initialize from another object.
  45      */
  46     public ConstantMethodHandle(final ConstantMethodHandle c) {
  47         this(c.getReferenceKind(), c.getReferenceIndex());
  48     }
  49 
  50 
  51     /**
  52      * Initialize instance from file data.
  53      *
  54      * @param file Input stream
  55      * @throws IOException
  56      */
  57     ConstantMethodHandle(final DataInput file) throws IOException {
  58         this(file.readUnsignedByte(), file.readUnsignedShort());
  59     }
  60 
  61 
  62     public ConstantMethodHandle(final int reference_kind, final int reference_index) {
  63         super(Const.CONSTANT_MethodHandle);
  64         this.reference_kind = reference_kind;
  65         this.reference_index = reference_index;
  66     }
  67 
  68 
  69     /**
  70      * Called by objects that are traversing the nodes of the tree implicitly
  71      * defined by the contents of a Java class. I.e., the hierarchy of methods,
  72      * fields, attributes, etc. spawns a tree of objects.
  73      *
  74      * @param v Visitor object
  75      */
  76     @Override
  77     public void accept( final Visitor v ) {
  78         v.visitConstantMethodHandle(this);
  79     }
  80 
  81 
  82     /**
  83      * Dump method kind and index to file stream in binary format.
  84      *
  85      * @param file Output file stream
  86      * @throws IOException
  87      */
  88     @Override
  89     public final void dump( final DataOutputStream file ) throws IOException {
  90         file.writeByte(super.getTag());
  91         file.writeByte(reference_kind);
  92         file.writeShort(reference_index);
  93     }
  94 
  95 
  96     public int getReferenceKind() {
  97         return reference_kind;
  98     }
  99 
 100 
 101     public void setReferenceKind(final int reference_kind) {
 102         this.reference_kind = reference_kind;
 103     }
 104 
 105 
 106     public int getReferenceIndex() {
 107         return reference_index;
 108     }
 109 
 110 
 111     public void setReferenceIndex(final int reference_index) {
 112         this.reference_index = reference_index;
 113     }
 114 
 115 
 116     /**
 117      * @return String representation
 118      */
 119     @Override
 120     public final String toString() {
 121         return super.toString() + "(reference_kind = " + reference_kind +
 122                 ", reference_index = " + reference_index + ")";
 123     }
 124 }