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.DataOutputStream; 22 import java.io.IOException; 23 import java.util.Arrays; 24 25 import org.apache.bcel.Const; 26 27 /** 28 * This class represents a bootstrap method attribute, i.e., the bootstrap 29 * method ref, the number of bootstrap arguments and an array of the 30 * bootstrap arguments. 31 * 32 * @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.23"> 33 * The class File Format : The BootstrapMethods Attribute</a> 34 * @since 6.0 35 */ 36 public class BootstrapMethod implements Cloneable { 37 38 /** Index of the CONSTANT_MethodHandle_info structure in the constant_pool table */ 39 private int bootstrap_method_ref; 40 41 /** Array of references to the constant_pool table */ 42 private int[] bootstrap_arguments; 43 44 45 /** 46 * Initialize from another object. 47 */ BootstrapMethod(final BootstrapMethod c)48 public BootstrapMethod(final BootstrapMethod c) { 49 this(c.getBootstrapMethodRef(), c.getBootstrapArguments()); 50 } 51 52 /** 53 * Construct object from input stream. 54 * 55 * @param input Input stream 56 * @throws IOException 57 */ BootstrapMethod(final DataInput input)58 BootstrapMethod(final DataInput input) throws IOException { 59 this(input.readUnsignedShort(), input.readUnsignedShort()); 60 61 for (int i = 0; i < bootstrap_arguments.length; i++) { 62 bootstrap_arguments[i] = input.readUnsignedShort(); 63 } 64 } 65 66 // helper method BootstrapMethod(final int bootstrap_method_ref, final int num_bootstrap_arguments)67 private BootstrapMethod(final int bootstrap_method_ref, final int num_bootstrap_arguments) { 68 this(bootstrap_method_ref, new int[num_bootstrap_arguments]); 69 } 70 71 /** 72 * @param bootstrap_method_ref int index into constant_pool of CONSTANT_MethodHandle 73 * @param bootstrap_arguments int[] indices into constant_pool of CONSTANT_<type>_info 74 */ BootstrapMethod(final int bootstrap_method_ref, final int[] bootstrap_arguments)75 public BootstrapMethod(final int bootstrap_method_ref, final int[] bootstrap_arguments) { 76 this.bootstrap_method_ref = bootstrap_method_ref; 77 this.bootstrap_arguments = bootstrap_arguments; 78 } 79 80 /** 81 * @return index into constant_pool of bootstrap_method 82 */ getBootstrapMethodRef()83 public int getBootstrapMethodRef() { 84 return bootstrap_method_ref; 85 } 86 87 /** 88 * @param bootstrap_method_ref int index into constant_pool of CONSTANT_MethodHandle 89 */ setBootstrapMethodRef(final int bootstrap_method_ref)90 public void setBootstrapMethodRef(final int bootstrap_method_ref) { 91 this.bootstrap_method_ref = bootstrap_method_ref; 92 } 93 94 /** 95 * @return int[] of bootstrap_method indices into constant_pool of CONSTANT_<type>_info 96 */ getBootstrapArguments()97 public int[] getBootstrapArguments() { 98 return bootstrap_arguments; 99 } 100 101 /** 102 * @return count of number of boostrap arguments 103 */ getNumBootstrapArguments()104 public int getNumBootstrapArguments() { 105 return bootstrap_arguments.length; 106 } 107 108 /** 109 * @param bootstrap_arguments int[] indices into constant_pool of CONSTANT_<type>_info 110 */ setBootstrapArguments(final int[] bootstrap_arguments)111 public void setBootstrapArguments(final int[] bootstrap_arguments) { 112 this.bootstrap_arguments = bootstrap_arguments; 113 } 114 115 /** 116 * @return String representation. 117 */ 118 @Override toString()119 public final String toString() { 120 return "BootstrapMethod(" + bootstrap_method_ref + ", " + bootstrap_arguments.length + ", " 121 + Arrays.toString(bootstrap_arguments) + ")"; 122 } 123 124 /** 125 * @return Resolved string representation 126 */ toString( final ConstantPool constant_pool )127 public final String toString( final ConstantPool constant_pool ) { 128 final StringBuilder buf = new StringBuilder(); 129 String bootstrap_method_name; 130 bootstrap_method_name = constant_pool.constantToString(bootstrap_method_ref, 131 Const.CONSTANT_MethodHandle); 132 buf.append(Utility.compactClassName(bootstrap_method_name)); 133 final int num_bootstrap_arguments = bootstrap_arguments.length; 134 if (num_bootstrap_arguments > 0) { 135 buf.append("\n Method Arguments:"); 136 for (int i = 0; i < num_bootstrap_arguments; i++) { 137 buf.append("\n ").append(i).append(": "); 138 buf.append(constant_pool.constantToString(constant_pool.getConstant(bootstrap_arguments[i]))); 139 } 140 } 141 return buf.toString(); 142 } 143 144 /** 145 * Dump object to file stream in binary format. 146 * 147 * @param file Output file stream 148 * @throws IOException 149 */ dump(final DataOutputStream file)150 public final void dump(final DataOutputStream file) throws IOException { 151 file.writeShort(bootstrap_method_ref); 152 file.writeShort(bootstrap_arguments.length); 153 for (final int bootstrap_argument : bootstrap_arguments) { 154 file.writeShort(bootstrap_argument); 155 } 156 } 157 158 /** 159 * @return deep copy of this object 160 */ copy()161 public BootstrapMethod copy() { 162 try { 163 return (BootstrapMethod) clone(); 164 } catch (final CloneNotSupportedException e) { 165 // TODO should this throw? 166 } 167 return null; 168 } 169 } 170