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.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);
 172     }
 173 
 174     @Override
 175     public void visitSource(final String file, final String debug) {
 176         p.visitSource(file, debug);
 177         super.visitSource(file, debug);
 178     }
 179 
 180     @Override
 181     public void visitOuterClass(final String owner, final String name,
 182             final String desc) {
 183         p.visitOuterClass(owner, name, desc);
 184         super.visitOuterClass(owner, name, desc);
 185     }
 186 
 187     @Override
 188     public AnnotationVisitor visitAnnotation(final String desc,
 189             final boolean visible) {
 190         Printer p = this.p.visitClassAnnotation(desc, visible);
 191         AnnotationVisitor av = cv == null ? null : cv.visitAnnotation(desc,
 192                 visible);
 193         return new TraceAnnotationVisitor(av, p);
 194     }
 195 
 196     @Override
 197     public AnnotationVisitor visitTypeAnnotation(int typeRef,
 198             TypePath typePath, String desc, boolean visible) {
 199         Printer p = this.p.visitClassTypeAnnotation(typeRef, typePath, desc,
 200                 visible);
 201         AnnotationVisitor av = cv == null ? null : cv.visitTypeAnnotation(
 202                 typeRef, typePath, desc, visible);
 203         return new TraceAnnotationVisitor(av, p);
 204     }
 205 
 206     @Override
 207     public void visitAttribute(final Attribute attr) {
 208         p.visitClassAttribute(attr);
 209         super.visitAttribute(attr);
 210     }
 211 
 212     @Override
 213     public void visitInnerClass(final String name, final String outerName,
 214             final String innerName, final int access) {
 215         p.visitInnerClass(name, outerName, innerName, access);
 216         super.visitInnerClass(name, outerName, innerName, access);
 217     }
 218 
 219     @Override
 220     public FieldVisitor visitField(final int access, final String name,
 221             final String desc, final String signature, final Object value) {
 222         Printer p = this.p.visitField(access, name, desc, signature, value);
 223         FieldVisitor fv = cv == null ? null : cv.visitField(access, name, desc,
 224                 signature, value);
 225         return new TraceFieldVisitor(fv, p);
 226     }
 227 
 228     @Override
 229     public MethodVisitor visitMethod(final int access, final String name,
 230             final String desc, final String signature, final String[] exceptions) {
 231         Printer p = this.p.visitMethod(access, name, desc, signature,
 232                 exceptions);
 233         MethodVisitor mv = cv == null ? null : cv.visitMethod(access, name,
 234                 desc, signature, exceptions);
 235         return new TraceMethodVisitor(mv, p);
 236     }
 237 
 238     @Override
 239     public void visitEnd() {
 240         p.visitClassEnd();
 241         if (pw != null) {
 242             p.print(pw);
 243             pw.flush();
 244         }
 245         super.visitEnd();
 246     }
 247 }