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.util.attrs.Traceable; 35 36 /** 37 * An abstract trace visitor. 38 * 39 * @author Eric Bruneton 40 */ 41 public abstract class TraceAbstractVisitor extends AbstractVisitor { 42 43 /** 44 * Constant used in {@link #appendDescriptor appendDescriptor} for internal 45 * type names in bytecode notation. 46 */ 47 public final static int INTERNAL_NAME = 0; 48 49 /** 50 * Constant used in {@link #appendDescriptor appendDescriptor} for field 51 * descriptors, formatted in bytecode notation 52 */ 53 public final static int FIELD_DESCRIPTOR = 1; 54 55 /** 56 * Constant used in {@link #appendDescriptor appendDescriptor} for field 57 * signatures, formatted in bytecode notation 58 */ 59 public final static int FIELD_SIGNATURE = 2; 60 61 /** 62 * Constant used in {@link #appendDescriptor appendDescriptor} for method 63 * descriptors, formatted in bytecode notation 64 */ 65 public final static int METHOD_DESCRIPTOR = 3; 66 67 /** 68 * Constant used in {@link #appendDescriptor appendDescriptor} for method 69 * signatures, formatted in bytecode notation 70 */ 71 public final static int METHOD_SIGNATURE = 4; 72 73 /** 74 * Constant used in {@link #appendDescriptor appendDescriptor} for class 75 * signatures, formatted in bytecode notation 76 */ 77 public final static int CLASS_SIGNATURE = 5; 78 79 /** 80 * Constant used in {@link #appendDescriptor appendDescriptor} for field or 81 * method return value signatures, formatted in default Java notation 82 * (non-bytecode) 83 */ 84 public final static int TYPE_DECLARATION = 6; 85 86 /** 87 * Constant used in {@link #appendDescriptor appendDescriptor} for class 88 * signatures, formatted in default Java notation (non-bytecode) 89 */ 90 public final static int CLASS_DECLARATION = 7; 91 92 /** 93 * Constant used in {@link #appendDescriptor appendDescriptor} for method 94 * parameter signatures, formatted in default Java notation (non-bytecode) 95 */ 96 public final static int PARAMETERS_DECLARATION = 8; 97 98 /** 99 * Tab for class members. 100 */ 101 protected String tab = " "; 102 103 /** 104 * Prints a disassembled view of the given annotation. 105 * 106 * @param desc the class descriptor of the annotation class. 107 * @param visible <tt>true</tt> if the annotation is visible at runtime. 108 * @return a visitor to visit the annotation values. 109 */ visitAnnotation( final String desc, final boolean visible)110 public AnnotationVisitor visitAnnotation( 111 final String desc, 112 final boolean visible) 113 { 114 buf.setLength(0); 115 buf.append(tab).append('@'); 116 appendDescriptor(FIELD_DESCRIPTOR, desc); 117 buf.append('('); 118 text.add(buf.toString()); 119 TraceAnnotationVisitor tav = createTraceAnnotationVisitor(); 120 text.add(tav.getText()); 121 text.add(visible ? ")\n" : ") // invisible\n"); 122 return tav; 123 } 124 125 /** 126 * Prints a disassembled view of the given attribute. 127 * 128 * @param attr an attribute. 129 */ visitAttribute(final Attribute attr)130 public void visitAttribute(final Attribute attr) { 131 buf.setLength(0); 132 buf.append(tab).append("ATTRIBUTE "); 133 appendDescriptor(-1, attr.type); 134 135 if (attr instanceof Traceable) { 136 ((Traceable) attr).trace(buf, null); 137 } else { 138 buf.append(" : ").append(attr.toString()).append("\n"); 139 } 140 141 text.add(buf.toString()); 142 } 143 144 /** 145 * Does nothing. 146 */ visitEnd()147 public void visitEnd() { 148 // does nothing 149 } 150 151 // ------------------------------------------------------------------------ 152 // Utility methods 153 // ------------------------------------------------------------------------ 154 createTraceAnnotationVisitor()155 protected TraceAnnotationVisitor createTraceAnnotationVisitor() { 156 return new TraceAnnotationVisitor(); 157 } 158 159 /** 160 * Appends an internal name, a type descriptor or a type signature to 161 * {@link #buf buf}. 162 * 163 * @param type indicates if desc is an internal name, a field descriptor, a 164 * method descriptor, a class signature, ... 165 * @param desc an internal name, type descriptor, or type signature. May be 166 * <tt>null</tt>. 167 */ appendDescriptor(final int type, final String desc)168 protected void appendDescriptor(final int type, final String desc) { 169 if (type == CLASS_SIGNATURE || type == FIELD_SIGNATURE 170 || type == METHOD_SIGNATURE) 171 { 172 if (desc != null) { 173 buf.append("// signature ").append(desc).append('\n'); 174 } 175 } else { 176 buf.append(desc); 177 } 178 } 179 180 } 181