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