没有合适的资源?快使用搜索试试~ 我知道了~
首页ASM4开发手册:英文原版与CGLIB字节码库解析
ASM4.0是一款专为Java字节码操作而设计的Java字节码工程库,由Eric Bruneton开发并享有版权。该手册提供了深入的细节,展示了ASM在CGlib动态代理框架中的核心作用。CGlib是一个广泛使用的轻量级的Java代码生成库,它利用ASM对Java类的字节码进行分析和修改,实现了动态代理的功能,即在运行时动态创建和执行代理类。 ASM的核心特性包括低级别的字节码操作接口,允许开发者直接操作类文件的二进制表示,如方法调用、字段访问、指令插入和删除等。这使得在不改变源代码的情况下,能够实现诸如AOP(面向切面编程)中的动态代理功能,例如创建动态代理对象来拦截目标方法的执行,实现事务管理、日志记录等功能。 在使用ASM4.0时,开发者需要注意以下几点: 1. 版权与许可:在使用ASM的源代码(例如XML或LaTeX格式)时,必须保留版权声明、条件列表以及免责声明,并确保未经作者明确书面许可,不得将作者的名字用于推广基于此文档的产品。 2. 使用限制:编译后的文档(如PDF、PostScript、HTML等)同样需要包含版权声明和免责声明,且不能暗示或保证其适用于特定目的的适销性或适合性。 3. 责任声明:作者提供的文档是"原样"提供的,不附带任何形式的明示或默示保证,包括但不限于适销性和特定用途的适用性。因此,在使用ASM时,用户需要自行承担风险,理解并接受可能存在的局限性。 ASM4.0开发手册为Java开发者提供了一把强大的字节码操作工具,通过理解并掌握其原理,可以实现高效、灵活的动态代理机制,提升软件的灵活性和可扩展性。然而,使用时务必遵守相关条款,尊重作者权益。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/11161607/bg10.jpg)
2. Classes
• A compiled class does not contain comments, of course, but can contain
class, field, method and code attributes that can be used to associate
additional information to these elements. Since the introduction of an-
notations in Java 5, which can be used for the same purpose, attributes
have become mostly useless.
• A compiled class does not contain a package and import section, so all
type names must be fully qualified.
Another very important structural difference is that a compiled class contains
a constant pool section. This pool is an array containing all the numeric, string
and type constants that appear in the class. These constants are defined only
once, in the constant pool section, and are referenced by their index in all other
sections of the class file. Hopefully ASM hides all the details related to the
constant pool, so you will not have to bother about it. Figure 2.1 summarizes
the overall structure of a compiled class. The exact structure is described in
the Java Virtual Machine Specification, section 4.
Modifiers, name, super class, interfaces
Constant pool: numeric, string and type constants
Source file name (optional)
Enclosing class reference
Annotation*
Attribute*
Inner class* Name
Field* Modifiers, name, type
Annotation*
Attribute*
Method* Modifiers, name, return and parameter types
Annotation*
Attribute*
Compiled code
Figure 2.1.: Overall structure of a compiled class (* means zero or more)
Another important difference is that Java types are represented differently in
compiled and source classes. The next sections explain their representation in
compiled classes.
10
![](https://csdnimg.cn/release/download_crawler_static/11161607/bg11.jpg)
2.1. Structure
2.1.2. Internal names
In many situations a type is constrained to be a class or interface type. For
instance the super class of a class, the interfaces implemented by a class, or
the exceptions thrown by a method cannot be primitive types or array types,
and are necessarily class or interface types. These types are represented in
compiled classes with internal names. The internal name of a class is just the
fully qualified name of this class, where dots are replaced with slashes. For
example the internal name of String is java/lang/String.
2.1.3. Type descriptors
Internal names are used only for types that are constrained to be class or
interface types. In all other situations, such as field types, Java types are
represented in compiled classes with type descriptors (see Figure 2.2).
Java type Type descriptor
boolean Z
char C
byte B
short S
int I
float F
long J
double D
Object Ljava/lang/Object;
int[] [I
Object[][] [[Ljava/lang/Object;
Figure 2.2.: Type descriptors of some Java types
The descriptors of the primitive types are single characters: Z for boolean, C
for char, B for byte, S for short, I for int, F for float, J for long and D
for double. The descriptor of a class type is the internal name of this class,
preceded by L and followed by a semicolon. For instance the type descriptor
of String is Ljava/lang/String;. Finally the descriptor of an array type is
a square bracket followed by the descriptor of the array element type.
11
![](https://csdnimg.cn/release/download_crawler_static/11161607/bg12.jpg)
2. Classes
2.1.4. Method descriptors
A method descriptor is a list of type descriptors that describe the parameter
types and the return type of a method, in a single string. A method descriptor
starts with a left parenthesis, followed by the type descriptors of each formal
parameter, followed by a right parenthesis, followed by the type descriptor of
the return type, or V if the method returns void (a method descriptor does
not contain the method’s name or the argument names).
Method declaration in source file Method descriptor
void m(int i, float f) (IF)V
int m(Object o) (Ljava/lang/Object;)I
int[] m(int i, String s) (ILjava/lang/String;)[I
Object m(int[] i) ([I)Ljava/lang/Object;
Figure 2.3.: Sample method descriptors
Once you know how type descriptors work, understanding method descriptors
is easy. For instance (I)I describes a method that takes one argument of type
int, and returns an int. Figure 2.3 gives several method descriptor examples.
2.2. Interfaces and components
2.2.1. Presentation
The ASM API for generating and transforming compiled classes is based on
the ClassVisitor abstract class (see Figure 2.4). Each method in this class
corresponds to the class file structure section of the same name (see Figure
2.1). Simple sections are visited with a single method call whose arguments
describe their content, and which returns void. Sections whose content can be
of arbitrary length and complexity are visited with a initial method call that
returns an auxiliary visitor class. This is the case of the visitAnnotation,
visitField and visitMethod methods, which return an AnnotationVisitor,
a FieldVisitor and a MethodVisitor respectively.
The same principles are used recursively for these auxiliary classes. For ex-
ample each method in the FieldVisitor abstract class (see Figure 2.5) corre-
sponds to the class file sub structure of the same name, and visitAnnotation
12
![](https://csdnimg.cn/release/download_crawler_static/11161607/bg13.jpg)
2.2. Interfaces and components
public abstract class ClassVisitor {
public ClassVisitor(int api);
public ClassVisitor(int api, ClassVisitor cv);
public void visit(int version, int access, String name,
String signature, String superName, String[] interfaces);
public void visitSource(String source, String debug);
public void visitOuterClass(String owner, String name, String desc);
AnnotationVisitor visitAnnotation(String desc, boolean visible);
public void visitAttribute(Attribute attr);
public void visitInnerClass(String name, String outerName,
String innerName, int access);
public FieldVisitor visitField(int access, String name, String desc,
String signature, Object value);
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions);
void visitEnd();
}
Figure 2.4.: The ClassVisitor class
returns an auxiliary AnnotationVisitor, as in ClassVisitor. The creation
and usage of these auxiliary visitors is explained in the next chapters: in-
deed this chapter is restricted to simple problems that can be solved with the
ClassVisitor class alone.
public abstract class FieldVisitor {
public FieldVisitor(int api);
public FieldVisitor(int api, FieldVisitor fv);
public AnnotationVisitor visitAnnotation(String desc, boolean visible);
public void visitAttribute(Attribute attr);
public void visitEnd();
}
Figure 2.5.: The FieldVisitor class
The methods of the ClassVisitor class must be called in the following order,
specified in the Javadoc of this class:
visit visitSource? visitOuterClass? ( visitAnnotation | visitAttribute )*
( visitInnerClass | visitField | visitMethod )*
visitEnd
This means that visit must be called first, followed by at most one call to
visitSource, followed by at most one call to visitOuterClass, followed by
13
![](https://csdnimg.cn/release/download_crawler_static/11161607/bg14.jpg)
2. Classes
any number of calls in any order to visitAnnotation and visitAttribute,
followed by any number of calls in any order to visitInnerClass, visitField
and visitMethod, and terminated by a single call to visitEnd.
ASM provides three core components based on the ClassVisitor API to
generate and transform classes:
• The ClassReader class parses a compiled class given as a byte array,
and calls the corresponding visitXxx methods on the ClassVisitor
instance passed as argument to its accept method. It can be seen as an
event producer.
• The ClassWriter class is a subclass of the ClassVisitor abstract class
that builds compiled classes directly in binary form. It produces as
output a byte array containing the compiled class, which can be retrieved
with the toByteArray method. It can be seen as an event consumer.
• The ClassVisitor class delegates all the method calls it receives to
another ClassVisitor instance. It can be seen as an event filter.
The next sections show with concrete examples how these components can be
used to generate and transform classes.
2.2.2. Parsing classes
The only required component to parse an existing class is the ClassReader
component. Let’s take an example to illustrate this. Suppose that we would
like to print the content of a class, in a similar way as the javap tool. The first
step is to write a subclass of the ClassVisitor class that prints information
about the classes it visits. Here is a possible, overly simplified implementation:
public class ClassPrinter extends ClassVisitor {
public ClassPrinter() {
super(ASM4);
}
public void visit(int version, int access, String name,
String signature, String superName, String[] interfaces) {
System.out.println(name + " extends " + superName + " {");
}
public void visitSource(String source, String debug) {
}
public void visitOuterClass(String owner, String name, String desc) {
}
public AnnotationVisitor visitAnnotation(String desc,
boolean visible) {
14
剩余153页未读,继续阅读
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/6d777e0d77b34cc886bd9372e6f8c57b_u012132050.jpg!1)
alwynxu
- 粉丝: 3
- 资源: 4
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- Vue实现iOS原生Picker组件:详细解析与实现思路
- Arduino蓝牙小车:参数调试与功能控制
- 百度Java面试精华:200页精选资源涵盖核心知识点
- Swift使用CoreData填坑指南:CoreData在Swift 3.0的变化
- 微距离无线充电器创新设计及其实验探索
- MTK Android平台开发全攻略:44步详解流程
- RecyclerView全面解析:替代ListView的新选择
- Android开发:自动适配中英文键盘解决方案
- Android调用WebService接口教程
- Android开发:BitmapUtil图片处理全解析与实例
- Android多线程断点续传实现详解
- PCA算法在人脸识别会议签到系统中的应用
- EventBus 3.0:Android事件总线详解与实战应用
- Android FileUtil:全面解析文件操作实用技巧与实例
- RecyclerView添加头部和尾部实战教程
- Android实现微博滑动固定顶部栏实战与优化
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)