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

📄 generatoradapter.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/***
 * 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.commons;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.drools.asm.ClassVisitor;
import org.drools.asm.Label;
import org.drools.asm.MethodVisitor;
import org.drools.asm.Opcodes;
import org.drools.asm.Type;

/**
 * A {@link org.drools.asm.MethodAdapter} with convenient methods to generate
 * code. For example, using this adapter, the class below
 * 
 * <pre>
 * public class Example {
 *     public static void main(String[] args) {
 *         System.out.println(&quot;Hello world!&quot;);
 *     }
 * }
 * </pre>
 * 
 * can be generated as follows:
 * 
 * <pre>
 * ClassWriter cw = new ClassWriter(true);
 * cw.visit(V1_1, ACC_PUBLIC, &quot;Example&quot;, null, &quot;java/lang/Object&quot;, null);
 * 
 * Method m = Method.getMethod(&quot;void &lt;init&gt; ()&quot;);
 * GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw);
 * mg.loadThis();
 * mg.invokeConstructor(Type.getType(Object.class), m);
 * mg.returnValue();
 * mg.endMethod();
 * 
 * m = Method.getMethod(&quot;void main (String[])&quot;);
 * mg = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
 * mg.getStatic(Type.getType(System.class), &quot;out&quot;, Type.getType(PrintStream.class));
 * mg.push(&quot;Hello world!&quot;);
 * mg.invokeVirtual(Type.getType(PrintStream.class), Method.getMethod(&quot;void println (String)&quot;));
 * mg.returnValue();
 * mg.endMethod();
 * 
 * cw.visitEnd();
 * </pre>
 * 
 * @author Juozas Baliuka
 * @author Chris Nokleberg
 * @author Eric Bruneton
 */
public class GeneratorAdapter extends LocalVariablesSorter {

    private final static Type   BYTE_TYPE      = Type.getType( "Ljava/lang/Byte;" );

    private final static Type   BOOLEAN_TYPE   = Type.getType( "Ljava/lang/Boolean;" );

    private final static Type   SHORT_TYPE     = Type.getType( "Ljava/lang/Short;" );

    private final static Type   CHARACTER_TYPE = Type.getType( "Ljava/lang/Character;" );

    private final static Type   INTEGER_TYPE   = Type.getType( "Ljava/lang/Integer;" );

    private final static Type   FLOAT_TYPE     = Type.getType( "Ljava/lang/Float;" );

    private final static Type   LONG_TYPE      = Type.getType( "Ljava/lang/Long;" );

    private final static Type   DOUBLE_TYPE    = Type.getType( "Ljava/lang/Double;" );

    private final static Type   NUMBER_TYPE    = Type.getType( "Ljava/lang/Number;" );

    private final static Type   OBJECT_TYPE    = Type.getType( "Ljava/lang/Object;" );

    private final static Method BOOLEAN_VALUE  = Method.getMethod( "boolean booleanValue()" );

    private final static Method CHAR_VALUE     = Method.getMethod( "char charValue()" );

    private final static Method INT_VALUE      = Method.getMethod( "int intValue()" );

    private final static Method FLOAT_VALUE    = Method.getMethod( "float floatValue()" );

    private final static Method LONG_VALUE     = Method.getMethod( "long longValue()" );

    private final static Method DOUBLE_VALUE   = Method.getMethod( "double doubleValue()" );

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     ADD            = Opcodes.IADD;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     SUB            = Opcodes.ISUB;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     MUL            = Opcodes.IMUL;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     DIV            = Opcodes.IDIV;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     REM            = Opcodes.IREM;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     NEG            = Opcodes.INEG;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     SHL            = Opcodes.ISHL;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     SHR            = Opcodes.ISHR;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     USHR           = Opcodes.IUSHR;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     AND            = Opcodes.IAND;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     OR             = Opcodes.IOR;

    /**
     * Constant for the {@link #math math} method.
     */
    public final static int     XOR            = Opcodes.IXOR;

    /**
     * Constant for the {@link #ifCmp ifCmp} method.
     */
    public final static int     EQ             = Opcodes.IFEQ;

    /**
     * Constant for the {@link #ifCmp ifCmp} method.
     */
    public final static int     NE             = Opcodes.IFNE;

    /**
     * Constant for the {@link #ifCmp ifCmp} method.
     */
    public final static int     LT             = Opcodes.IFLT;

    /**
     * Constant for the {@link #ifCmp ifCmp} method.
     */
    public final static int     GE             = Opcodes.IFGE;

    /**
     * Constant for the {@link #ifCmp ifCmp} method.
     */
    public final static int     GT             = Opcodes.IFGT;

    /**
     * Constant for the {@link #ifCmp ifCmp} method.
     */
    public final static int     LE             = Opcodes.IFLE;

    /**
     * Access flags of the method visited by this adapter.
     */
    private final int           access;

    /**
     * Return type of the method visited by this adapter.
     */
    private final Type          returnType;

    /**
     * Argument types of the method visited by this adapter.
     */
    private final Type[]        argumentTypes;

    /**
     * Types of the local variables of the method visited by this adapter.
     */
    private final List          localTypes;

    /**
     * Creates a new {@link GeneratorAdapter}.
     * 
     * @param mv the method visitor to which this adapter delegates calls.
     * @param access the method's access flags (see {@link Opcodes}).
     * @param name the method's name.
     * @param desc the method's descriptor (see {@link Type Type}).
     */
    public GeneratorAdapter(final MethodVisitor mv,
                            final int access,
                            final String name,
                            final String desc) {
        super( access,
               desc,
               mv );
        this.access = access;
        this.returnType = Type.getReturnType( desc );
        this.argumentTypes = Type.getArgumentTypes( desc );
        this.localTypes = new ArrayList();
    }

    /**
     * Creates a new {@link GeneratorAdapter}.
     * 
     * @param access access flags of the adapted method.
     * @param method the adapted method.
     * @param mv the method visitor to which this adapter delegates calls.
     */
    public GeneratorAdapter(final int access,
                            final Method method,
                            final MethodVisitor mv) {
        super( access,
               method.getDescriptor(),
               mv );
        this.access = access;
        this.returnType = method.getReturnType();
        this.argumentTypes = method.getArgumentTypes();
        this.localTypes = new ArrayList();
    }

    /**
     * Creates a new {@link GeneratorAdapter}.
     * 
     * @param access access flags of the adapted method.
     * @param method the adapted method.
     * @param signature the signature of the adapted method (may be
     *        <tt>null</tt>).
     * @param exceptions the exceptions thrown by the adapted method (may be
     *        <tt>null</tt>).
     * @param cv the class visitor to which this adapter delegates calls.
     */
    public GeneratorAdapter(final int access,
                            final Method method,
                            final String signature,
                            final Type[] exceptions,
                            final ClassVisitor cv) {
        this( access,
              method,
              cv.visitMethod( access,
                              method.getName(),
                              method.getDescriptor(),
                              signature,
                              getInternalNames( exceptions ) ) );
    }

    /**
     * Returns the internal names of the given types.
     * 
     * @param types a set of types.
     * @return the internal names of the given types.
     */
    private static String[] getInternalNames(final Type[] types) {
        if ( types == null ) {
            return null;
        }
        final String[] names = new String[types.length];
        for ( int i = 0; i < names.length; ++i ) {
            names[i] = types[i].getInternalName();
        }
        return names;
    }

    // ------------------------------------------------------------------------
    // Instructions to push constants on the stack
    // ------------------------------------------------------------------------

    /**
     * Generates the instruction to push the given value on the stack.
     * 
     * @param value the value to be pushed on the stack.
     */
    public void push(final boolean value) {
        push( value ? 1 : 0 );
    }

    /**
     * Generates the instruction to push the given value on the stack.
     * 
     * @param value the value to be pushed on the stack.
     */
    public void push(final int value) {
        if ( value >= -1 && value <= 5 ) {
            this.mv.visitInsn( Opcodes.ICONST_0 + value );
        } else if ( value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE ) {
            this.mv.visitIntInsn( Opcodes.BIPUSH,
                                  value );
        } else if ( value >= Short.MIN_VALUE && value <= Short.MAX_VALUE ) {
            this.mv.visitIntInsn( Opcodes.SIPUSH,
                                  value );
        } else {
            this.mv.visitLdcInsn( new Integer( value ) );
        }
    }

    /**
     * Generates the instruction to push the given value on the stack.
     * 
     * @param value the value to be pushed on the stack.
     */
    public void push(final long value) {
        if ( value == 0L || value == 1L ) {
            this.mv.visitInsn( Opcodes.LCONST_0 + (int) value );
        } else {
            this.mv.visitLdcInsn( new Long( value ) );
        }
    }

    /**
     * Generates the instruction to push the given value on the stack.
     * 
     * @param value the value to be pushed on the stack.
     */
    public void push(final float value) {
        final int bits = Float.floatToIntBits( value );
        if ( bits == 0L || bits == 0x3f800000 || bits == 0x40000000 ) { // 0..2
            this.mv.visitInsn( Opcodes.FCONST_0 + (int) value );
        } else {
            this.mv.visitLdcInsn( new Float( value ) );
        }
    }

    /**
     * Generates the instruction to push the given value on the stack.
     * 
     * @param value the value to be pushed on the stack.
     */
    public void push(final double value) {
        final long bits = Double.doubleToLongBits( value );
        if ( bits == 0L || bits == 0x3ff0000000000000L ) { // +0.0d and 1.0d
            this.mv.visitInsn( Opcodes.DCONST_0 + (int) value );
        } else {
            this.mv.visitLdcInsn( new Double( value ) );
        }
    }

    /**
     * Generates the instruction to push the given value on the stack.
     * 
     * @param value the value to be pushed on the stack. May be <tt>null</tt>.
     */
    public void push(final String value) {

⌨️ 快捷键说明

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