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