    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="https://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 bootstrapMethodRef;
    040
    041    /** Array of references to the constant_pool table */
    042    private int[] bootstrapArguments;
    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 < bootstrapArguments.length; i++) {
    062            bootstrapArguments[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 bootstrapMethodRef int index into constant_pool of CONSTANT_MethodHandle
    073     * @param bootstrapArguments int[] indices into constant_pool of CONSTANT_[type]_info
    074     */
    075    public BootstrapMethod(final int bootstrapMethodRef, final int[] bootstrapArguments) {
    076        this.bootstrapMethodRef = bootstrapMethodRef;
    077        this.bootstrapArguments = bootstrapArguments;
    078    }
    079
    080    /**
    081     * @return index into constant_pool of bootstrap_method
    082     */
    083    public int getBootstrapMethodRef() {
    084        return bootstrapMethodRef;
    085    }
    086
    087    /**
    088     * @param bootstrapMethodRef int index into constant_pool of CONSTANT_MethodHandle
    089     */
    090    public void setBootstrapMethodRef(final int bootstrapMethodRef) {
    091        this.bootstrapMethodRef = bootstrapMethodRef;
    092    }
    093
    094    /**
    095     * @return int[] of bootstrap_method indices into constant_pool of CONSTANT_[type]_info
    096     */
    097    public int[] getBootstrapArguments() {
    098        return bootstrapArguments;
    099    }
    100
    101    /**
    102     * @return count of number of boostrap arguments
    103     */
    104    public int getNumBootstrapArguments() {
    105        return bootstrapArguments.length;
    106    }
    107
    108    /**
    109     * @param bootstrapArguments int[] indices into constant_pool of CONSTANT_[type]_info
    110     */
    111    public void setBootstrapArguments(final int[] bootstrapArguments) {
    112        this.bootstrapArguments = bootstrapArguments;
    113    }
    114
    115    /**
    116     * @return String representation.
    117     */
    118    @Override
    119    public final String toString() {
    120        return "BootstrapMethod(" + bootstrapMethodRef + ", " + bootstrapArguments.length + ", "
    121               + Arrays.toString(bootstrapArguments) + ")";
    122    }
    123
    124    /**
    125     * @return Resolved string representation
    126     */
    127    public final String toString( final ConstantPool constantPool ) {
    128        final StringBuilder buf = new StringBuilder();
    129        String bootstrap_method_name;
    130        bootstrap_method_name = constantPool.constantToString(bootstrapMethodRef,
    131                Const.CONSTANT_MethodHandle);
    132        buf.append(Utility.compactClassName(bootstrap_method_name, false));
    133        final int num_bootstrap_arguments = bootstrapArguments.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(constantPool.constantToString(constantPool.getConstant(bootstrapArguments[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(bootstrapMethodRef);
    152        file.writeShort(bootstrapArguments.length);
    153        for (final int bootstrap_argument : bootstrapArguments) {
    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}