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  */
18 package org.apache.bcel.classfile;
19 
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 
24 import org.apache.bcel.Const;
25 
26 /**
27  * This class is derived from the abstract {@link Constant}
28  * 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 reference_kind;
36     private int reference_index;
37 
38 
39     /**
40      * Initialize from another object.
41      */
ConstantMethodHandle(final ConstantMethodHandle c)42     public ConstantMethodHandle(final ConstantMethodHandle c) {
43         this(c.getReferenceKind(), c.getReferenceIndex());
44     }
45 
46 
47     /**
48      * Initialize instance from file data.
49      *
50      * @param file Input stream
51      * @throws IOException
52      */
ConstantMethodHandle(final DataInput file)53     ConstantMethodHandle(final DataInput file) throws IOException {
54         this(file.readUnsignedByte(), file.readUnsignedShort());
55     }
56 
57 
ConstantMethodHandle(final int reference_kind, final int reference_index)58     public ConstantMethodHandle(final int reference_kind, final int reference_index) {
59         super(Const.CONSTANT_MethodHandle);
60         this.reference_kind = reference_kind;
61         this.reference_index = reference_index;
62     }
63 
64 
65     /**
66      * Called by objects that are traversing the nodes of the tree implicitly
67      * defined by the contents of a Java class. I.e., the hierarchy of methods,
68      * fields, attributes, etc. spawns a tree of objects.
69      *
70      * @param v Visitor object
71      */
72     @Override
accept( final Visitor v )73     public void accept( final Visitor v ) {
74         v.visitConstantMethodHandle(this);
75     }
76 
77 
78     /**
79      * Dump method kind and index to file stream in binary format.
80      *
81      * @param file Output file stream
82      * @throws IOException
83      */
84     @Override
dump( final DataOutputStream file )85     public final void dump( final DataOutputStream file ) throws IOException {
86         file.writeByte(super.getTag());
87         file.writeByte(reference_kind);
88         file.writeShort(reference_index);
89     }
90 
91 
getReferenceKind()92     public int getReferenceKind() {
93         return reference_kind;
94     }
95 
96 
setReferenceKind(final int reference_kind)97     public void setReferenceKind(final int reference_kind) {
98         this.reference_kind = reference_kind;
99     }
100 
101 
getReferenceIndex()102     public int getReferenceIndex() {
103         return reference_index;
104     }
105 
106 
setReferenceIndex(final int reference_index)107     public void setReferenceIndex(final int reference_index) {
108         this.reference_index = reference_index;
109     }
110 
111 
112     /**
113      * @return String representation
114      */
115     @Override
toString()116     public final String toString() {
117         return super.toString() + "(reference_kind = " + reference_kind +
118                 ", reference_index = " + reference_index + ")";
119     }
120 }
121