Source code

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.classfile;
    019
    020import java.io.DataInput;
    021import java.io.IOException;
    022
    023import org.apache.bcel.Const;
    024
    025/**
    026 * This class is derived from the abstract {@link Constant}
    027 * and represents a reference to a dynamically computed constant.
    028 *
    029 * @see     Constant
    030 * @see  <a href="https://bugs.openjdk.java.net/secure/attachment/74618/constant-dynamic.html">
    031 * Change request for JEP 309</a>
    032 * @since 6.3
    033 */
    034public final class ConstantDynamic extends ConstantCP {
    035
    036    /**
    037     * Initialize from another object.
    038     */
    039    public ConstantDynamic(final ConstantDynamic c) {
    040        this(c.getBootstrapMethodAttrIndex(), c.getNameAndTypeIndex());
    041    }
    042
    043
    044    /**
    045     * Initialize instance from file data.
    046     *
    047     * @param file Input stream
    048     * @throws IOException
    049     */
    050    ConstantDynamic(final DataInput file) throws IOException {
    051        this(file.readShort(), file.readShort());
    052    }
    053
    054
    055    public ConstantDynamic(final int bootstrap_method_attr_index, final int name_and_type_index) {
    056        super(Const.CONSTANT_Dynamic, bootstrap_method_attr_index, name_and_type_index);
    057    }
    058
    059
    060    /**
    061     * Called by objects that are traversing the nodes of the tree implicitly
    062     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    063     * fields, attributes, etc. spawns a tree of objects.
    064     *
    065     * @param v Visitor object
    066     */
    067    @Override
    068    public void accept( final Visitor v ) {
    069        v.visitConstantDynamic(this);
    070    }
    071
    072    /**
    073     * @return Reference (index) to bootstrap method this constant refers to.
    074     *
    075     * Note that this method is a functional duplicate of getClassIndex
    076     * for use by ConstantInvokeDynamic.
    077     * @since 6.0
    078     */
    079    public int getBootstrapMethodAttrIndex() {
    080        return super.getClassIndex();  // AKA bootstrap_method_attr_index
    081    }
    082
    083    /**
    084     * @return String representation
    085     */
    086    @Override
    087    public String toString() {
    088        return super.toString().replace("class_index", "bootstrap_method_attr_index");
    089    }
    090}