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

📄 tracemethodvisitor.java

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

import java.util.HashMap;

import org.drools.asm.AnnotationVisitor;
import org.drools.asm.Attribute;
import org.drools.asm.Label;
import org.drools.asm.MethodVisitor;
import org.drools.asm.Opcodes;
import org.drools.asm.Type;
import org.drools.asm.signature.SignatureReader;
import org.drools.asm.util.attrs.Traceable;

/**
 * A {@link MethodVisitor} that prints a disassembled view of the methods it
 * visits.
 * 
 * @author Eric Bruneton
 */
public class TraceMethodVisitor extends TraceAbstractVisitor
    implements
    MethodVisitor {

    /**
     * The {@link MethodVisitor} to which this visitor delegates calls. May be
     * <tt>null</tt>.
     */
    protected MethodVisitor mv;

    /**
     * Tab for bytecode instructions.
     */
    protected String        tab2 = "    ";

    /**
     * Tab for table and lookup switch instructions.
     */
    protected String        tab3 = "      ";

    /**
     * Tab for labels.
     */
    protected String        ltab = "   ";

    /**
     * The label names. This map associate String values to Label keys.
     */
    protected final HashMap labelNames;

    /**
     * Constructs a new {@link TraceMethodVisitor}.
     */
    public TraceMethodVisitor() {
        this( null );
    }

    /**
     * Constructs a new {@link TraceMethodVisitor}.
     * 
     * @param mv the {@link MethodVisitor} to which this visitor delegates
     *        calls. May be <tt>null</tt>.
     */
    public TraceMethodVisitor(final MethodVisitor mv) {
        this.labelNames = new HashMap();
        this.mv = mv;
    }

    // ------------------------------------------------------------------------
    // Implementation of the MethodVisitor interface
    // ------------------------------------------------------------------------

    public AnnotationVisitor visitAnnotation(final String desc,
                                             final boolean visible) {
        final AnnotationVisitor av = super.visitAnnotation( desc,
                                                            visible );
        if ( this.mv != null ) {
            ((TraceAnnotationVisitor) av).av = this.mv.visitAnnotation( desc,
                                                                        visible );
        }
        return av;
    }

    public void visitAttribute(final Attribute attr) {
        this.buf.setLength( 0 );
        this.buf.append( this.tab ).append( "ATTRIBUTE " );
        appendDescriptor( -1,
                          attr.type );

        if ( attr instanceof Traceable ) {
            ((Traceable) attr).trace( this.buf,
                                      this.labelNames );
        } else {
            this.buf.append( " : " ).append( attr.toString() ).append( "\n" );
        }

        this.text.add( this.buf.toString() );
        if ( this.mv != null ) {
            this.mv.visitAttribute( attr );
        }
    }

    public AnnotationVisitor visitAnnotationDefault() {
        this.text.add( this.tab2 + "default=" );
        final TraceAnnotationVisitor tav = new TraceAnnotationVisitor();
        this.text.add( tav.getText() );
        this.text.add( "\n" );
        if ( this.mv != null ) {
            tav.av = this.mv.visitAnnotationDefault();
        }
        return tav;
    }

    public AnnotationVisitor visitParameterAnnotation(final int parameter,
                                                      final String desc,
                                                      final boolean visible) {
        this.buf.setLength( 0 );
        this.buf.append( this.tab2 ).append( '@' );
        appendDescriptor( TraceAbstractVisitor.FIELD_DESCRIPTOR,
                          desc );
        this.buf.append( '(' );
        this.text.add( this.buf.toString() );
        final TraceAnnotationVisitor tav = new TraceAnnotationVisitor();
        this.text.add( tav.getText() );
        this.text.add( visible ? ") // parameter " : ") // invisible, parameter " );
        this.text.add( new Integer( parameter ) );
        this.text.add( "\n" );
        if ( this.mv != null ) {
            tav.av = this.mv.visitParameterAnnotation( parameter,
                                                       desc,
                                                       visible );
        }
        return tav;
    }

    public void visitCode() {
        if ( this.mv != null ) {
            this.mv.visitCode();
        }
    }

    public void visitInsn(final int opcode) {
        this.buf.setLength( 0 );
        this.buf.append( this.tab2 ).append( AbstractVisitor.OPCODES[opcode] ).append( '\n' );
        this.text.add( this.buf.toString() );

        if ( this.mv != null ) {
            this.mv.visitInsn( opcode );
        }
    }

    public void visitIntInsn(final int opcode,
                             final int operand) {
        this.buf.setLength( 0 );
        this.buf.append( this.tab2 ).append( AbstractVisitor.OPCODES[opcode] ).append( ' ' ).append( opcode == Opcodes.NEWARRAY ? AbstractVisitor.TYPES[operand] : Integer.toString( operand ) ).append( '\n' );
        this.text.add( this.buf.toString() );

        if ( this.mv != null ) {
            this.mv.visitIntInsn( opcode,
                                  operand );
        }
    }

    public void visitVarInsn(final int opcode,
                             final int var) {
        this.buf.setLength( 0 );
        this.buf.append( this.tab2 ).append( AbstractVisitor.OPCODES[opcode] ).append( ' ' ).append( var ).append( '\n' );
        this.text.add( this.buf.toString() );

        if ( this.mv != null ) {
            this.mv.visitVarInsn( opcode,
                                  var );
        }
    }

    public void visitTypeInsn(final int opcode,
                              final String desc) {
        this.buf.setLength( 0 );
        this.buf.append( this.tab2 ).append( AbstractVisitor.OPCODES[opcode] ).append( ' ' );
        if ( desc.startsWith( "[" ) ) {
            appendDescriptor( TraceAbstractVisitor.FIELD_DESCRIPTOR,
                              desc );
        } else {
            appendDescriptor( TraceAbstractVisitor.INTERNAL_NAME,
                              desc );
        }
        this.buf.append( '\n' );
        this.text.add( this.buf.toString() );

        if ( this.mv != null ) {
            this.mv.visitTypeInsn( opcode,
                                   desc );
        }
    }

    public void visitFieldInsn(final int opcode,
                               final String owner,
                               final String name,
                               final String desc) {
        this.buf.setLength( 0 );
        this.buf.append( this.tab2 ).append( AbstractVisitor.OPCODES[opcode] ).append( ' ' );
        appendDescriptor( TraceAbstractVisitor.INTERNAL_NAME,
                          owner );
        this.buf.append( '.' ).append( name ).append( " : " );
        appendDescriptor( TraceAbstractVisitor.FIELD_DESCRIPTOR,
                          desc );
        this.buf.append( '\n' );
        this.text.add( this.buf.toString() );

        if ( this.mv != null ) {
            this.mv.visitFieldInsn( opcode,
                                    owner,
                                    name,
                                    desc );
        }
    }

    public void visitMethodInsn(final int opcode,
                                final String owner,
                                final String name,
                                final String desc) {
        this.buf.setLength( 0 );
        this.buf.append( this.tab2 ).append( AbstractVisitor.OPCODES[opcode] ).append( ' ' );
        appendDescriptor( TraceAbstractVisitor.INTERNAL_NAME,
                          owner );
        this.buf.append( '.' ).append( name ).append( ' ' );

⌨️ 快捷键说明

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