1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * ASM: a very small and fast Java bytecode manipulation framework 32 * Copyright (c) 2000-2011 INRIA, France Telecom 33 * Copyright (c) 2011 Google 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the copyright holders nor the names of its 45 * contributors may be used to endorse or promote products derived from 46 * this software without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 49 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 52 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 58 * THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package jdk.internal.org.objectweb.asm.util; 61 62 import java.io.PrintWriter; 63 64 import jdk.internal.org.objectweb.asm.AnnotationVisitor; 65 import jdk.internal.org.objectweb.asm.Attribute; 66 import jdk.internal.org.objectweb.asm.ClassVisitor; 67 import jdk.internal.org.objectweb.asm.FieldVisitor; 68 import jdk.internal.org.objectweb.asm.MethodVisitor; 69 import jdk.internal.org.objectweb.asm.Opcodes; 70 71 /** 72 * A {@link ClassVisitor} that prints the classes it visits with a 73 * {@link Printer}. This class visitor can be used in the middle of a class 74 * visitor chain to trace the class that is visited at a given point in this 75 * chain. This may be useful for debugging purposes. <p> The trace printed when 76 * visiting the <tt>Hello</tt> class is the following: <p> <blockquote> 77 * 78 * <pre> // class version 49.0 (49) // access flags 0x21 public class Hello { 79 * 80 * // compiled from: Hello.java 81 * 82 * // access flags 0x1 public <init> ()V ALOAD 0 INVOKESPECIAL 83 * java/lang/Object <init> ()V RETURN MAXSTACK = 1 MAXLOCALS = 1 84 * 85 * // access flags 0x9 public static main ([Ljava/lang/String;)V GETSTATIC 86 * java/lang/System out Ljava/io/PrintStream; LDC "hello" 87 * INVOKEVIRTUAL java/io/PrintStream println (Ljava/lang/String;)V RETURN 88 * MAXSTACK = 2 MAXLOCALS = 1 } </pre> 89 * 90 * </blockquote> where <tt>Hello</tt> is defined by: <p> <blockquote> 91 * 92 * <pre> public class Hello { 93 * 94 * public static void main(String[] args) { 95 * System.out.println("hello"); } } </pre> 96 * 97 * </blockquote> 98 * 99 * @author Eric Bruneton 100 * @author Eugene Kuleshov 101 */ 102 public final class TraceClassVisitor extends ClassVisitor { 103 104 /** 105 * The print writer to be used to print the class. May be null. 106 */ 107 private final PrintWriter pw; 108 109 /** 110 * The object that actually converts visit events into text. 111 */ 112 public final Printer p; 113 114 /** 115 * Constructs a new {@link TraceClassVisitor}. 116 * 117 * @param pw the print writer to be used to print the class. 118 */ 119 public TraceClassVisitor(final PrintWriter pw) { 120 this(null, pw); 121 } 122 123 /** 124 * Constructs a new {@link TraceClassVisitor}. 125 * 126 * @param cv the {@link ClassVisitor} to which this visitor delegates calls. 127 * May be <tt>null</tt>. 128 * @param pw the print writer to be used to print the class. 129 */ 130 public TraceClassVisitor(final ClassVisitor cv, final PrintWriter pw) { 131 this(cv, new Textifier(), pw); 132 } 133 134 /** 135 * Constructs a new {@link TraceClassVisitor}. 136 * 137 * @param cv the {@link ClassVisitor} to which this visitor delegates calls. 138 * May be <tt>null</tt>. 139 * @param p the object that actually converts visit events into text. 140 * @param pw the print writer to be used to print the class. May be null if 141 * you simply want to use the result via 142 * {@link Printer#getText()}, instead of printing it. 143 */ 144 public TraceClassVisitor( 145 final ClassVisitor cv, 146 final Printer p, 147 final PrintWriter pw) 148 { 149 super(Opcodes.ASM4, cv); 150 this.pw = pw; 151 this.p = p; 152 } 153 154 @Override 155 public void visit( 156 final int version, 157 final int access, 158 final String name, 159 final String signature, 160 final String superName, 161 final String[] interfaces) 162 { 163 p.visit(version, access, name, signature, superName, interfaces); 164 super.visit(version, access, name, signature, superName, interfaces); 165 } 166 167 @Override 168 public void visitSource(final String file, final String debug) { 169 p.visitSource(file, debug); 170 super.visitSource(file, debug); 171 } 172 173 @Override 174 public void visitOuterClass( 175 final String owner, 176 final String name, 177 final String desc) 178 { 179 p.visitOuterClass(owner, name, desc); 180 super.visitOuterClass(owner, name, desc); 181 } 182 183 @Override 184 public AnnotationVisitor visitAnnotation( 185 final String desc, 186 final boolean visible) 187 { 188 Printer p = this.p.visitClassAnnotation(desc, visible); 189 AnnotationVisitor av = cv == null ? null : cv.visitAnnotation(desc, 190 visible); 191 return new TraceAnnotationVisitor(av, p); 192 } 193 194 @Override 195 public void visitAttribute(final Attribute attr) { 196 p.visitClassAttribute(attr); 197 super.visitAttribute(attr); 198 } 199 200 @Override 201 public void visitInnerClass( 202 final String name, 203 final String outerName, 204 final String innerName, 205 final int access) 206 { 207 p.visitInnerClass(name, outerName, innerName, access); 208 super.visitInnerClass(name, outerName, innerName, access); 209 } 210 211 @Override 212 public FieldVisitor visitField( 213 final int access, 214 final String name, 215 final String desc, 216 final String signature, 217 final Object value) 218 { 219 Printer p = this.p.visitField(access, 220 name, 221 desc, 222 signature, 223 value); 224 FieldVisitor fv = cv == null ? null : cv.visitField(access, 225 name, 226 desc, 227 signature, 228 value); 229 return new TraceFieldVisitor(fv, p); 230 } 231 232 @Override 233 public MethodVisitor visitMethod( 234 final int access, 235 final String name, 236 final String desc, 237 final String signature, 238 final String[] exceptions) 239 { 240 Printer p = this.p.visitMethod(access, 241 name, 242 desc, 243 signature, 244 exceptions); 245 MethodVisitor mv = cv == null ? null : cv.visitMethod(access, 246 name, 247 desc, 248 signature, 249 exceptions); 250 return new TraceMethodVisitor(mv, p); 251 } 252 253 @Override 254 public void visitEnd() { 255 p.visitClassEnd(); 256 if (pw != null) { 257 p.print(pw); 258 pw.flush(); 259 } 260 super.visitEnd(); 261 } 262 }