1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 import java.io.DataInput; 25 import java.io.DataOutputStream; 26 import java.io.IOException; 27 28 import com.sun.org.apache.bcel.internal.Const; 29 30 /** 31 * This class represents a BootstrapMethods attribute. 32 * 33 * @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.23"> 34 * The class File Format : The BootstrapMethods Attribute</a> 35 * @since 6.0 36 */ 37 public class BootstrapMethods extends Attribute { 38 39 private BootstrapMethod[] bootstrap_methods; // TODO this could be made final (setter is not used) 40 41 /** 42 * Initialize from another object. Note that both objects use the same 43 * references (shallow copy). Use clone() for a physical copy. 44 */ BootstrapMethods(final BootstrapMethods c)45 public BootstrapMethods(final BootstrapMethods c) { 46 this(c.getNameIndex(), c.getLength(), c.getBootstrapMethods(), c.getConstantPool()); 47 } 48 49 50 /** 51 * @param name_index Index in constant pool to CONSTANT_Utf8 52 * @param length Content length in bytes 53 * @param bootstrap_methods array of bootstrap methods 54 * @param constant_pool Array of constants 55 */ BootstrapMethods(final int name_index, final int length, final BootstrapMethod[] bootstrap_methods, final ConstantPool constant_pool)56 public BootstrapMethods(final int name_index, final int length, final BootstrapMethod[] bootstrap_methods, final ConstantPool constant_pool) { 57 super(Const.ATTR_BOOTSTRAP_METHODS, name_index, length, constant_pool); 58 this.bootstrap_methods = bootstrap_methods; 59 } 60 61 /** 62 * Construct object from Input stream. 63 * 64 * @param name_index Index in constant pool to CONSTANT_Utf8 65 * @param length Content length in bytes 66 * @param input Input stream 67 * @param constant_pool Array of constants 68 * @throws IOException 69 */ BootstrapMethods(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)70 BootstrapMethods(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException { 71 this(name_index, length, (BootstrapMethod[]) null, constant_pool); 72 73 final int num_bootstrap_methods = input.readUnsignedShort(); 74 bootstrap_methods = new BootstrapMethod[num_bootstrap_methods]; 75 for (int i = 0; i < num_bootstrap_methods; i++) { 76 bootstrap_methods[i] = new BootstrapMethod(input); 77 } 78 } 79 80 /** 81 * @return array of bootstrap method "records" 82 */ getBootstrapMethods()83 public final BootstrapMethod[] getBootstrapMethods() { 84 return bootstrap_methods; 85 } 86 87 /** 88 * @param bootstrap_methods the array of bootstrap methods 89 */ setBootstrapMethods(final BootstrapMethod[] bootstrap_methods)90 public final void setBootstrapMethods(final BootstrapMethod[] bootstrap_methods) { 91 this.bootstrap_methods = bootstrap_methods; 92 } 93 94 /** 95 * @param v Visitor object 96 */ 97 @Override accept(final Visitor v)98 public void accept(final Visitor v) { 99 v.visitBootstrapMethods(this); 100 } 101 102 /** 103 * @return deep copy of this attribute 104 */ 105 @Override copy(final ConstantPool _constant_pool)106 public BootstrapMethods copy(final ConstantPool _constant_pool) { 107 final BootstrapMethods c = (BootstrapMethods) clone(); 108 c.bootstrap_methods = new BootstrapMethod[bootstrap_methods.length]; 109 110 for (int i = 0; i < bootstrap_methods.length; i++) { 111 c.bootstrap_methods[i] = bootstrap_methods[i].copy(); 112 } 113 c.setConstantPool(_constant_pool); 114 return c; 115 } 116 117 /** 118 * Dump bootstrap methods attribute to file stream in binary format. 119 * 120 * @param file Output file stream 121 * @throws IOException 122 */ 123 @Override dump(final DataOutputStream file)124 public final void dump(final DataOutputStream file) throws IOException { 125 super.dump(file); 126 127 file.writeShort(bootstrap_methods.length); 128 for (final BootstrapMethod bootstrap_method : bootstrap_methods) { 129 bootstrap_method.dump(file); 130 } 131 } 132 133 /** 134 * @return String representation. 135 */ 136 @Override toString()137 public final String toString() { 138 final StringBuilder buf = new StringBuilder(); 139 buf.append("BootstrapMethods("); 140 buf.append(bootstrap_methods.length); 141 buf.append("):\n"); 142 for (int i = 0; i < bootstrap_methods.length; i++) { 143 buf.append(" ").append(i).append(": "); 144 buf.append(bootstrap_methods[i].toString(super.getConstantPool())).append("\n"); 145 } 146 return buf.toString(); 147 } 148 } 149