KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > util > TraceAnnotationVisitor


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 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.objectweb.asm.util;
31
32 import org.objectweb.asm.AnnotationVisitor;
33 import org.objectweb.asm.Type;
34
35 /**
36  * An {@link AnnotationVisitor} that prints a disassembled view of the
37  * annotations it visits.
38  *
39  * @author Eric Bruneton
40  */

41 public class TraceAnnotationVisitor extends TraceAbstractVisitor implements
42         AnnotationVisitor
43 {
44
45     /**
46      * The {@link AnnotationVisitor} to which this visitor delegates calls. May
47      * be <tt>null</tt>.
48      */

49     protected AnnotationVisitor av;
50
51     private int valueNumber = 0;
52
53     /**
54      * Constructs a new {@link TraceAnnotationVisitor}.
55      */

56     public TraceAnnotationVisitor() {
57         // ignore
58
}
59
60     // ------------------------------------------------------------------------
61
// Implementation of the AnnotationVisitor interface
62
// ------------------------------------------------------------------------
63

64     public void visit(final String JavaDoc name, final Object JavaDoc value) {
65         buf.setLength(0);
66         appendComa(valueNumber++);
67
68         if (name != null) {
69             buf.append(name).append('=');
70         }
71
72         if (value instanceof String JavaDoc) {
73             visitString((String JavaDoc) value);
74         } else if (value instanceof Type) {
75             visitType((Type) value);
76         } else if (value instanceof Byte JavaDoc) {
77             visitByte(((Byte JavaDoc) value).byteValue());
78         } else if (value instanceof Boolean JavaDoc) {
79             visitBoolean(((Boolean JavaDoc) value).booleanValue());
80         } else if (value instanceof Short JavaDoc) {
81             visitShort(((Short JavaDoc) value).shortValue());
82         } else if (value instanceof Character JavaDoc) {
83             visitChar(((Character JavaDoc) value).charValue());
84         } else if (value instanceof Integer JavaDoc) {
85             visitInt(((Integer JavaDoc) value).intValue());
86         } else if (value instanceof Float JavaDoc) {
87             visitFloat(((Float JavaDoc) value).floatValue());
88         } else if (value instanceof Long JavaDoc) {
89             visitLong(((Long JavaDoc) value).longValue());
90         } else if (value instanceof Double JavaDoc) {
91             visitDouble(((Double JavaDoc) value).doubleValue());
92         } else if (value.getClass().isArray()) {
93             buf.append('{');
94             if (value instanceof byte[]) {
95                 byte[] v = (byte[]) value;
96                 for (int i = 0; i < v.length; i++) {
97                     appendComa(i);
98                     visitByte(v[i]);
99                 }
100             } else if (value instanceof boolean[]) {
101                 boolean[] v = (boolean[]) value;
102                 for (int i = 0; i < v.length; i++) {
103                     appendComa(i);
104                     visitBoolean(v[i]);
105                 }
106             } else if (value instanceof short[]) {
107                 short[] v = (short[]) value;
108                 for (int i = 0; i < v.length; i++) {
109                     appendComa(i);
110                     visitShort(v[i]);
111                 }
112             } else if (value instanceof char[]) {
113                 char[] v = (char[]) value;
114                 for (int i = 0; i < v.length; i++) {
115                     appendComa(i);
116                     visitChar(v[i]);
117                 }
118             } else if (value instanceof int[]) {
119                 int[] v = (int[]) value;
120                 for (int i = 0; i < v.length; i++) {
121                     appendComa(i);
122                     visitInt(v[i]);
123                 }
124             } else if (value instanceof long[]) {
125                 long[] v = (long[]) value;
126                 for (int i = 0; i < v.length; i++) {
127                     appendComa(i);
128                     visitLong(v[i]);
129                 }
130             } else if (value instanceof float[]) {
131                 float[] v = (float[]) value;
132                 for (int i = 0; i < v.length; i++) {
133                     appendComa(i);
134                     visitFloat(v[i]);
135                 }
136             } else if (value instanceof double[]) {
137                 double[] v = (double[]) value;
138                 for (int i = 0; i < v.length; i++) {
139                     appendComa(i);
140                     visitDouble(v[i]);
141                 }
142             }
143             buf.append('}');
144         } else {
145             buf.append(value);
146         }
147
148         text.add(buf.toString());
149
150         if (av != null) {
151             av.visit(name, value);
152         }
153     }
154
155     private void visitInt(int value) {
156         buf.append(value);
157     }
158
159     private void visitLong(long value) {
160         buf.append(value).append('L');
161     }
162
163     private void visitFloat(float value) {
164         buf.append(value).append('F');
165     }
166
167     private void visitDouble(double value) {
168         buf.append(value).append('D');
169     }
170
171     private void visitChar(char value) {
172         buf.append("(char)").append((int) value);
173     }
174
175     private void visitShort(short value) {
176         buf.append("(short)").append(value);
177     }
178
179     private void visitByte(byte value) {
180         buf.append("(byte)").append(value);
181     }
182
183     private void visitBoolean(boolean value) {
184         buf.append(value);
185     }
186
187     private void visitString(String JavaDoc value) {
188         appendString(buf, value);
189     }
190
191     private void visitType(Type value) {
192         buf.append(value.getClassName()).append(".class");
193     }
194
195     public void visitEnum(
196         final String JavaDoc name,
197         final String JavaDoc desc,
198         final String JavaDoc value)
199     {
200         buf.setLength(0);
201         appendComa(valueNumber++);
202         if (name != null) {
203             buf.append(name).append('=');
204         }
205         appendDescriptor(FIELD_DESCRIPTOR, desc);
206         buf.append('.').append(value);
207         text.add(buf.toString());
208
209         if (av != null) {
210             av.visitEnum(name, desc, value);
211         }
212     }
213
214     public AnnotationVisitor visitAnnotation(
215         final String JavaDoc name,
216         final String JavaDoc desc)
217     {
218         buf.setLength(0);
219         appendComa(valueNumber++);
220         if (name != null) {
221             buf.append(name).append('=');
222         }
223         buf.append('@');
224         appendDescriptor(FIELD_DESCRIPTOR, desc);
225         buf.append('(');
226         text.add(buf.toString());
227         TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
228         text.add(tav.getText());
229         text.add(")");
230         if (av != null) {
231             tav.av = av.visitAnnotation(name, desc);
232         }
233         return tav;
234     }
235
236     public AnnotationVisitor visitArray(final String JavaDoc name) {
237         buf.setLength(0);
238         appendComa(valueNumber++);
239         if (name != null) {
240             buf.append(name).append('=');
241         }
242         buf.append('{');
243         text.add(buf.toString());
244         TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
245         text.add(tav.getText());
246         text.add("}");
247         if (av != null) {
248             tav.av = av.visitArray(name);
249         }
250         return tav;
251     }
252
253     public void visitEnd() {
254         if (av != null) {
255             av.visitEnd();
256         }
257     }
258
259     // ------------------------------------------------------------------------
260
// Utility methods
261
// ------------------------------------------------------------------------
262

263     protected TraceAnnotationVisitor createTraceAnnotationVisitor() {
264         return new TraceAnnotationVisitor();
265     }
266
267     private void appendComa(int i) {
268         if (i != 0) {
269             buf.append(", ");
270         }
271     }
272 }
273
Popular Tags