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