ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Introduction to the ASM 2.0 Bytecode Framework
Pages: 1, 2, 3, 4, 5, 6, 7, 8

Now we can use DependencyVisitor to collect dependencies from the entire .jar file. For example:

  DependencyVisitor v = new DependencyVisitor();
  ZipFile f = new ZipFile(jarName);
  Enumeration<? extends ZipEntry> en = f.entries();
  while(en.hasMoreElements()) {
    ZipEntry e = en.nextElement();
    String name = e.getName();
    if(name.endsWith(".class")) {
      ClassReader cr = 
          new ClassReader(f.getInputStream(e));
      cr.accept(v, false);

The collected information can be represented in many different ways. One can build dependency trees and calculate some metrics, or create some visualizations. For example, Figure 5 shows how ant.1.6.5.jar looks in a visualization I built on top of the collected information using some simple Java2D code. The following diagram shows packages from the input .jar on a horizontal axis and external dependencies on a vertical axis. The darker a box's color is, the more times the package is referenced.

Dependencies in ant.1.6.5.jar as discovered with ASM
Figure 5. Dependencies in ant.1.6.5.jar, as discovered with ASM

The complete code of this tool will be included into the next ASM release. It can be also obtained from ASM CVS.

Changes in the ASM 2.0 API Since ASM 1.x

You can skip this section if you haven't used ASM 1.x.

The major structural change in ASM 2.0 is that all J2SE 5.0 features are built into the ASM visitor/filter event flow. So the new API allows you to deal with generics and annotations in a much more lightweight and semantically natural way. Instead of explicitly creating annotation attribute instances, we have generics and annotation data within the event flow. For example, in ASM 1.x, the ClassVisitor interface used the following method:

  CodeVisitor visitMethod(int access, String name, 
      String desc, String[] exceptions, 
      Attribute attrs);

This has been split into several methods in ASM 2.0:

  MethodVisitor visitMethod(int access, 
      String name, String desc, String signature,
      String[] exceptions)

  AnnotationVisitor visitAnnotation(String desc,
      boolean visible)

  void visitAttribute(Attribute attr)

In the 1.x API, in order to define generics info, you'd have to create specific instances of the SignatureAttribute, and to define annotations, you'd need instances of the RuntimeInvisibleAnnotations, RuntimeInvisibleParameterAnnotations, RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations, and AnnotationDefault. Then you'd put these instances into the attrs parameter of the appropriate visit method.

In ASM 2.0, a new signature parameter has been added to represent generics info. The new AnnotationVisitor interface is used to handle all annotations. There is no need to create an attrs collection, and annotation data is more strictly typed. However, when migrating existing code, especially when "adapter" classes have been used; it is necessary to be careful and make sure that all methods overwritten from the adapter are updated to new signatures, because the compiler will raise no warnings.

Pages: 1, 2, 3, 4, 5, 6, 7, 8

Next Pagearrow