`
mineral
  • 浏览: 288204 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[10Minutes]Introduction to the ASM 2.0 Bytecode Framework

阅读更多




Introduction 
to the ASM 2.0 Bytecode Framework Introduction to the ASM 2.0 Bytecode Framework

by Eugene Kuleshov
08/17/2005

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.

Arrangement of ASM packages
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 Tree package provides in-memory representation of Java bytecode.
  • The Analysis package provides basic data-flow analyses and type-checking algorithms for Java method bytecode stored in structures from the tree package.
  • 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. 

Ref:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics