All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feilong.lib.javassist.bytecode.BootstrapMethodsAttribute Maven / Gradle / Ivy

Go to download

feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.

There is a newer version: 4.3.0
Show newest version
package com.feilong.lib.javassist.bytecode;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Map;

public class BootstrapMethodsAttribute extends AttributeInfo{

    /**
     * The name of this attribute "BootstrapMethods".
     */
    public static final String tag = "BootstrapMethods";

    /**
     * An element of bootstrap_methods.
     */
    public static class BootstrapMethod{

        /**
         * Constructs an element of bootstrap_methods.
         *
         * @param method
         *            bootstrap_method_ref.
         * @param args
         *            bootstrap_arguments.
         */
        public BootstrapMethod(int method, int[] args){
            methodRef = method;
            arguments = args;
        }

        /**
         * bootstrap_method_ref.
         * The value at this index must be a CONSTANT_MethodHandle_info.
         */
        public int   methodRef;

        /**
         * bootstrap_arguments.
         */
        public int[] arguments;
    }

    BootstrapMethodsAttribute(ConstPool cp, int n, DataInputStream in) throws IOException{
        super(cp, n, in);
    }

    /**
     * Constructs a BootstrapMethods attribute.
     *
     * @param cp
     *            a constant pool table.
     * @param methods
     *            the contents.
     */
    public BootstrapMethodsAttribute(ConstPool cp, BootstrapMethod[] methods){
        super(cp, tag);
        int size = 2;
        for (BootstrapMethod method : methods){
            size += 4 + method.arguments.length * 2;
        }

        byte[] data = new byte[size];
        ByteArray.write16bit(methods.length, data, 0); // num_bootstrap_methods
        int pos = 2;
        for (BootstrapMethod method : methods){
            ByteArray.write16bit(method.methodRef, data, pos);
            ByteArray.write16bit(method.arguments.length, data, pos + 2);
            int[] args = method.arguments;
            pos += 4;
            for (int arg : args){
                ByteArray.write16bit(arg, data, pos);
                pos += 2;
            }
        }

        set(data);
    }

    /**
     * Obtains bootstrap_methods in this attribute.
     *
     * @return an array of BootstrapMethod. Since it
     *         is a fresh copy, modifying the returned array does not
     *         affect the original contents of this attribute.
     */
    public BootstrapMethod[] getMethods(){
        byte[] data = this.get();
        int num = ByteArray.readU16bit(data, 0);
        BootstrapMethod[] methods = new BootstrapMethod[num];
        int pos = 2;
        for (int i = 0; i < num; i++){
            int ref = ByteArray.readU16bit(data, pos);
            int len = ByteArray.readU16bit(data, pos + 2);
            int[] args = new int[len];
            pos += 4;
            for (int k = 0; k < len; k++){
                args[k] = ByteArray.readU16bit(data, pos);
                pos += 2;
            }

            methods[i] = new BootstrapMethod(ref, args);
        }

        return methods;
    }

    /**
     * Makes a copy. Class names are replaced according to the
     * given Map object.
     *
     * @param newCp
     *            the constant pool table used by the new copy.
     * @param classnames
     *            pairs of replaced and substituted
     *            class names.
     */
    @Override
    public AttributeInfo copy(ConstPool newCp,Map classnames){
        BootstrapMethod[] methods = getMethods();
        ConstPool thisCp = getConstPool();
        for (BootstrapMethod method : methods){
            BootstrapMethod m = method;
            m.methodRef = thisCp.copy(m.methodRef, newCp, classnames);
            for (int k = 0; k < m.arguments.length; k++){
                m.arguments[k] = thisCp.copy(m.arguments[k], newCp, classnames);
            }
        }

        return new BootstrapMethodsAttribute(newCp, methods);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy