Home | History | Annotate | Download | only in util
      1 /***
      2  * ASM: a very small and fast Java bytecode manipulation framework
      3  * Copyright (c) 2000-2005 INRIA, France Telecom
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. Neither the name of the copyright holders nor the names of its
     15  *    contributors may be used to endorse or promote products derived from
     16  *    this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     28  * THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 package org.objectweb.asm.util;
     31 
     32 import org.objectweb.asm.AnnotationVisitor;
     33 import org.objectweb.asm.Attribute;
     34 import org.objectweb.asm.TypeAnnotationVisitor;
     35 import org.objectweb.asm.util.attrs.Traceable;
     36 
     37 /**
     38  * An abstract trace visitor.
     39  *
     40  * @author Eric Bruneton
     41  */
     42 public abstract class TraceAbstractVisitor extends AbstractVisitor {
     43 
     44     /**
     45      * Constant used in {@link #appendDescriptor appendDescriptor} for internal
     46      * type names in bytecode notation.
     47      */
     48     public final static int INTERNAL_NAME = 0;
     49 
     50     /**
     51      * Constant used in {@link #appendDescriptor appendDescriptor} for field
     52      * descriptors, formatted in bytecode notation
     53      */
     54     public final static int FIELD_DESCRIPTOR = 1;
     55 
     56     /**
     57      * Constant used in {@link #appendDescriptor appendDescriptor} for field
     58      * signatures, formatted in bytecode notation
     59      */
     60     public final static int FIELD_SIGNATURE = 2;
     61 
     62     /**
     63      * Constant used in {@link #appendDescriptor appendDescriptor} for method
     64      * descriptors, formatted in bytecode notation
     65      */
     66     public final static int METHOD_DESCRIPTOR = 3;
     67 
     68     /**
     69      * Constant used in {@link #appendDescriptor appendDescriptor} for method
     70      * signatures, formatted in bytecode notation
     71      */
     72     public final static int METHOD_SIGNATURE = 4;
     73 
     74     /**
     75      * Constant used in {@link #appendDescriptor appendDescriptor} for class
     76      * signatures, formatted in bytecode notation
     77      */
     78     public final static int CLASS_SIGNATURE = 5;
     79 
     80     /**
     81      * Constant used in {@link #appendDescriptor appendDescriptor} for field or
     82      * method return value signatures, formatted in default Java notation
     83      * (non-bytecode)
     84      */
     85     public final static int TYPE_DECLARATION = 6;
     86 
     87     /**
     88      * Constant used in {@link #appendDescriptor appendDescriptor} for class
     89      * signatures, formatted in default Java notation (non-bytecode)
     90      */
     91     public final static int CLASS_DECLARATION = 7;
     92 
     93     /**
     94      * Constant used in {@link #appendDescriptor appendDescriptor} for method
     95      * parameter signatures, formatted in default Java notation (non-bytecode)
     96      */
     97     public final static int PARAMETERS_DECLARATION = 8;
     98 
     99     /**
    100      * Tab for class members.
    101      */
    102     protected String tab = "  ";
    103 
    104     /**
    105      * Prints a disassembled view of the given annotation.
    106      *
    107      * @param desc the class descriptor of the annotation class.
    108      * @param visible <tt>true</tt> if the annotation is visible at runtime.
    109      * @return a visitor to visit the annotation values.
    110      */
    111     public AnnotationVisitor visitAnnotation(
    112         final String desc,
    113         final boolean visible)
    114     {
    115         buf.setLength(0);
    116         buf.append(tab).append('@');
    117         appendDescriptor(FIELD_DESCRIPTOR, desc);
    118         buf.append('(');
    119         text.add(buf.toString());
    120         TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
    121         text.add(tav.getText());
    122         text.add(visible ? ")\n" : ") // invisible\n");
    123         return tav;
    124     }
    125 
    126     //jaime
    127     public TypeAnnotationVisitor visitTypeAnnotation(
    128         final String desc,
    129         final boolean visible)
    130     {
    131         buf.setLength(0);
    132         buf.append(tab).append('@');
    133         appendDescriptor(FIELD_DESCRIPTOR, desc);
    134         buf.append('(');
    135         text.add(buf.toString());
    136         TraceTypeAnnotationVisitor txav =
    137           createTraceTypeAnnotationVisitor();
    138         text.add(txav.getText());
    139         text.add(visible ? ")\n" : ") // invisible\n");
    140         return txav;
    141     }
    142     //end jaime
    143 
    144     /**
    145      * Prints a disassembled view of the given attribute.
    146      *
    147      * @param attr an attribute.
    148      */
    149     public void visitAttribute(final Attribute attr) {
    150         buf.setLength(0);
    151         buf.append(tab).append("ATTRIBUTE ");
    152         appendDescriptor(-1, attr.type);
    153 
    154         if (attr instanceof Traceable) {
    155             ((Traceable) attr).trace(buf, null);
    156         } else {
    157             buf.append(" : ").append(attr.toString()).append("\n");
    158         }
    159 
    160         text.add(buf.toString());
    161     }
    162 
    163     /**
    164      * Does nothing.
    165      */
    166     public void visitEnd() {
    167         // does nothing
    168     }
    169 
    170     // ------------------------------------------------------------------------
    171     // Utility methods
    172     // ------------------------------------------------------------------------
    173 
    174     protected TraceAnnotationVisitor createTraceAnnotationVisitor() {
    175         return new TraceAnnotationVisitor();
    176     }
    177 
    178     //jaime
    179     protected TraceTypeAnnotationVisitor createTraceTypeAnnotationVisitor() {
    180         return new TraceTypeAnnotationVisitor();
    181     }
    182     //end jaime
    183 
    184     /**
    185      * Appends an internal name, a type descriptor or a type signature to
    186      * {@link #buf buf}.
    187      *
    188      * @param type indicates if desc is an internal name, a field descriptor, a
    189      *        method descriptor, a class signature, ...
    190      * @param desc an internal name, type descriptor, or type signature. May be
    191      *        <tt>null</tt>.
    192      */
    193     protected void appendDescriptor(final int type, final String desc) {
    194         if (type == CLASS_SIGNATURE || type == FIELD_SIGNATURE
    195                 || type == METHOD_SIGNATURE)
    196         {
    197             if (desc != null) {
    198                 buf.append("// signature ").append(desc).append('\n');
    199             }
    200         } else {
    201             buf.append(desc);
    202         }
    203     }
    204 
    205 }
    206