Introduction to the ASM 2.0 Bytecode Framework

Java features such as dynamic class loading and reflection make
it a dynamic language. However, in many cases, reflection is not
sufficient, and developers need to generate bytecode from non-Java
source code, such as scripting languages like Groovy (JSR-241) or BeanShell
(JSR-274), or
from metadata such as an OR-mapping configuration. When working with
existing classes, and especially when original Java sources are not
available, some tools may need to do a static analysis of the
interdependencies or even method behavior in order to produce test
coverage or metrics, or to detect bugs and anti-patterns. New features
added to into Java 5, such as annotations and generics, affected
bytecode structure and require special attention from bytecode
manipulation tools to maintain good performance. This article will
give an overview of one of the smallest and fastest bytecode
manipulation frameworks available for Java.

Framework Structure

The ASM bytecode
manipulation framework is written in Java and uses a visitor-based
approach to generate bytecode and drive transformations of existing
classes. It allows developers to avoid dealing directly with a
class constant pool and offsets within method bytecode, thus hiding
bytecode complexity from the developer and providing better
performance, compared to other tools such as BCEL, SERP, or Javassist.

ASM is divided into several packages that allow flexible
bundling. The packaging arrangement is shown in Figure 1.

Figure 1. Arrangement of ASM packages

The Core package provides an API to read, write, and
transform Java bytecode and defines a foundation for the other
packages. This package is sufficient to generate Java bytecode and
to implement the majority of bytecode transformations.

The Commons package (added in ASM 2.0) provides several
commonly used bytecode transformations and adapters
to simplify bytecode generation.

The Util package contains several helper classes and simple
bytecode verifiers that can help in development or testing.

The XML package provides an adapter to convert bytecode
structures to and from XML, and SAX-compliant
adapters that allow the use of XSLT to define bytecode transformations.

The next few sections will give an introduction to the Core
package of the ASM framework. To get a better understanding of
the organization of this package, you have to have some basic
understanding of the bytecode structures that are defined in the
JVM
specification. Here is a high-level diagram of the class file
format ([*] marks repeatable structures).