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    public ConstantInvokeDynamic(final int bootstrapMethodAttrIndex, final int nameAndTypeIndex) {
056        super(Const.CONSTANT_InvokeDynamic, bootstrapMethodAttrIndex, nameAndTypeIndex);
057    }
058
059    /**
060     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
061     * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
062     *
063     * @param v Visitor object
064     */
065    @Override
066    public void accept(final Visitor v) {
067        v.visitConstantInvokeDynamic(this);
068    }
069
070    /**
071     * @return Reference (index) to bootstrap method this constant refers to.
072     *
073     *         Note that this method is a functional duplicate of getClassIndex for use by ConstantInvokeDynamic.
074     * @since 6.0
075     */
076    public int getBootstrapMethodAttrIndex() {
077        return super.getClassIndex(); // AKA bootstrap_method_attr_index
078    }
079
080    /**
081     * @return String representation
082     */
083    @Override
084    public String toString() {
085        return super.toString().replace("class_index", "bootstrap_method_attr_index");
086    }
087}