1 30 package org.objectweb.asm.util; 31 32 import org.objectweb.asm.AnnotationVisitor; 33 import org.objectweb.asm.Type; 34 35 41 public class TraceAnnotationVisitor extends TraceAbstractVisitor implements 42 AnnotationVisitor 43 { 44 45 49 protected AnnotationVisitor av; 50 51 private int valueNumber = 0; 52 53 56 public TraceAnnotationVisitor() { 57 } 59 60 64 public void visit(final String name, final Object 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 ) { 73 visitString((String ) value); 74 } else if (value instanceof Type) { 75 visitType((Type) value); 76 } else if (value instanceof Byte ) { 77 visitByte(((Byte ) value).byteValue()); 78 } else if (value instanceof Boolean ) { 79 visitBoolean(((Boolean ) value).booleanValue()); 80 } else if (value instanceof Short ) { 81 visitShort(((Short ) value).shortValue()); 82 } else if (value instanceof Character ) { 83 visitChar(((Character ) value).charValue()); 84 } else if (value instanceof Integer ) { 85 visitInt(((Integer ) value).intValue()); 86 } else if (value instanceof Float ) { 87 visitFloat(((Float ) value).floatValue()); 88 } else if (value instanceof Long ) { 89 visitLong(((Long ) value).longValue()); 90 } else if (value instanceof Double ) { 91 visitDouble(((Double ) 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 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 name, 197 final String desc, 198 final String 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 name, 216 final String 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 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 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 |