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 */
59package jdk.internal.org.objectweb.asm.util;
60
61import java.io.PrintWriter;
62
63import jdk.internal.org.objectweb.asm.AnnotationVisitor;
64import jdk.internal.org.objectweb.asm.Attribute;
65import jdk.internal.org.objectweb.asm.ClassVisitor;
66import jdk.internal.org.objectweb.asm.FieldVisitor;
67import jdk.internal.org.objectweb.asm.MethodVisitor;
68import jdk.internal.org.objectweb.asm.Opcodes;
69import 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 &lt;init&gt; ()V ALOAD 0 INVOKESPECIAL
87 * java/lang/Object &lt;init&gt; ()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 &quot;hello&quot;
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(&quot;hello&quot;);
104 *     }
105 * }
106 * </pre>
107 *
108 * </blockquote>
109 *
110 * @author Eric Bruneton
111 * @author Eugene Kuleshov
112 */
113public 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);
174    }
175
176    @Override
177    public void visitSource(final String file, final String debug) {
178        p.visitSource(file, debug);
179        super.visitSource(file, debug);
180    }
181
182    @Override
183    public void visitOuterClass(final String owner, final String name,
184            final String desc) {
185        p.visitOuterClass(owner, name, desc);
186        super.visitOuterClass(owner, name, desc);
187    }
188
189    @Override
190    public AnnotationVisitor visitAnnotation(final String desc,
191            final boolean visible) {
192        Printer p = this.p.visitClassAnnotation(desc, visible);
193        AnnotationVisitor av = cv == null ? null : cv.visitAnnotation(desc,
194                visible);
195        return new TraceAnnotationVisitor(av, p);
196    }
197
198    @Override
199    public AnnotationVisitor visitTypeAnnotation(int typeRef,
200            TypePath typePath, String desc, boolean visible) {
201        Printer p = this.p.visitClassTypeAnnotation(typeRef, typePath, desc,
202                visible);
203        AnnotationVisitor av = cv == null ? null : cv.visitTypeAnnotation(
204                typeRef, typePath, desc, visible);
205        return new TraceAnnotationVisitor(av, p);
206    }
207
208    @Override
209    public void visitAttribute(final Attribute attr) {
210        p.visitClassAttribute(attr);
211        super.visitAttribute(attr);
212    }
213
214    @Override
215    public void visitInnerClass(final String name, final String outerName,
216            final String innerName, final int access) {
217        p.visitInnerClass(name, outerName, innerName, access);
218        super.visitInnerClass(name, outerName, innerName, access);
219    }
220
221    @Override
222    public FieldVisitor visitField(final int access, final String name,
223            final String desc, final String signature, final Object value) {
224        Printer p = this.p.visitField(access, name, desc, signature, value);
225        FieldVisitor fv = cv == null ? null : cv.visitField(access, name, desc,
226                signature, value);
227        return new TraceFieldVisitor(fv, p);
228    }
229
230    @Override
231    public MethodVisitor visitMethod(final int access, final String name,
232            final String desc, final String signature, final String[] exceptions) {
233        Printer p = this.p.visitMethod(access, name, desc, signature,
234                exceptions);
235        MethodVisitor mv = cv == null ? null : cv.visitMethod(access, name,
236                desc, signature, exceptions);
237        return new TraceMethodVisitor(mv, p);
238    }
239
240    @Override
241    public void visitEnd() {
242        p.visitClassEnd();
243        if (pw != null) {
244            p.print(pw);
245            pw.flush();
246        }
247        super.visitEnd();
248    }
249}
250