1 30 package org.objectweb.asm.depend; 31 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 import org.objectweb.asm.AnnotationVisitor; 38 import org.objectweb.asm.Attribute; 39 import org.objectweb.asm.ClassVisitor; 40 import org.objectweb.asm.FieldVisitor; 41 import org.objectweb.asm.Label; 42 import org.objectweb.asm.MethodVisitor; 43 import org.objectweb.asm.Type; 44 import org.objectweb.asm.signature.SignatureReader; 45 import org.objectweb.asm.signature.SignatureVisitor; 46 47 52 public class DependencyVisitor implements 53 AnnotationVisitor, 54 SignatureVisitor, 55 ClassVisitor, 56 FieldVisitor, 57 MethodVisitor 58 { 59 Set <String > packages = new HashSet <String >(); 60 61 Map <String , Map <String , Integer >> groups = new HashMap <String , Map <String , Integer >>(); 62 63 Map <String , Integer > current; 64 65 public Map <String , Map <String , Integer >> getGlobals() { 66 return groups; 67 } 68 69 public Set <String > getPackages() { 70 return packages; 71 } 72 73 75 public void visit( 76 int version, 77 int access, 78 String name, 79 String signature, 80 String superName, 81 String [] interfaces) 82 { 83 String p = getGroupKey(name); 84 current = groups.get(p); 85 if (current == null) { 86 current = new HashMap <String , Integer >(); 87 groups.put(p, current); 88 } 89 90 if (signature == null) { 91 addName(superName); 92 addNames(interfaces); 93 } else { 94 addSignature(signature); 95 } 96 } 97 98 public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 99 addDesc(desc); 100 return this; 101 } 102 103 public void visitAttribute(Attribute attr) { 104 } 105 106 public FieldVisitor visitField( 107 int access, 108 String name, 109 String desc, 110 String signature, 111 Object value) 112 { 113 if (signature == null) { 114 addDesc(desc); 115 } else { 116 addTypeSignature(signature); 117 } 118 if (value instanceof Type) 119 addType((Type) value); 120 return this; 121 } 122 123 public MethodVisitor visitMethod( 124 int access, 125 String name, 126 String desc, 127 String signature, 128 String [] exceptions) 129 { 130 if (signature == null) { 131 addMethodDesc(desc); 132 } else { 133 addSignature(signature); 134 } 135 addNames(exceptions); 136 return this; 137 } 138 139 public void visitSource(String source, String debug) { 140 } 141 142 public void visitInnerClass( 143 String name, 144 String outerName, 145 String innerName, 146 int access) 147 { 148 } 151 152 public void visitOuterClass(String owner, String name, String desc) { 153 } 156 157 159 public AnnotationVisitor visitParameterAnnotation( 160 int parameter, 161 String desc, 162 boolean visible) 163 { 164 addDesc(desc); 165 return this; 166 } 167 168 public void visitTypeInsn(int opcode, String desc) { 169 if (desc.charAt(0) == '[') 170 addDesc(desc); 171 else 172 addName(desc); 173 } 174 175 public void visitFieldInsn( 176 int opcode, 177 String owner, 178 String name, 179 String desc) 180 { 181 addName(owner); 182 addDesc(desc); 183 } 184 185 public void visitMethodInsn( 186 int opcode, 187 String owner, 188 String name, 189 String desc) 190 { 191 addName(owner); 192 addMethodDesc(desc); 193 } 194 195 public void visitLdcInsn(Object cst) { 196 if (cst instanceof Type) 197 addType((Type) cst); 198 } 199 200 public void visitMultiANewArrayInsn(String desc, int dims) { 201 addDesc(desc); 202 } 203 204 public void visitLocalVariable( 205 String name, 206 String desc, 207 String signature, 208 Label start, 209 Label end, 210 int index) 211 { 212 addTypeSignature(signature); 213 } 214 215 public AnnotationVisitor visitAnnotationDefault() { 216 return this; 217 } 218 219 public void visitCode() { 220 } 221 222 public void visitInsn(int opcode) { 223 } 224 225 public void visitIntInsn(int opcode, int operand) { 226 } 227 228 public void visitVarInsn(int opcode, int var) { 229 } 230 231 public void visitJumpInsn(int opcode, Label label) { 232 } 233 234 public void visitLabel(Label label) { 235 } 236 237 public void visitIincInsn(int var, int increment) { 238 } 239 240 public void visitTableSwitchInsn( 241 int min, 242 int max, 243 Label dflt, 244 Label[] labels) 245 { 246 } 247 248 public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) { 249 } 250 251 public void visitTryCatchBlock( 252 Label start, 253 Label end, 254 Label handler, 255 String type) 256 { 257 addName(type); 258 } 259 260 public void visitLineNumber(int line, Label start) { 261 } 262 263 public void visitMaxs(int maxStack, int maxLocals) { 264 } 265 266 268 public void visit(String name, Object value) { 269 if (value instanceof Type) 270 addType((Type) value); 271 } 272 273 public void visitEnum(String name, String desc, String value) { 274 addDesc(desc); 275 } 276 277 public AnnotationVisitor visitAnnotation(String name, String desc) { 278 addDesc(desc); 279 return this; 280 } 281 282 public AnnotationVisitor visitArray(String name) { 283 return this; 284 } 285 286 288 public void visitFormalTypeParameter(String name) { 289 } 290 291 public SignatureVisitor visitClassBound() { 292 return this; 293 } 294 295 public SignatureVisitor visitInterfaceBound() { 296 return this; 297 } 298 299 public SignatureVisitor visitSuperclass() { 300 return this; 301 } 302 303 public SignatureVisitor visitInterface() { 304 return this; 305 } 306 307 public SignatureVisitor visitParameterType() { 308 return this; 309 } 310 311 public SignatureVisitor visitReturnType() { 312 return this; 313 } 314 315 public SignatureVisitor visitExceptionType() { 316 return this; 317 } 318 319 public void visitBaseType(char descriptor) { 320 } 321 322 public void visitTypeVariable(String name) { 323 } 325 326 public SignatureVisitor visitArrayType() { 327 return this; 328 } 329 330 public void visitClassType(String name) { 331 addName(name); 332 } 333 334 public void visitInnerClassType(String name) { 335 addName(name); 336 } 337 338 public void visitTypeArgument() { 339 } 340 341 public SignatureVisitor visitTypeArgument(char wildcard) { 342 return this; 343 } 344 345 347 public void visitEnd() { 348 } 349 350 352 private String getGroupKey(String name) { 353 int n = name.lastIndexOf('/'); 354 if (n > -1) 355 name = name.substring(0, n); 356 packages.add(name); 357 return name; 358 } 359 360 private void addName(String name) { 361 if (name == null) 362 return; 363 String p = getGroupKey(name); 364 if (current.containsKey(p)) { 365 current.put(p, current.get(p) + 1); 366 } else { 367 current.put(p, 1); 368 } 369 } 370 371 private void addNames(String [] names) { 372 for (int i = 0; names != null && i < names.length; i++) 373 addName(names[i]); 374 } 375 376 private void addDesc(String desc) { 377 addType(Type.getType(desc)); 378 } 379 380 private void addMethodDesc(String desc) { 381 addType(Type.getReturnType(desc)); 382 Type[] types = Type.getArgumentTypes(desc); 383 for (int i = 0; i < types.length; i++) 384 addType(types[i]); 385 } 386 387 private void addType(Type t) { 388 switch (t.getSort()) { 389 case Type.ARRAY: 390 addType(t.getElementType()); 391 break; 392 case Type.OBJECT: 393 addName(t.getClassName().replace('.', '/')); 394 break; 395 } 396 } 397 398 private void addSignature(String signature) { 399 if (signature != null) 400 new SignatureReader(signature).accept(this); 401 } 402 403 private void addTypeSignature(String signature) { 404 if (signature != null) 405 new SignatureReader(signature).acceptType(this); 406 } 407 } 408 | Popular Tags |