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 * All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the copyright holders nor the names of its 44 * contributors may be used to endorse or promote products derived from 45 * this software without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 57 * THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 package jdk.internal.org.objectweb.asm.util; 60 61 import java.io.PrintWriter; 62 63 import jdk.internal.org.objectweb.asm.AnnotationVisitor; 64 import jdk.internal.org.objectweb.asm.Attribute; 65 import jdk.internal.org.objectweb.asm.ClassVisitor; 66 import jdk.internal.org.objectweb.asm.FieldVisitor; 67 import jdk.internal.org.objectweb.asm.MethodVisitor; 68 import jdk.internal.org.objectweb.asm.ModuleVisitor; 69 import jdk.internal.org.objectweb.asm.Opcodes; 70 import jdk.internal.org.objectweb.asm.TypePath; 71 72 /** 73 * A {@link ClassVisitor} that prints the classes it visits with a 74 * {@link Printer}. This class visitor can be used in the middle of a class 75 * visitor chain to trace the class that is visited at a given point in this 76 * chain. This may be useful for debugging purposes. 77 * <p> 78 * The trace printed when visiting the <tt>Hello</tt> class is the following: 79 * <p> 80 * <blockquote> 81 * 82 * <pre> 83 * // class version 49.0 (49) // access flags 0x21 public class Hello { 84 * 85 * // compiled from: Hello.java 86 * 87 * // access flags 0x1 public <init> ()V ALOAD 0 INVOKESPECIAL 88 * java/lang/Object <init> ()V RETURN MAXSTACK = 1 MAXLOCALS = 1 89 * 90 * // access flags 0x9 public static main ([Ljava/lang/String;)V GETSTATIC 91 * java/lang/System out Ljava/io/PrintStream; LDC "hello" 92 * INVOKEVIRTUAL java/io/PrintStream println (Ljava/lang/String;)V RETURN 93 * MAXSTACK = 2 MAXLOCALS = 1 } 94 * </pre> 95 * 96 * </blockquote> where <tt>Hello</tt> is defined by: 97 * <p> 98 * <blockquote> 99 * 100 * <pre> 101 * public class Hello { 102 * 103 * public static void main(String[] args) { 104 * System.out.println("hello"); 105 * } 106 * } 107 * </pre> 108 * 109 * </blockquote> 110 * 111 * @author Eric Bruneton 112 * @author Eugene Kuleshov 113 */ 114 public final class TraceClassVisitor extends ClassVisitor { 115 116 /** 117 * The print writer to be used to print the class. May be null. 118 */ 119 private final PrintWriter pw; 120 121 /** 122 * The object that actually converts visit events into text. 123 */ 124 public final Printer p; 125 126 /** 127 * Constructs a new {@link TraceClassVisitor}. 128 * 129 * @param pw 130 * the print writer to be used to print the class. 131 */ 132 public TraceClassVisitor(final PrintWriter pw) { 133 this(null, pw); 134 } 135 136 /** 137 * Constructs a new {@link TraceClassVisitor}. 138 * 139 * @param cv 140 * the {@link ClassVisitor} to which this visitor delegates 141 * calls. May be <tt>null</tt>. 142 * @param pw 143 * the print writer to be used to print the class. 144 */ 145 public TraceClassVisitor(final ClassVisitor cv, final PrintWriter pw) { 146 this(cv, new Textifier(), pw); 147 } 148 149 /** 150 * Constructs a new {@link TraceClassVisitor}. 151 * 152 * @param cv 153 * the {@link ClassVisitor} to which this visitor delegates 154 * calls. May be <tt>null</tt>. 155 * @param p 156 * the object that actually converts visit events into text. 157 * @param pw 158 * the print writer to be used to print the class. May be null if 159 * you simply want to use the result via 160 * {@link Printer#getText()}, instead of printing it. 161 */ 162 public TraceClassVisitor(final ClassVisitor cv, final Printer p, 163 final PrintWriter pw) { 164 super(Opcodes.ASM6, cv); 165 this.pw = pw; 166 this.p = p; 167 } 168 169 @Override 170 public void visit(final int version, final int access, final String name, 171 final String signature, final String superName, 172 final String[] interfaces) { 173 p.visit(version, access, name, signature, superName, interfaces); 174 super.visit(version, access, name, signature, superName, interfaces); 175 } 176 177 @Override 178 public void visitSource(final String file, final String debug) { 179 p.visitSource(file, debug); 180 super.visitSource(file, debug); 181 } 182 183 @Override 184 public ModuleVisitor visitModule(String name, int flags, 185 String version) { 186 Printer p = this.p.visitModule(name, flags, version); 187 ModuleVisitor mv = super.visitModule(name, flags, version); 188 return new TraceModuleVisitor(mv, p); 189 } 190 191 @Override 192 public void visitOuterClass(final String owner, final String name, 193 final String desc) { 194 p.visitOuterClass(owner, name, desc); 195 super.visitOuterClass(owner, name, desc); 196 } 197 198 @Override 199 public AnnotationVisitor visitAnnotation(final String desc, 200 final boolean visible) { 201 Printer p = this.p.visitClassAnnotation(desc, visible); 202 AnnotationVisitor av = cv == null ? null : cv.visitAnnotation(desc, 203 visible); 204 return new TraceAnnotationVisitor(av, p); 205 } 206 207 @Override 208 public AnnotationVisitor visitTypeAnnotation(int typeRef, 209 TypePath typePath, String desc, boolean visible) { 210 Printer p = this.p.visitClassTypeAnnotation(typeRef, typePath, desc, 211 visible); 212 AnnotationVisitor av = cv == null ? null : cv.visitTypeAnnotation( 213 typeRef, typePath, desc, visible); 214 return new TraceAnnotationVisitor(av, p); 215 } 216 217 @Override 218 public void visitAttribute(final Attribute attr) { 219 p.visitClassAttribute(attr); 220 super.visitAttribute(attr); 221 } 222 223 @Override 224 public void visitInnerClass(final String name, final String outerName, 225 final String innerName, final int access) { 226 p.visitInnerClass(name, outerName, innerName, access); 227 super.visitInnerClass(name, outerName, innerName, access); 228 } 229 230 @Override 231 public FieldVisitor visitField(final int access, final String name, 232 final String desc, final String signature, final Object value) { 233 Printer p = this.p.visitField(access, name, desc, signature, value); 234 FieldVisitor fv = cv == null ? null : cv.visitField(access, name, desc, 235 signature, value); 236 return new TraceFieldVisitor(fv, p); 237 } 238 239 @Override 240 public MethodVisitor visitMethod(final int access, final String name, 241 final String desc, final String signature, final String[] exceptions) { 242 Printer p = this.p.visitMethod(access, name, desc, signature, 243 exceptions); 244 MethodVisitor mv = cv == null ? null : cv.visitMethod(access, name, 245 desc, signature, exceptions); 246 return new TraceMethodVisitor(mv, p); 247 } 248 249 @Override 250 public void visitEnd() { 251 p.visitClassEnd(); 252 if (pw != null) { 253 p.print(pw); 254 pw.flush(); 255 } 256 super.visitEnd(); 257 } 258 }