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 */ 18 package org.apache.bcel.classfile; 19 20 import java.io.DataInput; 21 import java.io.IOException; 22 23 import org.apache.bcel.Const; 24 25 /** 26 * This class is derived from the abstract {@link Constant} 27 * and represents a reference to a invoke dynamic. 28 * 29 * @see Constant 30 * @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.10"> 31 * The 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 */ ConstantInvokeDynamic(final ConstantInvokeDynamic c)39 public ConstantInvokeDynamic(final ConstantInvokeDynamic c) { 40 this(c.getBootstrapMethodAttrIndex(), c.getNameAndTypeIndex()); 41 } 42 43 44 /** 45 * Initialize instance from file data. 46 * 47 * @param file Input stream 48 * @throws IOException 49 */ ConstantInvokeDynamic(final DataInput file)50 ConstantInvokeDynamic(final DataInput file) throws IOException { 51 this(file.readShort(), file.readShort()); 52 } 53 54 ConstantInvokeDynamic(final int bootstrap_method_attr_index, final int name_and_type_index)55 public ConstantInvokeDynamic(final int bootstrap_method_attr_index, final int name_and_type_index) { 56 super(Const.CONSTANT_InvokeDynamic, bootstrap_method_attr_index, name_and_type_index); 57 } 58 59 60 /** 61 * Called by objects that are traversing the nodes of the tree implicitly 62 * defined by the contents of a Java class. I.e., the hierarchy of methods, 63 * fields, attributes, etc. spawns a tree of objects. 64 * 65 * @param v Visitor object 66 */ 67 @Override accept( final Visitor v )68 public void accept( final Visitor v ) { 69 v.visitConstantInvokeDynamic(this); 70 } 71 72 /** 73 * @return Reference (index) to bootstrap method this constant refers to. 74 * 75 * Note that this method is a functional duplicate of getClassIndex 76 * for use by ConstantInvokeDynamic. 77 * @since 6.0 78 */ getBootstrapMethodAttrIndex()79 public final int getBootstrapMethodAttrIndex() { 80 return super.getClassIndex(); // AKA bootstrap_method_attr_index 81 } 82 83 /** 84 * @return String representation 85 */ 86 @Override toString()87 public final String toString() { 88 return super.toString().replace("class_index", "bootstrap_method_attr_index"); 89 } 90 } 91