001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.ejb3unit.asm.util;
030:
031: import org.ejb3unit.asm.AnnotationVisitor;
032: import org.ejb3unit.asm.Type;
033:
034: /**
035: * An {@link AnnotationVisitor} that prints a disassembled view of the
036: * annotations it visits.
037: *
038: * @author Eric Bruneton
039: */
040: public class TraceAnnotationVisitor extends TraceAbstractVisitor
041: implements AnnotationVisitor {
042:
043: /**
044: * The {@link AnnotationVisitor} to which this visitor delegates calls. May
045: * be <tt>null</tt>.
046: */
047: protected AnnotationVisitor av;
048:
049: private int valueNumber = 0;
050:
051: /**
052: * Constructs a new {@link TraceAnnotationVisitor}.
053: */
054: public TraceAnnotationVisitor() {
055: // ignore
056: }
057:
058: // ------------------------------------------------------------------------
059: // Implementation of the AnnotationVisitor interface
060: // ------------------------------------------------------------------------
061:
062: public void visit(final String name, final Object value) {
063: buf.setLength(0);
064: appendComa(valueNumber++);
065:
066: if (name != null) {
067: buf.append(name).append('=');
068: }
069:
070: if (value instanceof String) {
071: visitString((String) value);
072: } else if (value instanceof Type) {
073: visitType((Type) value);
074: } else if (value instanceof Byte) {
075: visitByte(((Byte) value).byteValue());
076: } else if (value instanceof Boolean) {
077: visitBoolean(((Boolean) value).booleanValue());
078: } else if (value instanceof Short) {
079: visitShort(((Short) value).shortValue());
080: } else if (value instanceof Character) {
081: visitChar(((Character) value).charValue());
082: } else if (value instanceof Integer) {
083: visitInt(((Integer) value).intValue());
084: } else if (value instanceof Float) {
085: visitFloat(((Float) value).floatValue());
086: } else if (value instanceof Long) {
087: visitLong(((Long) value).longValue());
088: } else if (value instanceof Double) {
089: visitDouble(((Double) value).doubleValue());
090: } else if (value.getClass().isArray()) {
091: buf.append('{');
092: if (value instanceof byte[]) {
093: byte[] v = (byte[]) value;
094: for (int i = 0; i < v.length; i++) {
095: appendComa(i);
096: visitByte(v[i]);
097: }
098: } else if (value instanceof boolean[]) {
099: boolean[] v = (boolean[]) value;
100: for (int i = 0; i < v.length; i++) {
101: appendComa(i);
102: visitBoolean(v[i]);
103: }
104: } else if (value instanceof short[]) {
105: short[] v = (short[]) value;
106: for (int i = 0; i < v.length; i++) {
107: appendComa(i);
108: visitShort(v[i]);
109: }
110: } else if (value instanceof char[]) {
111: char[] v = (char[]) value;
112: for (int i = 0; i < v.length; i++) {
113: appendComa(i);
114: visitChar(v[i]);
115: }
116: } else if (value instanceof int[]) {
117: int[] v = (int[]) value;
118: for (int i = 0; i < v.length; i++) {
119: appendComa(i);
120: visitInt(v[i]);
121: }
122: } else if (value instanceof long[]) {
123: long[] v = (long[]) value;
124: for (int i = 0; i < v.length; i++) {
125: appendComa(i);
126: visitLong(v[i]);
127: }
128: } else if (value instanceof float[]) {
129: float[] v = (float[]) value;
130: for (int i = 0; i < v.length; i++) {
131: appendComa(i);
132: visitFloat(v[i]);
133: }
134: } else if (value instanceof double[]) {
135: double[] v = (double[]) value;
136: for (int i = 0; i < v.length; i++) {
137: appendComa(i);
138: visitDouble(v[i]);
139: }
140: }
141: buf.append('}');
142: }
143:
144: text.add(buf.toString());
145:
146: if (av != null) {
147: av.visit(name, value);
148: }
149: }
150:
151: private void visitInt(final int value) {
152: buf.append(value);
153: }
154:
155: private void visitLong(final long value) {
156: buf.append(value).append('L');
157: }
158:
159: private void visitFloat(final float value) {
160: buf.append(value).append('F');
161: }
162:
163: private void visitDouble(final double value) {
164: buf.append(value).append('D');
165: }
166:
167: private void visitChar(final char value) {
168: buf.append("(char)").append((int) value);
169: }
170:
171: private void visitShort(final short value) {
172: buf.append("(short)").append(value);
173: }
174:
175: private void visitByte(final byte value) {
176: buf.append("(byte)").append(value);
177: }
178:
179: private void visitBoolean(final boolean value) {
180: buf.append(value);
181: }
182:
183: private void visitString(final String value) {
184: appendString(buf, value);
185: }
186:
187: private void visitType(final Type value) {
188: buf.append(value.getClassName()).append(".class");
189: }
190:
191: public void visitEnum(final String name, final String desc,
192: final String value) {
193: buf.setLength(0);
194: appendComa(valueNumber++);
195: if (name != null) {
196: buf.append(name).append('=');
197: }
198: appendDescriptor(FIELD_DESCRIPTOR, desc);
199: buf.append('.').append(value);
200: text.add(buf.toString());
201:
202: if (av != null) {
203: av.visitEnum(name, desc, value);
204: }
205: }
206:
207: public AnnotationVisitor visitAnnotation(final String name,
208: final String desc) {
209: buf.setLength(0);
210: appendComa(valueNumber++);
211: if (name != null) {
212: buf.append(name).append('=');
213: }
214: buf.append('@');
215: appendDescriptor(FIELD_DESCRIPTOR, desc);
216: buf.append('(');
217: text.add(buf.toString());
218: TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
219: text.add(tav.getText());
220: text.add(")");
221: if (av != null) {
222: tav.av = av.visitAnnotation(name, desc);
223: }
224: return tav;
225: }
226:
227: public AnnotationVisitor visitArray(final String name) {
228: buf.setLength(0);
229: appendComa(valueNumber++);
230: if (name != null) {
231: buf.append(name).append('=');
232: }
233: buf.append('{');
234: text.add(buf.toString());
235: TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
236: text.add(tav.getText());
237: text.add("}");
238: if (av != null) {
239: tav.av = av.visitArray(name);
240: }
241: return tav;
242: }
243:
244: public void visitEnd() {
245: if (av != null) {
246: av.visitEnd();
247: }
248: }
249:
250: // ------------------------------------------------------------------------
251: // Utility methods
252: // ------------------------------------------------------------------------
253:
254: private void appendComa(final int i) {
255: if (i != 0) {
256: buf.append(", ");
257: }
258: }
259: }
|