• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // ASM: a very small and fast Java bytecode manipulation framework
2 // Copyright (c) 2000-2011 INRIA, France Telecom
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holders nor the names of its
14 //    contributors may be used to endorse or promote products derived from
15 //    this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 // THE POSSIBILITY OF SUCH DAMAGE.
28 package org.objectweb.asm.util;
29 
30 import org.objectweb.asm.AnnotationVisitor;
31 import org.objectweb.asm.Attribute;
32 import org.objectweb.asm.FieldVisitor;
33 import org.objectweb.asm.Opcodes;
34 import org.objectweb.asm.TypePath;
35 
36 /**
37  * A {@link FieldVisitor} that prints the fields it visits with a {@link Printer}.
38  *
39  * @author Eric Bruneton
40  */
41 public final class TraceFieldVisitor extends FieldVisitor {
42 
43   /** The printer to convert the visited field into text. */
44   // DontCheck(MemberName): can't be renamed (for backward binary compatibility).
45   public final Printer p;
46 
47   /**
48    * Constructs a new {@link TraceFieldVisitor}.
49    *
50    * @param printer the printer to convert the visited field into text.
51    */
TraceFieldVisitor(final Printer printer)52   public TraceFieldVisitor(final Printer printer) {
53     this(null, printer);
54   }
55 
56   /**
57    * Constructs a new {@link TraceFieldVisitor}.
58    *
59    * @param fieldVisitor the field visitor to which to delegate calls. May be {@literal null}.
60    * @param printer the printer to convert the visited field into text.
61    */
TraceFieldVisitor(final FieldVisitor fieldVisitor, final Printer printer)62   public TraceFieldVisitor(final FieldVisitor fieldVisitor, final Printer printer) {
63     super(/* latest api = */ Opcodes.ASM9, fieldVisitor);
64     this.p = printer;
65   }
66 
67   @Override
visitAnnotation(final String descriptor, final boolean visible)68   public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
69     Printer annotationPrinter = p.visitFieldAnnotation(descriptor, visible);
70     return new TraceAnnotationVisitor(
71         super.visitAnnotation(descriptor, visible), annotationPrinter);
72   }
73 
74   @Override
visitTypeAnnotation( final int typeRef, final TypePath typePath, final String descriptor, final boolean visible)75   public AnnotationVisitor visitTypeAnnotation(
76       final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
77     Printer annotationPrinter = p.visitFieldTypeAnnotation(typeRef, typePath, descriptor, visible);
78     return new TraceAnnotationVisitor(
79         super.visitTypeAnnotation(typeRef, typePath, descriptor, visible), annotationPrinter);
80   }
81 
82   @Override
visitAttribute(final Attribute attribute)83   public void visitAttribute(final Attribute attribute) {
84     p.visitFieldAttribute(attribute);
85     super.visitAttribute(attribute);
86   }
87 
88   @Override
visitEnd()89   public void visitEnd() {
90     p.visitFieldEnd();
91     super.visitEnd();
92   }
93 }
94