View Javadoc
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  
19  import java.io.DataInput;
20  import java.io.IOException;
21  
22  import org.apache.bcel.Const;
23  
24  /**
25   * This class is derived from the abstract {@link Constant} and represents a reference to a dynamically computed
26   * constant.
27   *
28   * @see Constant
29   * @see <a href="https://bugs.openjdk.java.net/secure/attachment/74618/constant-dynamic.html"> Change request for JEP
30   *      309</a>
31   * @since 6.3
32   */
33  public final class ConstantDynamic extends ConstantCP {
34  
35      /**
36       * Initialize from another object.
37       *
38       * @param c Source to copy.
39       */
40      public ConstantDynamic(final ConstantDynamic c) {
41          this(c.getBootstrapMethodAttrIndex(), c.getNameAndTypeIndex());
42      }
43  
44      /**
45       * Initialize instance from file data.
46       *
47       * @param file Input stream
48       * @throws IOException if an I/O error occurs.
49       */
50      ConstantDynamic(final DataInput file) throws IOException {
51          this(file.readShort(), file.readShort());
52      }
53  
54      public ConstantDynamic(final int bootstrapMethodAttrIndex, final int nameAndTypeIndex) {
55          super(Const.CONSTANT_Dynamic, bootstrapMethodAttrIndex, nameAndTypeIndex);
56      }
57  
58      /**
59       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
60       * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
61       *
62       * @param v Visitor object
63       */
64      @Override
65      public void accept(final Visitor v) {
66          v.visitConstantDynamic(this);
67      }
68  
69      /**
70       * @return Reference (index) to bootstrap method this constant refers to.
71       *
72       *         Note that this method is a functional duplicate of getClassIndex for use by ConstantInvokeDynamic.
73       * @since 6.0
74       */
75      public int getBootstrapMethodAttrIndex() {
76          return super.getClassIndex(); // AKA bootstrap_method_attr_index
77      }
78  
79      /**
80       * @return String representation
81       */
82      @Override
83      public String toString() {
84          return super.toString().replace("class_index", "bootstrap_method_attr_index");
85      }
86  }