Home | History | Annotate | Download | only in classfile
      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 
     24 import org.apache.bcel.Const;
     25 
     26 /**
     27  * This class represents a BootstrapMethods attribute.
     28  *
     29  * @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.23">
     30  * The class File Format : The BootstrapMethods Attribute</a>
     31  * @since 6.0
     32  */
     33 public class BootstrapMethods extends Attribute {
     34 
     35     private BootstrapMethod[] bootstrap_methods;  // TODO this could be made final (setter is not used)
     36 
     37     /**
     38      * Initialize from another object. Note that both objects use the same
     39      * references (shallow copy). Use clone() for a physical copy.
     40      */
     41     public BootstrapMethods(final BootstrapMethods c) {
     42         this(c.getNameIndex(), c.getLength(), c.getBootstrapMethods(), c.getConstantPool());
     43     }
     44 
     45 
     46     /**
     47      * @param name_index Index in constant pool to CONSTANT_Utf8
     48      * @param length Content length in bytes
     49      * @param bootstrap_methods array of bootstrap methods
     50      * @param constant_pool Array of constants
     51      */
     52     public BootstrapMethods(final int name_index, final int length, final BootstrapMethod[] bootstrap_methods, final ConstantPool constant_pool) {
     53         super(Const.ATTR_BOOTSTRAP_METHODS, name_index, length, constant_pool);
     54         this.bootstrap_methods = bootstrap_methods;
     55     }
     56 
     57     /**
     58      * Construct object from Input stream.
     59      *
     60      * @param name_index Index in constant pool to CONSTANT_Utf8
     61      * @param length Content length in bytes
     62      * @param input Input stream
     63      * @param constant_pool Array of constants
     64      * @throws IOException
     65      */
     66     BootstrapMethods(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
     67         this(name_index, length, (BootstrapMethod[]) null, constant_pool);
     68 
     69         final int num_bootstrap_methods = input.readUnsignedShort();
     70         bootstrap_methods = new BootstrapMethod[num_bootstrap_methods];
     71         for (int i = 0; i < num_bootstrap_methods; i++) {
     72             bootstrap_methods[i] = new BootstrapMethod(input);
     73         }
     74     }
     75 
     76     /**
     77      * @return array of bootstrap method "records"
     78      */
     79     public final BootstrapMethod[] getBootstrapMethods() {
     80         return bootstrap_methods;
     81     }
     82 
     83     /**
     84      * @param bootstrap_methods the array of bootstrap methods
     85      */
     86     public final void setBootstrapMethods(final BootstrapMethod[] bootstrap_methods) {
     87         this.bootstrap_methods = bootstrap_methods;
     88     }
     89 
     90     /**
     91      * @param v Visitor object
     92      */
     93     @Override
     94     public void accept(final Visitor v) {
     95         v.visitBootstrapMethods(this);
     96     }
     97 
     98     /**
     99      * @return deep copy of this attribute
    100      */
    101     @Override
    102     public BootstrapMethods copy(final ConstantPool _constant_pool) {
    103         final BootstrapMethods c = (BootstrapMethods) clone();
    104         c.bootstrap_methods = new BootstrapMethod[bootstrap_methods.length];
    105 
    106         for (int i = 0; i < bootstrap_methods.length; i++) {
    107             c.bootstrap_methods[i] = bootstrap_methods[i].copy();
    108         }
    109         c.setConstantPool(_constant_pool);
    110         return c;
    111     }
    112 
    113     /**
    114      * Dump bootstrap methods attribute to file stream in binary format.
    115      *
    116      * @param file Output file stream
    117      * @throws IOException
    118      */
    119     @Override
    120     public final void dump(final DataOutputStream file) throws IOException {
    121         super.dump(file);
    122 
    123         file.writeShort(bootstrap_methods.length);
    124         for (final BootstrapMethod bootstrap_method : bootstrap_methods) {
    125             bootstrap_method.dump(file);
    126         }
    127     }
    128 
    129     /**
    130      * @return String representation.
    131      */
    132     @Override
    133     public final String toString() {
    134         final StringBuilder buf = new StringBuilder();
    135         buf.append("BootstrapMethods(");
    136         buf.append(bootstrap_methods.length);
    137         buf.append("):\n");
    138         for (int i = 0; i < bootstrap_methods.length; i++) {
    139             buf.append("  ").append(i).append(": ");
    140             buf.append(bootstrap_methods[i].toString(super.getConstantPool())).append("\n");
    141         }
    142         return buf.toString();
    143     }
    144 }
    145