Java虚拟机

Java 字节码增强技术 2019-02-01

2019-02-02  本文已影响0人  watermountain

本文将介绍有哪些常见的字节码增强技术、字节码增强的实现方式、AOP实现的原理。

1. 字节码增强技术的应用场景:

    写日志、事务管理

    常见的字节码增强技术:

    1. Java 动态代理

        Java Proxy API 通过invoke方法拦截出来相应的代码逻辑。Proxy 是面向接口的,被代理的Class的所有方法调用都会通过反射调用invoke方法。

        缺点:性能开销大

    2.  Java 5 提供的Instrumentation API

        适应场景:适用于监控

        缺点:不适合处理灵活的代码逻辑

        Instrumentation API 不仅可以做字节码增强,还可以通过调用getObjectSize(Object o) 方法来计算一个对象的精确大小。

    3. ASM 

        ASM 是一个提供字节码解析和操作的框架。CGlib 框架是基于ASM 实现,而常用的框架Hibernate、Spring 是基于CGlib 实现 AOP的。

        ASM 对使用者屏蔽了整个类的字节码的长度、偏移量,能够灵活、方便地解析和操作字节码。主要提供 Core API 和Tree API。

        Core API 主要的类(接口):

            ClassVisitor、ClassAdapter、ClassReader、ClassWriter

        Tree API 主要的类(接口):

        工具类:

            TraceClassVisitor、CheckClassAdapter、ASMifier、Type

            TraceClassVisitor 能打印ClassWriter 提供的byte[] 字节数组。

            TraceClassVisitor 通过初始化一个ClassWriter 和一个Printer 对象来打印我们需要的字节流信息。方便比较类文件及分析字节码文件的结构。

2. 两种实现机制:

    (1) 通过创建原始类的一个子类(动态创建的类继承原来的类)。子类名以原始类名为前缀,以避免重名。Spring AOP 使用的就是这种

    (2) 直接修改原始类的字节码。类的跟踪过程中使用

3. 实现字节码增强要执行两个步骤:

    (1) 在内存中获取到原始的字节码, 然后通过一些开源的API 来修改它的byte[] 数组,得到一个新的byte[] 数组。

    (2) 将新的byte[] 数组加载到PermGen 区(即加载新的byte[] 数组或替换原始类的字节码)。

4. 使用较多的开源的字节码增强API:

    ASM、javassist、BCEL、SERP、CGLib(基于ASM )

5. 加载字节数组的方式:

    1. 基于Java 的instrument API (接口ClassFileTransformer 的transform方法)

byte[] transform( ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain     protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException

    2. 通过指定的ClassLoader  来完成

    FAQ: 这两种加载字节数组的方式的区别?

附录:

    ASM  是一种修改字节码本身的工具库,它实现的抽象层次是很低的,几乎接近于指令级别。例子中的操作都是基于指令和操作数的。

/**

* Visits a local variable instruction. A local variable instruction is an

* instruction that loads or stores the value of a local variable.

*

* @param opcode the opcode of the local variable instruction to be visited.

*        This opcode is either ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE,

*        LSTORE, FSTORE, DSTORE, ASTORE or RET.

* @param var the operand of the instruction to be visited. This operand is

*        the index of a local variable.

*/

void visitVarInsn(int opcode,int var);

LDC 指令将一个常量加载到操作数栈

/**

* Visits a LDC instruction.

*

* @param cst the constant to be loaded on the stack. This parameter must be

*        a non null {@link Integer}, a {@link Float}, a {@link Long}, a

*        {@link Double} a {@link String} (or a {@link Type} for

*        <tt>.class</tt>constants, for classes whose version is 49.0 or

*        more).

*/

void visitLdcInsn(Object cst);

/**

* Visits a field instruction. A field instruction is an instruction that

* loads or stores the value of a field of an object.

*

* @param opcode the opcode of the type instruction to be visited. This

*        opcode is either GETSTATIC, PUTSTATIC, GETFIELD or PUTFIELD.

* @param owner the internal name of the field's owner class (see {@link

*        Type#getInternalName() getInternalName}).

* @param name the field's name.

* @param desc the field's descriptor (see {@link Type Type}).

*/

void visitFieldInsn(int opcode, String owner, String name, String desc);

/**

* Visits a method instruction. A method instruction is an instruction that

* invokes a method.

*

* @param opcode the opcode of the type instruction to be visited. This

*        opcode is either INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC,

*        INVOKEINTERFACE or INVOKEDYNAMIC.

* @param owner the internal name of the method's owner class (see {@link

*        Type#getInternalName() getInternalName})

*        or {@link org.objectweb.asm.Opcodes#INVOKEDYNAMIC_OWNER}.

* @param name the method's name.

* @param desc the method's descriptor (see {@link Type Type}).

*/

void visitMethodInsn(int opcode, String owner, String name, String desc);

上一篇下一篇

猜你喜欢

热点阅读