    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017
    018package org.apache.bcel.classfile;
    019
    020import java.io.DataInput;
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023import java.util.Arrays;
    024
    025import org.apache.bcel.Const;
    026
    027/**
    028 * This class represents a bootstrap method attribute, i.e., the bootstrap
    029 * method ref, the number of bootstrap arguments and an array of the
    030 * bootstrap arguments.
    031 *
    032 * @see <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.23">
    033 * The class File Format : The BootstrapMethods Attribute</a>
    034 * @since 6.0
    035 */
    036public class BootstrapMethod implements Cloneable {
    037
    038    /** Index of the CONSTANT_MethodHandle_info structure in the constant_pool table */
    039    private int bootstrap_method_ref;
    040
    041    /** Array of references to the constant_pool table */
    042    private int[] bootstrap_arguments;
    043
    044
    045    /**
    046     * Initialize from another object.
    047     */
    048    public BootstrapMethod(final BootstrapMethod c) {
    049        this(c.getBootstrapMethodRef(), c.getBootstrapArguments());
    050    }
    051
    052    /**
    053     * Construct object from input stream.
    054     *
    055     * @param input Input stream
    056     * @throws IOException
    057     */
    058    BootstrapMethod(final DataInput input) throws IOException {
    059        this(input.readUnsignedShort(), input.readUnsignedShort());
    060
    061        for (int i = 0; i < bootstrap_arguments.length; i++) {
    062            bootstrap_arguments[i] = input.readUnsignedShort();
    063        }
    064    }
    065
    066    // helper method
    067    private BootstrapMethod(final int bootstrap_method_ref, final int num_bootstrap_arguments) {
    068        this(bootstrap_method_ref, new int[num_bootstrap_arguments]);
    069    }
    070
    071    /**
    072     * @param bootstrap_method_ref int index into constant_pool of CONSTANT_MethodHandle
    073     * @param bootstrap_arguments int[] indices into constant_pool of CONSTANT_[type]_info
    074     */
    075    public BootstrapMethod(final int bootstrap_method_ref, final int[] bootstrap_arguments) {
    076        this.bootstrap_method_ref = bootstrap_method_ref;
    077        this.bootstrap_arguments = bootstrap_arguments;
    078    }
    079
    080    /**
    081     * @return index into constant_pool of bootstrap_method
    082     */
    083    public int getBootstrapMethodRef() {
    084        return bootstrap_method_ref;
    085    }
    086
    087    /**
    088     * @param bootstrap_method_ref int index into constant_pool of CONSTANT_MethodHandle
    089     */
    090    public void setBootstrapMethodRef(final int bootstrap_method_ref) {
    091        this.bootstrap_method_ref = bootstrap_method_ref;
    092    }
    093
    094    /**
    095     * @return int[] of bootstrap_method indices into constant_pool of CONSTANT_[type]_info
    096     */
    097    public int[] getBootstrapArguments() {
    098        return bootstrap_arguments;
    099    }
    100
    101    /**
    102     * @return count of number of boostrap arguments
    103     */
    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     */
    111    public void setBootstrapArguments(final int[] bootstrap_arguments) {
    112        this.bootstrap_arguments = bootstrap_arguments;
    113    }
    114
    115    /**
    116     * @return String representation.
    117     */
    118    @Override
    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     */
    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, false));
    133        final int num_bootstrap_arguments = bootstrap_arguments.length;
    134        if (num_bootstrap_arguments > 0) {
    135            buf.append("\nMethod 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     */
    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     */
    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}