ConstantDynamic.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.IOException;

  20. import org.apache.bcel.Const;

  21. /**
  22.  * This class is derived from the abstract {@link Constant} and represents a reference to a dynamically computed
  23.  * constant.
  24.  *
  25.  * @see Constant
  26.  * @see <a href="https://bugs.openjdk.java.net/secure/attachment/74618/constant-dynamic.html"> Change request for JEP
  27.  *      309</a>
  28.  * @since 6.3
  29.  */
  30. public final class ConstantDynamic extends ConstantCP {

  31.     /**
  32.      * Initialize from another object.
  33.      *
  34.      * @param c Source to copy.
  35.      */
  36.     public ConstantDynamic(final ConstantDynamic c) {
  37.         this(c.getBootstrapMethodAttrIndex(), c.getNameAndTypeIndex());
  38.     }

  39.     /**
  40.      * Initialize instance from file data.
  41.      *
  42.      * @param file Input stream
  43.      * @throws IOException if an I/O error occurs.
  44.      */
  45.     ConstantDynamic(final DataInput file) throws IOException {
  46.         this(file.readShort(), file.readShort());
  47.     }

  48.     public ConstantDynamic(final int bootstrapMethodAttrIndex, final int nameAndTypeIndex) {
  49.         super(Const.CONSTANT_Dynamic, bootstrapMethodAttrIndex, nameAndTypeIndex);
  50.     }

  51.     /**
  52.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
  53.      * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  54.      *
  55.      * @param v Visitor object
  56.      */
  57.     @Override
  58.     public void accept(final Visitor v) {
  59.         v.visitConstantDynamic(this);
  60.     }

  61.     /**
  62.      * @return Reference (index) to bootstrap method this constant refers to.
  63.      *
  64.      *         Note that this method is a functional duplicate of getClassIndex for use by ConstantInvokeDynamic.
  65.      * @since 6.0
  66.      */
  67.     public int getBootstrapMethodAttrIndex() {
  68.         return super.getClassIndex(); // AKA bootstrap_method_attr_index
  69.     }

  70.     /**
  71.      * @return String representation
  72.      */
  73.     @Override
  74.     public String toString() {
  75.         return super.toString().replace("class_index", "bootstrap_method_attr_index");
  76.     }
  77. }