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.Opcodes; 69 import jdk.internal.org.objectweb.asm.TypePath; 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. 76 * <p> 77 * The trace printed when visiting the <tt>Hello</tt> class is the following: 78 * <p> 79 * <blockquote> 80 * 81 * <pre> 82 * // class version 49.0 (49) // access flags 0x21 public class Hello { 83 * 84 * // compiled from: Hello.java 85 * 86 * // access flags 0x1 public <init> ()V ALOAD 0 INVOKESPECIAL 87 * java/lang/Object <init> ()V RETURN MAXSTACK = 1 MAXLOCALS = 1 88 * 89 * // access flags 0x9 public static main ([Ljava/lang/String;)V GETSTATIC 90 * java/lang/System out Ljava/io/PrintStream; LDC "hello" 91 * INVOKEVIRTUAL java/io/PrintStream println (Ljava/lang/String;)V RETURN 92 * MAXSTACK = 2 MAXLOCALS = 1 } 93 * </pre> 94 * 95 * </blockquote> where <tt>Hello</tt> is defined by: 96 * <p> 97 * <blockquote> 98 * 99 * <pre> 100 * public class Hello { 101 * 102 * public static void main(String[] args) { 103 * System.out.println("hello"); 104 * } 105 * } 106 * </pre> 107 * 108 * </blockquote> 109 * 110 * @author Eric Bruneton 111 * @author Eugene Kuleshov 112 */ 113 public final class TraceClassVisitor extends ClassVisitor { 114 115 /** 116 * The print writer to be used to print the class. May be null. 117 */ 118 private final PrintWriter pw; 119 120 /** 121 * The object that actually converts visit events into text. 122 */ 123 public final Printer p; 124 125 /** 126 * Constructs a new {@link TraceClassVisitor}. 127 * 128 * @param pw 129 * the print writer to be used to print the class. 130 */ 131 public TraceClassVisitor(final PrintWriter pw) { 132 this(null, pw); 133 } 134 135 /** 136 * Constructs a new {@link TraceClassVisitor}. 137 * 138 * @param cv 139 * the {@link ClassVisitor} to which this visitor delegates 140 * calls. May be <tt>null</tt>. 141 * @param pw 142 * the print writer to be used to print the class. 143 */ 144 public TraceClassVisitor(final ClassVisitor cv, final PrintWriter pw) { 145 this(cv, new Textifier(), pw); 146 } 147 148 /** 149 * Constructs a new {@link TraceClassVisitor}. 150 * 151 * @param cv 152 * the {@link ClassVisitor} to which this visitor delegates 153 * calls. May be <tt>null</tt>. 154 * @param p 155 * the object that actually converts visit events into text. 156 * @param pw 157 * the print writer to be used to print the class. May be null if 158 * you simply want to use the result via 159 * {@link Printer#getText()}, instead of printing it. 160 */ 161 public TraceClassVisitor(final ClassVisitor cv, final Printer p, 162 final PrintWriter pw) { 163 super(Opcodes.ASM5, cv); 164 this.pw = pw; 165 this.p = p; 166 } 167 168 @Override 169 public void visit(final int version, final int access, final String name, 170 final String signature, final String superName, 171 final String[] interfaces) { 172 p.visit(version, access, name, signature, superName, interfaces); 173 super.visit(version, access, name, signature, superName, interfaces); | 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.Opcodes; 69 import jdk.internal.org.objectweb.asm.TypePath; 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. 76 * <p> 77 * The trace printed when visiting the {@code Hello} class is the following: 78 * <blockquote> 79 * 80 * <pre>{@code 81 * // class version 49.0 (49) // access flags 0x21 public class Hello { 82 * 83 * // compiled from: Hello.java 84 * 85 * // access flags 0x1 public <init> ()V ALOAD 0 INVOKESPECIAL 86 * java/lang/Object <init> ()V RETURN MAXSTACK = 1 MAXLOCALS = 1 87 * 88 * // access flags 0x9 public static main ([Ljava/lang/String;)V GETSTATIC 89 * java/lang/System out Ljava/io/PrintStream; LDC "hello" 90 * INVOKEVIRTUAL java/io/PrintStream println (Ljava/lang/String;)V RETURN 91 * MAXSTACK = 2 MAXLOCALS = 1 } 92 * }</pre> 93 * 94 * </blockquote> where {@code Hello} is defined by: 95 * <blockquote> 96 * 97 * <pre>{@code 98 * public class Hello { 99 * 100 * public static void main(String[] args) { 101 * System.out.println("hello"); 102 * } 103 * } 104 * }</pre> 105 * 106 * </blockquote> 107 * 108 * @author Eric Bruneton 109 * @author Eugene Kuleshov 110 */ 111 public final class TraceClassVisitor extends ClassVisitor { 112 113 /** 114 * The print writer to be used to print the class. May be null. 115 */ 116 private final PrintWriter pw; 117 118 /** 119 * The object that actually converts visit events into text. 120 */ 121 public final Printer p; 122 123 /** 124 * Constructs a new {@link TraceClassVisitor}. 125 * 126 * @param pw 127 * the print writer to be used to print the class. 128 */ 129 public TraceClassVisitor(final PrintWriter pw) { 130 this(null, pw); 131 } 132 133 /** 134 * Constructs a new {@link TraceClassVisitor}. 135 * 136 * @param cv 137 * the {@link ClassVisitor} to which this visitor delegates 138 * calls. May be {@code null}. 139 * @param pw 140 * the print writer to be used to print the class. 141 */ 142 public TraceClassVisitor(final ClassVisitor cv, final PrintWriter pw) { 143 this(cv, new Textifier(), pw); 144 } 145 146 /** 147 * Constructs a new {@link TraceClassVisitor}. 148 * 149 * @param cv 150 * the {@link ClassVisitor} to which this visitor delegates 151 * calls. May be {@code null}. 152 * @param p 153 * the object that actually converts visit events into text. 154 * @param pw 155 * the print writer to be used to print the class. May be null if 156 * you simply want to use the result via 157 * {@link Printer#getText()}, instead of printing it. 158 */ 159 public TraceClassVisitor(final ClassVisitor cv, final Printer p, 160 final PrintWriter pw) { 161 super(Opcodes.ASM5, cv); 162 this.pw = pw; 163 this.p = p; 164 } 165 166 @Override 167 public void visit(final int version, final int access, final String name, 168 final String signature, final String superName, 169 final String[] interfaces) { 170 p.visit(version, access, name, signature, superName, interfaces); 171 super.visit(version, access, name, signature, superName, interfaces); |