小编Chr*_*ann的帖子

如何使用ASM使用初始化程序添加静态最终字段?

我想使用ASM将静态最终字段添加到.class文件中,源文件是

public class Example {

    public Example(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    private final int code;

}
Run Code Online (Sandbox Code Playgroud)

并且生成的反编译类应该是这样的:

public class Example {

    public static final Example FIRST = new Example(1);

    public static final Example SECOND = new Example(2);

    public Example(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    private final int code;

}
Run Code Online (Sandbox Code Playgroud)

作为结论,我想使用ASM将FIRST和SECOND常量添加到.class文件中,我该怎么办?

java assembly bytecode bytecode-manipulation java-bytecode-asm

5
推荐指数
1
解决办法
4234
查看次数