001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.classfile;
020
021import java.io.DataInput;
022import java.io.DataOutputStream;
023import java.io.IOException;
024
025import org.apache.bcel.Const;
026
027/**
028 * This class is derived from the abstract {@link Constant} and represents a reference to a method handle.
029 *
030 * @see Constant
031 * @since 6.0
032 */
033public final class ConstantMethodHandle extends Constant {
034
035    private int referenceKind;
036    private int referenceIndex;
037
038    /**
039     * Initialize from another object.
040     *
041     * @param c Source to copy.
042     */
043    public ConstantMethodHandle(final ConstantMethodHandle c) {
044        this(c.getReferenceKind(), c.getReferenceIndex());
045    }
046
047    /**
048     * Initialize instance from file data.
049     *
050     * @param file Input stream.
051     * @throws IOException if an I/O error occurs.
052     */
053    ConstantMethodHandle(final DataInput file) throws IOException {
054        this(file.readUnsignedByte(), file.readUnsignedShort());
055    }
056
057    /**
058     * Constructs a ConstantMethodHandle.
059     *
060     * @param referenceKind Kind of method reference.
061     * @param referenceIndex Index to the method reference.
062     */
063    public ConstantMethodHandle(final int referenceKind, final int referenceIndex) {
064        super(Const.CONSTANT_MethodHandle);
065        this.referenceKind = referenceKind;
066        this.referenceIndex = referenceIndex;
067    }
068
069    /**
070     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
071     * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
072     *
073     * @param v Visitor object.
074     */
075    @Override
076    public void accept(final Visitor v) {
077        v.visitConstantMethodHandle(this);
078    }
079
080    /**
081     * Dumps method kind and index to file stream in binary format.
082     *
083     * @param file Output file stream.
084     * @throws IOException if an I/O error occurs.
085     */
086    @Override
087    public void dump(final DataOutputStream file) throws IOException {
088        file.writeByte(super.getTag());
089        file.writeByte(referenceKind);
090        file.writeShort(referenceIndex);
091    }
092
093    /**
094     * Gets the reference index.
095     *
096     * @return the reference index.
097     */
098    public int getReferenceIndex() {
099        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}