    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 dynamically computed
    028 * constant.
    029 *
    030 * @see Constant
    031 * @see <a href="https://bugs.openjdk.java.net/secure/attachment/74618/constant-dynamic.html"> Change request for JEP
    032 *      309</a>
    033 * @since 6.3
    034 */
    035public final class ConstantDynamic extends ConstantCP {
    036
    037    /**
    038     * Initialize from another object.
    039     *
    040     * @param c Source to copy.
    041     */
    042    public ConstantDynamic(final ConstantDynamic c) {
    043        this(c.getBootstrapMethodAttrIndex(), c.getNameAndTypeIndex());
    044    }
    045
    046    /**
    047     * Initialize instance from file data.
    048     *
    049     * @param file Input stream.
    050     * @throws IOException if an I/O error occurs.
    051     */
    052    ConstantDynamic(final DataInput file) throws IOException {
    053        this(file.readShort(), file.readShort());
    054    }
    055
    056    /**
    057     * Constructs a ConstantDynamic.
    058     *
    059     * @param bootstrapMethodAttrIndex Index to the bootstrap method.
    060     * @param nameAndTypeIndex Index to the name and type.
    061     */
    062    public ConstantDynamic(final int bootstrapMethodAttrIndex, final int nameAndTypeIndex) {
    063        super(Const.CONSTANT_Dynamic, bootstrapMethodAttrIndex, nameAndTypeIndex);
    064    }
    065
    066    /**
    067     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
    068     * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    069     *
    070     * @param v Visitor object.
    071     */
    072    @Override
    073    public void accept(final Visitor v) {
    074        v.visitConstantDynamic(this);
    075    }
    076
    077    /**
    078     * Gets the reference (index) to bootstrap method this constant refers to.
    079     *
    080     * @return Reference (index) to bootstrap method this constant refers to.
    081     *
    082     *         Note that this method is a functional duplicate of getClassIndex for use by ConstantInvokeDynamic.
    083     * @since 6.0
    084     */
    085    public int getBootstrapMethodAttrIndex() {
    086        return super.getClassIndex(); // AKA bootstrap_method_attr_index
    087    }
    088
    089    /**
    090     * @return String representation.
    091     */
    092    @Override
    093    public String toString() {
    094        return super.toString().replace("class_index", "bootstrap_method_attr_index");
    095    }
    096}