ASM + JASMIN combination for java class file editing in bytecode level

本文介绍使用ASM反编译工具获取指定Java类文件的字节码,并利用JASMIN工具将其重新编译为Java类的过程。首先通过ASM获得字节码源文件,然后根据JASMIN语法对源文件进行重写,最后编译成Java类文件。文章还提供了HelloWorld案例的JASMIN源码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

By using this combination, you will find that first we'll use ASM decompile the specified class and then rewite the specified class in JASMIN grammar, then compile this assemble source code file into java class file, first we'll show you the basic JASMIN grammar by a sample:

Typical Jasmin class source code:(HelloWorld case)

-------------------------------------------------------

.class public HelloWorld

.super java/lang/Object

.method public static main([Ljava/lang/String;)V

.limit stack 2

.limit locals 1

getstatic java/lang/System/out Ljava/io/PrintStream;

ldc "Hello World."

invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

return

.end method

-------------------------------------------------------

If you want to get more knowledge for Jasmin, please see http://jasmin.sourceforge.net/instructions.html.

Use Asm to get assemble source code(command line set)

----------------------------------------------------------------------------

SET CP="E:/Resource/ASM/asm3.3lib/asm-3.3.jar;E:/Resource/ASM/asm3.3lib/asm-util-3.3.jar"

ATTENTION: DUE TO THERE IS A DIFFERENCE (IN GRAMMAR) BETWEEN ASM AND JASMIN, WE HAVE TO REWRITE OR RE-ORGANIZE THE SOURCE AND COMPILE THEM AGAIN.

SET CP=%CP%;"E:/Resource/Test"

java -classpath %CP% org.objectweb.asm.util.TraceClassVisitor HelloWorld > HelloWorld.asm

----------------------------------------------------------------------------

Finally Run java class file in a source command:

----------------------------------------------------------------------------

SET CP="E:/Resource/Test"

java -cp %CP% examples.NewWorld

Use Jasmin to compile the specified assemble source code:

----------------------------------------------------------------------------

set CP=E:/Resource/Jasmin/jasmin-2.4/lib/ant.jar;

set CP=%CP%;E:/Resource/Jasmin/jasmin-2.4/lib/ant-launcher.jar;

set CP=%CP%;E:/Resource/Jasmin/jasmin-2.4/lib/java_cup.jar

java -cp %CP% -jar E:/Resource/Jasmin/jasmin-2.4/jasmin.jar examples/NewWorld.j

-----------------------------------------------------------------------

ATTENTION: DUE TO THERE IS A DIFFERENCE BETWEEN THE ASSEMBLE CODE DECOMPILED BY ASM AND THE SOURCE FOR JASMIN COMPILING, WE WILL HAVE TO REWRITE OR RE-ORGANIZE THESE SOURCE.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值