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.drools.asm.util;
030:
031: import org.drools.asm.AnnotationVisitor;
032: import org.drools.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: this .buf.setLength(0);
064: appendComa(this .valueNumber++);
065:
066: if (name != null) {
067: this .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: this .buf.append('{');
092: if (value instanceof byte[]) {
093: final 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: final 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: final 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: final 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: final 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: final 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: final 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: final double[] v = (double[]) value;
136: for (int i = 0; i < v.length; i++) {
137: appendComa(i);
138: visitDouble(v[i]);
139: }
140: }
141: this .buf.append('}');
142: } else {
143: this .buf.append(value);
144: }
145:
146: this .text.add(this .buf.toString());
147:
148: if (this .av != null) {
149: this .av.visit(name, value);
150: }
151: }
152:
153: private void visitInt(final int value) {
154: this .buf.append(value);
155: }
156:
157: private void visitLong(final long value) {
158: this .buf.append(value).append('L');
159: }
160:
161: private void visitFloat(final float value) {
162: this .buf.append(value).append('F');
163: }
164:
165: private void visitDouble(final double value) {
166: this .buf.append(value).append('D');
167: }
168:
169: private void visitChar(final char value) {
170: this .buf.append("(char)").append((int) value);
171: }
172:
173: private void visitShort(final short value) {
174: this .buf.append("(short)").append(value);
175: }
176:
177: private void visitByte(final byte value) {
178: this .buf.append("(byte)").append(value);
179: }
180:
181: private void visitBoolean(final boolean value) {
182: this .buf.append(value);
183: }
184:
185: private void visitString(final String value) {
186: appendString(this .buf, value);
187: }
188:
189: private void visitType(final Type value) {
190: this .buf.append(value.getClassName()).append(".class");
191: }
192:
193: public void visitEnum(final String name, final String desc,
194: final String value) {
195: this .buf.setLength(0);
196: appendComa(this .valueNumber++);
197: if (name != null) {
198: this .buf.append(name).append('=');
199: }
200: appendDescriptor(TraceAbstractVisitor.FIELD_DESCRIPTOR, desc);
201: this .buf.append('.').append(value);
202: this .text.add(this .buf.toString());
203:
204: if (this .av != null) {
205: this .av.visitEnum(name, desc, value);
206: }
207: }
208:
209: public AnnotationVisitor visitAnnotation(final String name,
210: final String desc) {
211: this .buf.setLength(0);
212: appendComa(this .valueNumber++);
213: if (name != null) {
214: this .buf.append(name).append('=');
215: }
216: this .buf.append('@');
217: appendDescriptor(TraceAbstractVisitor.FIELD_DESCRIPTOR, desc);
218: this .buf.append('(');
219: this .text.add(this .buf.toString());
220: final TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
221: this .text.add(tav.getText());
222: this .text.add(")");
223: if (this .av != null) {
224: tav.av = this .av.visitAnnotation(name, desc);
225: }
226: return tav;
227: }
228:
229: public AnnotationVisitor visitArray(final String name) {
230: this .buf.setLength(0);
231: appendComa(this .valueNumber++);
232: if (name != null) {
233: this .buf.append(name).append('=');
234: }
235: this .buf.append('{');
236: this .text.add(this .buf.toString());
237: final TraceAnnotationVisitor tav = createTraceAnnotationVisitor();
238: this .text.add(tav.getText());
239: this .text.add("}");
240: if (this .av != null) {
241: tav.av = this .av.visitArray(name);
242: }
243: return tav;
244: }
245:
246: public void visitEnd() {
247: if (this .av != null) {
248: this .av.visitEnd();
249: }
250: }
251:
252: // ------------------------------------------------------------------------
253: // Utility methods
254: // ------------------------------------------------------------------------
255:
256: protected TraceAnnotationVisitor createTraceAnnotationVisitor() {
257: return new TraceAnnotationVisitor();
258: }
259:
260: private void appendComa(final int i) {
261: if (i != 0) {
262: this .buf.append(", ");
263: }
264: }
265: }
|