• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2007 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 package org.mockito.asm.util;
31 
32 import org.mockito.asm.AnnotationVisitor;
33 import org.mockito.asm.Attribute;
34 
35 /**
36  * An abstract trace visitor.
37  *
38  * @author Eric Bruneton
39  */
40 public abstract class TraceAbstractVisitor extends AbstractVisitor {
41 
42     /**
43      * Constant used in {@link #appendDescriptor appendDescriptor} for internal
44      * type names in bytecode notation.
45      */
46     public static final int INTERNAL_NAME = 0;
47 
48     /**
49      * Constant used in {@link #appendDescriptor appendDescriptor} for field
50      * descriptors, formatted in bytecode notation
51      */
52     public static final int FIELD_DESCRIPTOR = 1;
53 
54     /**
55      * Constant used in {@link #appendDescriptor appendDescriptor} for field
56      * signatures, formatted in bytecode notation
57      */
58     public static final int FIELD_SIGNATURE = 2;
59 
60     /**
61      * Constant used in {@link #appendDescriptor appendDescriptor} for method
62      * descriptors, formatted in bytecode notation
63      */
64     public static final int METHOD_DESCRIPTOR = 3;
65 
66     /**
67      * Constant used in {@link #appendDescriptor appendDescriptor} for method
68      * signatures, formatted in bytecode notation
69      */
70     public static final int METHOD_SIGNATURE = 4;
71 
72     /**
73      * Constant used in {@link #appendDescriptor appendDescriptor} for class
74      * signatures, formatted in bytecode notation
75      */
76     public static final int CLASS_SIGNATURE = 5;
77 
78     /**
79      * Constant used in {@link #appendDescriptor appendDescriptor} for field or
80      * method return value signatures, formatted in default Java notation
81      * (non-bytecode)
82      */
83     public static final int TYPE_DECLARATION = 6;
84 
85     /**
86      * Constant used in {@link #appendDescriptor appendDescriptor} for class
87      * signatures, formatted in default Java notation (non-bytecode)
88      */
89     public static final int CLASS_DECLARATION = 7;
90 
91     /**
92      * Constant used in {@link #appendDescriptor appendDescriptor} for method
93      * parameter signatures, formatted in default Java notation (non-bytecode)
94      */
95     public static final int PARAMETERS_DECLARATION = 8;
96 
97     /**
98      * Tab for class members.
99      */
100     protected String tab = "  ";
101 
102     /**
103      * Prints a disassembled view of the given annotation.
104      *
105      * @param desc the class descriptor of the annotation class.
106      * @param visible <tt>true</tt> if the annotation is visible at runtime.
107      * @return a visitor to visit the annotation values.
108      */
visitAnnotation( final String desc, final boolean visible)109     public AnnotationVisitor visitAnnotation(
110         final String desc,
111         final boolean visible)
112     {
113         buf.setLength(0);
114         buf.append(tab).append('@');
115         appendDescriptor(FIELD_DESCRIPTOR, desc);
116         buf.append('(');
117         text.add(buf.toString());
118         TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
119         text.add(tav.getText());
120         text.add(visible ? ")\n" : ") // invisible\n");
121         return tav;
122     }
123 
124     /**
125      * Prints a disassembled view of the given attribute.
126      *
127      * @param attr an attribute.
128      */
visitAttribute(final Attribute attr)129     public void visitAttribute(final Attribute attr) {
130         buf.setLength(0);
131         buf.append(tab).append("ATTRIBUTE ");
132         appendDescriptor(-1, attr.type);
133 
134         if (attr instanceof Traceable) {
135             ((Traceable) attr).trace(buf, null);
136         } else {
137             buf.append(" : unknown\n");
138         }
139 
140         text.add(buf.toString());
141     }
142 
143     /**
144      * Does nothing.
145      */
visitEnd()146     public void visitEnd() {
147         // does nothing
148     }
149 
150     // ------------------------------------------------------------------------
151     // Utility methods
152     // ------------------------------------------------------------------------
153 
createTraceAnnotationVisitor()154     protected TraceAnnotationVisitor createTraceAnnotationVisitor() {
155         return new TraceAnnotationVisitor();
156     }
157 
158     /**
159      * Appends an internal name, a type descriptor or a type signature to
160      * {@link #buf buf}.
161      *
162      * @param type indicates if desc is an internal name, a field descriptor, a
163      *        method descriptor, a class signature, ...
164      * @param desc an internal name, type descriptor, or type signature. May be
165      *        <tt>null</tt>.
166      */
appendDescriptor(final int type, final String desc)167     protected void appendDescriptor(final int type, final String desc) {
168         if (type == CLASS_SIGNATURE || type == FIELD_SIGNATURE
169                 || type == METHOD_SIGNATURE)
170         {
171             if (desc != null) {
172                 buf.append("// signature ").append(desc).append('\n');
173             }
174         } else {
175             buf.append(desc);
176         }
177     }
178 
179 }
180