小编Dan*_*sky的帖子

使用ASM选择和修改`if`语句

我想if在特定行上更新现有类中的语句而不更改整个方法.

这是目标代码(类,方法和一些代码的名称已更改,因为它们不相关):

public class Target extends Something {
   public Target(){
        super();
        //some code...
   }

    public Result targetMethod(Data firstPar, Data secondPar){
        if(someStatement()) {
            return Result.FAIL;
        } else {
            if(firstPar.check()){ //here is the line I want to change
                 firstPar.doSomething()
            }
            secondPar.doSomething();
            return Result.SUCCESS;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中我想改成if(firstPar.check())这样的东西:

if(firstPar.check() && !Utilities.someOtherCheck(firstPar, secondPar))
Run Code Online (Sandbox Code Playgroud)

这是我试图解决这个问题的方法:

ClassNode node = new ClassNode();
ClassReader reader = new ClassReader(bytes); //bytes - original target class
reader.accept(node, 0);
ClassWriter cw = new ClassWriter(0);
node.accept(cw);
MethodVisitor visitor = …
Run Code Online (Sandbox Code Playgroud)

java bytecode-manipulation java-bytecode-asm

3
推荐指数
1
解决办法
1381
查看次数