1 30 package org.objectweb.asm.util; 31 32 import org.objectweb.asm.Opcodes; 33 import org.objectweb.asm.signature.SignatureVisitor; 34 35 public class TraceSignatureVisitor implements SignatureVisitor { 36 37 private StringBuffer declaration; 38 39 private boolean isInterface; 40 41 private boolean seenFormalParameter; 42 43 private boolean seenInterfaceBound; 44 45 private boolean seenParameter; 46 47 private boolean seenInterface; 48 49 private StringBuffer returnType; 50 51 private StringBuffer exceptions; 52 53 59 private int argumentStack; 60 61 66 private int arrayStack; 67 68 private String separator = ""; 69 70 public TraceSignatureVisitor(int access) { 71 isInterface = (access & Opcodes.ACC_INTERFACE) != 0; 72 this.declaration = new StringBuffer (); 73 } 74 75 private TraceSignatureVisitor(StringBuffer buf) { 76 this.declaration = buf; 77 } 78 79 public void visitFormalTypeParameter(String name) { 80 declaration.append(seenFormalParameter ? ", " : "<").append(name); 81 seenFormalParameter = true; 82 seenInterfaceBound = false; 83 } 84 85 public SignatureVisitor visitClassBound() { 86 separator = " extends "; 87 startType(); 88 return this; 89 } 90 91 public SignatureVisitor visitInterfaceBound() { 92 separator = seenInterfaceBound ? ", " : (isInterface 93 ? " extends " 94 : " implements "); 95 seenInterfaceBound = true; 96 startType(); 97 return this; 98 } 99 100 public SignatureVisitor visitSuperclass() { 101 endFormals(); 102 separator = " extends "; 103 startType(); 104 return this; 105 } 106 107 public SignatureVisitor visitInterface() { 108 separator = seenInterface ? ", " : (isInterface 109 ? " extends " 110 : " implements "); 111 seenInterface = true; 112 startType(); 113 return this; 114 } 115 116 public SignatureVisitor visitParameterType() { 117 endFormals(); 118 if (!seenParameter) { 119 seenParameter = true; 120 declaration.append('('); 121 } else { 122 declaration.append(", "); 123 } 124 startType(); 125 return this; 126 } 127 128 public SignatureVisitor visitReturnType() { 129 endFormals(); 130 if (!seenParameter) { 131 declaration.append('('); 132 } 133 declaration.append(')'); 134 returnType = new StringBuffer (); 135 return new TraceSignatureVisitor(returnType); 136 } 137 138 public SignatureVisitor visitExceptionType() { 139 if (exceptions == null) { 140 exceptions = new StringBuffer (); 141 } else { 142 exceptions.append(", "); 143 } 144 return new TraceSignatureVisitor(exceptions); 146 } 147 148 public void visitBaseType(char descriptor) { 149 switch (descriptor) { 150 case 'V': 151 declaration.append("void"); 152 break; 153 case 'B': 154 declaration.append("byte"); 155 break; 156 case 'J': 157 declaration.append("long"); 158 break; 159 case 'Z': 160 declaration.append("boolean"); 161 break; 162 case 'I': 163 declaration.append("int"); 164 break; 165 case 'S': 166 declaration.append("short"); 167 break; 168 case 'C': 169 declaration.append("char"); 170 break; 171 case 'F': 172 declaration.append("float"); 173 break; 174 case 'D': 175 declaration.append("double"); 176 break; 177 default: 178 throw new IllegalArgumentException ("Invalid descriptor " 179 + descriptor); 180 } 181 endType(); 182 } 183 184 public void visitTypeVariable(String name) { 185 declaration.append(name); 186 endType(); 187 } 188 189 public SignatureVisitor visitArrayType() { 190 startType(); 191 arrayStack |= 1; 192 return this; 193 } 194 195 public void visitClassType(String name) { 196 if (!"java/lang/Object".equals(name)) { 197 declaration.append(separator).append(name.replace('/', '.')); 198 } 199 separator = ""; 200 argumentStack *= 2; 201 } 202 203 public void visitInnerClassType(String name) { 204 } 206 207 public void visitTypeArgument() { 208 if (argumentStack % 2 == 0) { 209 ++argumentStack; 210 declaration.append("<"); 211 } else { 212 declaration.append(", "); 213 } 214 declaration.append("?"); 215 } 216 217 public SignatureVisitor visitTypeArgument(char tag) { 218 if (argumentStack % 2 == 0) { 219 ++argumentStack; 220 declaration.append("<"); 221 } else { 222 declaration.append(", "); 223 } 224 225 if (tag == SignatureVisitor.EXTENDS) { 226 declaration.append("? extends "); 227 } else if (tag == SignatureVisitor.SUPER) { 228 declaration.append("? super "); 229 } 230 231 startType(); 232 return this; 233 } 234 235 public void visitEnd() { 236 if (argumentStack % 2 == 1) { 237 declaration.append(">"); 238 } 239 argumentStack /= 2; 240 endType(); 241 } 242 243 public String getDeclaration() { 244 return declaration.toString(); 245 } 246 247 public String getReturnType() { 248 return returnType == null ? null : returnType.toString(); 249 } 250 251 public String getExceptions() { 252 return exceptions == null ? null : exceptions.toString(); 253 } 254 255 257 private void endFormals() { 258 if (seenFormalParameter) { 259 declaration.append(">"); 260 seenFormalParameter = false; 261 } 262 } 263 264 private void startType() { 265 arrayStack *= 2; 266 } 267 268 private void endType() { 269 if (arrayStack % 2 == 1) { 270 while (arrayStack % 2 == 1) { 271 arrayStack /= 2; 272 declaration.append("[]"); 273 } 274 } else { 275 arrayStack /= 2; 276 } 277 } 278 } 279 | Popular Tags |