⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classwriter.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/***
 * ASM: a very small and fast Java bytecode manipulation framework
 * Copyright (c) 2000-2005 INRIA, France Telecom
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.drools.asm;

/**
 * A {@link ClassVisitor} that generates classes in bytecode form. More
 * precisely this visitor generates a byte array conforming to the Java class
 * file format. It can be used alone, to generate a Java class "from scratch",
 * or with one or more {@link ClassReader ClassReader} and adapter class visitor
 * to generate a modified class from one or more existing Java classes.
 * 
 * @author Eric Bruneton
 */
public class ClassWriter
    implements
    ClassVisitor {

    /**
     * The type of instructions without any argument.
     */
    final static int         NOARG_INSN       = 0;

    /**
     * The type of instructions with an signed byte argument.
     */
    final static int         SBYTE_INSN       = 1;

    /**
     * The type of instructions with an signed short argument.
     */
    final static int         SHORT_INSN       = 2;

    /**
     * The type of instructions with a local variable index argument.
     */
    final static int         VAR_INSN         = 3;

    /**
     * The type of instructions with an implicit local variable index argument.
     */
    final static int         IMPLVAR_INSN     = 4;

    /**
     * The type of instructions with a type descriptor argument.
     */
    final static int         TYPE_INSN        = 5;

    /**
     * The type of field and method invocations instructions.
     */
    final static int         FIELDORMETH_INSN = 6;

    /**
     * The type of the INVOKEINTERFACE instruction.
     */
    final static int         ITFMETH_INSN     = 7;

    /**
     * The type of instructions with a 2 bytes bytecode offset label.
     */
    final static int         LABEL_INSN       = 8;

    /**
     * The type of instructions with a 4 bytes bytecode offset label.
     */
    final static int         LABELW_INSN      = 9;

    /**
     * The type of the LDC instruction.
     */
    final static int         LDC_INSN         = 10;

    /**
     * The type of the LDC_W and LDC2_W instructions.
     */
    final static int         LDCW_INSN        = 11;

    /**
     * The type of the IINC instruction.
     */
    final static int         IINC_INSN        = 12;

    /**
     * The type of the TABLESWITCH instruction.
     */
    final static int         TABL_INSN        = 13;

    /**
     * The type of the LOOKUPSWITCH instruction.
     */
    final static int         LOOK_INSN        = 14;

    /**
     * The type of the MULTIANEWARRAY instruction.
     */
    final static int         MANA_INSN        = 15;

    /**
     * The type of the WIDE instruction.
     */
    final static int         WIDE_INSN        = 16;

    /**
     * The instruction types of all JVM opcodes.
     */
    static byte[]            TYPE;

    /**
     * The type of CONSTANT_Class constant pool items.
     */
    final static int         CLASS            = 7;

    /**
     * The type of CONSTANT_Fieldref constant pool items.
     */
    final static int         FIELD            = 9;

    /**
     * The type of CONSTANT_Methodref constant pool items.
     */
    final static int         METH             = 10;

    /**
     * The type of CONSTANT_InterfaceMethodref constant pool items.
     */
    final static int         IMETH            = 11;

    /**
     * The type of CONSTANT_String constant pool items.
     */
    final static int         STR              = 8;

    /**
     * The type of CONSTANT_Integer constant pool items.
     */
    final static int         INT              = 3;

    /**
     * The type of CONSTANT_Float constant pool items.
     */
    final static int         FLOAT            = 4;

    /**
     * The type of CONSTANT_Long constant pool items.
     */
    final static int         LONG             = 5;

    /**
     * The type of CONSTANT_Double constant pool items.
     */
    final static int         DOUBLE           = 6;

    /**
     * The type of CONSTANT_NameAndType constant pool items.
     */
    final static int         NAME_TYPE        = 12;

    /**
     * The type of CONSTANT_Utf8 constant pool items.
     */
    final static int         UTF8             = 1;

    /**
     * The class reader from which this class writer was constructed, if any.
     */
    ClassReader              cr;

    /**
     * Minor and major version numbers of the class to be generated.
     */
    int                      version;

    /**
     * Index of the next item to be added in the constant pool.
     */
    int                      index;

    /**
     * The constant pool of this class.
     */
    ByteVector               pool;

    /**
     * The constant pool's hash table data.
     */
    Item[]                   items;

    /**
     * The threshold of the constant pool's hash table.
     */
    int                      threshold;

    /**
     * A reusable key used to look for items in the hash {@link #items items}.
     */
    Item                     key;

    /**
     * A reusable key used to look for items in the hash {@link #items items}.
     */
    Item                     key2;

    /**
     * A reusable key used to look for items in the hash {@link #items items}.
     */
    Item                     key3;

    /**
     * The access flags of this class.
     */
    private int              access;

    /**
     * The constant pool item that contains the internal name of this class.
     */
    private int              name;

    /**
     * The constant pool item that contains the signature of this class.
     */
    private int              signature;

    /**
     * The constant pool item that contains the internal name of the super class
     * of this class.
     */
    private int              superName;

    /**
     * Number of interfaces implemented or extended by this class or interface.
     */
    private int              interfaceCount;

    /**
     * The interfaces implemented or extended by this class or interface. More
     * precisely, this array contains the indexes of the constant pool items
     * that contain the internal names of these interfaces.
     */
    private int[]            interfaces;

    /**
     * The index of the constant pool item that contains the name of the source
     * file from which this class was compiled.
     */
    private int              sourceFile;

    /**
     * The SourceDebug attribute of this class.
     */
    private ByteVector       sourceDebug;

    /**
     * The constant pool item that contains the name of the enclosing class of
     * this class.
     */
    private int              enclosingMethodOwner;

    /**
     * The constant pool item that contains the name and descriptor of the
     * enclosing method of this class.
     */
    private int              enclosingMethod;

    /**
     * The runtime visible annotations of this class.
     */
    private AnnotationWriter anns;

    /**
     * The runtime invisible annotations of this class.
     */
    private AnnotationWriter ianns;

    /**
     * The non standard attributes of this class.
     */
    private Attribute        attrs;

    /**
     * The number of entries in the InnerClasses attribute.
     */
    private int              innerClassesCount;

    /**
     * The InnerClasses attribute.
     */
    private ByteVector       innerClasses;

    /**
     * The fields of this class. These fields are stored in a linked list of
     * {@link FieldWriter} objects, linked to each other by their
     * {@link FieldWriter#next} field. This field stores the first element of
     * this list.
     */
    FieldWriter              firstField;

    /**
     * The fields of this class. These fields are stored in a linked list of
     * {@link FieldWriter} objects, linked to each other by their
     * {@link FieldWriter#next} field. This field stores the last element of
     * this list.
     */
    FieldWriter              lastField;

    /**
     * The methods of this class. These methods are stored in a linked list of
     * {@link MethodWriter} objects, linked to each other by their
     * {@link MethodWriter#next} field. This field stores the first element of
     * this list.
     */
    MethodWriter             firstMethod;

    /**
     * The methods of this class. These methods are stored in a linked list of
     * {@link MethodWriter} objects, linked to each other by their
     * {@link MethodWriter#next} field. This field stores the last element of
     * this list.
     */
    MethodWriter             lastMethod;

    /**
     * <tt>true</tt> if the maximum stack size and number of local variables
     * must be automatically computed.
     */
    private boolean          computeMaxs;

    // ------------------------------------------------------------------------
    // Static initializer
    // ------------------------------------------------------------------------

    /**
     * Computes the instruction types of JVM opcodes.
     */
    static {
        int i;
        final byte[] b = new byte[220];
        final String s = "AAAAAAAAAAAAAAAABCKLLDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD" + "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAIIIIIIIIIIIIIIIIDNOAA"
                         + "AAAAGGGGGGGHAFBFAAFFAAQPIIJJIIIIIIIIIIIIIIIIII";
        for ( i = 0; i < b.length; ++i ) {
            b[i] = (byte) (s.charAt( i ) - 'A');
        }
        ClassWriter.TYPE = b;

        // code to generate the above string
        //
        // // SBYTE_INSN instructions
        // b[Constants.NEWARRAY] = SBYTE_INSN;
        // b[Constants.BIPUSH] = SBYTE_INSN;
        //
        // // SHORT_INSN instructions
        // b[Constants.SIPUSH] = SHORT_INSN;
        //
        // // (IMPL)VAR_INSN instructions
        // b[Constants.RET] = VAR_INSN;
        // for (i = Constants.ILOAD; i <= Constants.ALOAD; ++i) {
        // b[i] = VAR_INSN;
        // }
        // for (i = Constants.ISTORE; i <= Constants.ASTORE; ++i) {
        // b[i] = VAR_INSN;
        // }
        // for (i = 26; i <= 45; ++i) { // ILOAD_0 to ALOAD_3
        // b[i] = IMPLVAR_INSN;
        // }
        // for (i = 59; i <= 78; ++i) { // ISTORE_0 to ASTORE_3
        // b[i] = IMPLVAR_INSN;
        // }
        //
        // // TYPE_INSN instructions
        // b[Constants.NEW] = TYPE_INSN;
        // b[Constants.ANEWARRAY] = TYPE_INSN;
        // b[Constants.CHECKCAST] = TYPE_INSN;
        // b[Constants.INSTANCEOF] = TYPE_INSN;
        //
        // // (Set)FIELDORMETH_INSN instructions
        // for (i = Constants.GETSTATIC; i <= Constants.INVOKESTATIC; ++i) {
        // b[i] = FIELDORMETH_INSN;
        // }
        // b[Constants.INVOKEINTERFACE] = ITFMETH_INSN;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -