java assist asm_javassist和asm比较

Javassist提供源代码级别的API,使得在不熟悉Java字节码的情况下也能编辑类文件,适合AOP和运行时反射。ASM则注重性能和简单使用,直接操作字节码,适用于动态系统和高性能需求。两者对比,Javassist易用但速度较慢,ASM速度快但操作复杂。
摘要由CSDN通过智能技术生成

Javassist:

• Javassist (Java Programming Assistant) makes Java bytecode

manipulation simple. It is a class library for editing bytecodes in

Java; it enables Java programs to define a new class at runtime and

to modify a class file when the JVM loads it. Unlike other similar

bytecode editors

• Javassist provides two levels of API: source level and bytecode

level. If the users use the source-level API, they can edit a class

file without knowledge of the specifications of the Java bytecode.

The whole API is designed with only the vocabulary of the Java

language. You can even specify inserted bytecode in the form of

source text; Javassist compiles it on the fly. On the other hand,

the bytecode-level API allows the users to directly edit a class

file as other editors.

• Javassist lets you inspect, edit, and create Java binary

classes.

• Javassist isn’t the only library for working with bytecode, but

it does have one feature in particular that makes it a great

starting point for experimenting with classworking: you can use

Javassist to alter the bytecode of a Java class without actually

needing to learn anything about bytecode or the Java virtual

machine (JVM) architecture.

• Aspect Oriented Programming: Javassist can be a good tool for

adding new methods into a class and for inserting

before/after/around advice at the both caller and callee

sides.

• Reflection: One of applications of Javassist is runtime

reflection; Javassist enables Java programs to use a metaobject

that controls method calls on base-level objects. No specialized

compiler or virtual machine are needed.

• Javassist also provides lower-level API for directly editing a

class file. To use this level of API, you need detailed knowledge

of the Java bytecode and the class file format while this level of

API allows you any kind of modification of class files.

ASM:

• ASM is an all purpose Java bytecode manipulation and analysis

framework. It can be used to modify existing classes or dynamically

generate classes, directly in binary form. Provided common

transformations and analysis algorithms allow to easily assembling

custom complex transformations and code analysis tools.

• ASM offer similar functionality as other bytecode frameworks, but

it is focused on simplicity of use and performance. Because it was

designed and implemented to be as small and as fast as possible, it

makes it very attractive for using in dynamic systems*.

• ASM is a Java class manipulation tool designed to dynamically

generate and manipulate Java classes, which are useful techniques

to implement adaptable systems. ASM is based on a new approach,

compared to equivalent existing tools, which consists in using the

“visitor” design pattern without explicitly representing the

visited tree with objects. This new approach gives much better

performances than those of existing tools, for most of practical

needs.

Comparison between Javassist &

ASM:

• Javassist source level API is much easier to use than the actual

bytecode manipulation in ASM

• Javassist provides a higher level abstraction layer over complex

bytecode level operations. Javassist source level API requires very

less or no knowledge of actual bytecodes, so much easier

& faster to implement.

• Javassist uses reflection mechanism which makes it slower

compared to ASM which uses Classworking techniques at

runtime.

• Overall ASM is much faster & gives better

performance than Javassist. Javassist uses a simplified version of

Java source code, which it then compiles into bytecode. That makes

Javassist very easy to use, but it also limits the use of bytecode

to what can be expressed within the limits of the Javassist source

code.

• In conclusion, if someone needs easier way of dynamically

manipulating or creating Java classes Javassist API should be used

& where the performance is the key issue ASM

library should be used.

Table 1. Class construction times

Framework First

time Later times

Javassist 257 5.2

BCEL 473 5.5

ASM 62.4 1.1

The Table 1 results show that ASM does live up to its billing as

faster than the other frameworks, and this advantage applies both

to startup time and to repeated uses.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值