View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataInput;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class is derived from the abstract {@link Constant} and represents a reference to a invoke dynamic.
28   *
29   * @see Constant
30   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.10"> The
31   *      CONSTANT_InvokeDynamic_info Structure in The Java Virtual Machine Specification</a>
32   * @since 6.0
33   */
34  public final class ConstantInvokeDynamic extends ConstantCP {
35  
36      /**
37       * Initialize from another object.
38       *
39       * @param c Source to copy.
40       */
41      public ConstantInvokeDynamic(final ConstantInvokeDynamic c) {
42          this(c.getBootstrapMethodAttrIndex(), c.getNameAndTypeIndex());
43      }
44  
45      /**
46       * Initialize instance from file data.
47       *
48       * @param file Input stream
49       * @throws IOException if an I/O error occurs.
50       */
51      ConstantInvokeDynamic(final DataInput file) throws IOException {
52          this(file.readUnsignedShort(), file.readUnsignedShort());
53      }
54  
55      public ConstantInvokeDynamic(final int bootstrapMethodAttrIndex, final int nameAndTypeIndex) {
56          super(Const.CONSTANT_InvokeDynamic, bootstrapMethodAttrIndex, nameAndTypeIndex);
57      }
58  
59      /**
60       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
61       * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
62       *
63       * @param v Visitor object
64       */
65      @Override
66      public void accept(final Visitor v) {
67          v.visitConstantInvokeDynamic(this);
68      }
69  
70      /**
71       * @return Reference (index) to bootstrap method this constant refers to.
72       *
73       *         Note that this method is a functional duplicate of getClassIndex for use by ConstantInvokeDynamic.
74       * @since 6.0
75       */
76      public int getBootstrapMethodAttrIndex() {
77          return super.getClassIndex(); // AKA bootstrap_method_attr_index
78      }
79  
80      /**
81       * @return String representation
82       */
83      @Override
84      public String toString() {
85          return super.toString().replace("class_index", "bootstrap_method_attr_index");
86      }
87  }