    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.IOException;
    023
    024import org.apache.bcel.Const;
    025
    026/**
    027 * This class is derived from the abstract {@link Constant} and represents a reference to a invoke dynamic.
    028 *
    029 * @see Constant
    030 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.10"> The
    031 *      CONSTANT_InvokeDynamic_info Structure in The Java Virtual Machine Specification</a>
    032 * @since 6.0
    033 */
    034public final class ConstantInvokeDynamic extends ConstantCP {
    035
    036    /**
    037     * Initialize from another object.
    038     *
    039     * @param c Source to copy.
    040     */
    041    public ConstantInvokeDynamic(final ConstantInvokeDynamic c) {
    042        this(c.getBootstrapMethodAttrIndex(), c.getNameAndTypeIndex());
    043    }
    044
    045    /**
    046     * Initialize instance from file data.
    047     *
    048     * @param file Input stream.
    049     * @throws IOException if an I/O error occurs.
    050     */
    051    ConstantInvokeDynamic(final DataInput file) throws IOException {
    052        this(file.readUnsignedShort(), file.readUnsignedShort());
    053    }
    054
    055    /**
    056     * Constructs a ConstantInvokeDynamic.
    057     *
    058     * @param bootstrapMethodAttrIndex Index to the bootstrap method.
    059     * @param nameAndTypeIndex Index to the name and type.
    060     */
    061    public ConstantInvokeDynamic(final int bootstrapMethodAttrIndex, final int nameAndTypeIndex) {
    062        super(Const.CONSTANT_InvokeDynamic, bootstrapMethodAttrIndex, nameAndTypeIndex);
    063    }
    064
    065    /**
    066     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
    067     * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    068     *
    069     * @param v Visitor object.
    070     */
    071    @Override
    072    public void accept(final Visitor v) {
    073        v.visitConstantInvokeDynamic(this);
    074    }
    075
    076    /**
    077     * Gets the reference (index) to bootstrap method this constant refers to.
    078     *
    079     * @return Reference (index) to bootstrap method this constant refers to.
    080     *
    081     *         Note that this method is a functional duplicate of getClassIndex for use by ConstantInvokeDynamic.
    082     * @since 6.0
    083     */
    084    public int getBootstrapMethodAttrIndex() {
    085        return super.getClassIndex(); // AKA bootstrap_method_attr_index
    086    }
    087
    088    /**
    089     * @return String representation.
    090     */
    091    @Override
    092    public String toString() {
    093        return super.toString().replace("class_index", "bootstrap_method_attr_index");
    094    }
    095}