猿问

在方法中将一个字段替换为另一个字段

我正在尝试在方法主体中动态地将字段引用替换为另一个字段引用。bytebuddy有可能吗?


我要转换的类是这样的:


public class TestReplace {

    @replace

    int a = 1;

    int b = 2;


    public int printA() {

        return a;

    }


    public int printB() {

        return b;

    }

}

转换包括在访问它们的方法中替换标有@replace的变量。替换将由我将插入的变量完成。


已经转换的类应如下所示:


public class TestReplace {

    @replace

    int a = 1;

    int ___a = a * 10; // var inserted

    int b = 2;


    public int printA() {

        return ___a;

    }


    public int printB() {

        return b;

    }

}

我是这个主题的新手,希望您能给我任何帮助。


提前致谢


jeck猫
浏览 172回答 1
1回答

交互式爱情

是的,可以使用MemberSubstitution:MemberSubstitution.relaxed()   .field(ElementMatchers.named("a"))   .onRead()   .doReplaceWith(otherField)例如,您可以从使用AgentBuilderAPI定义的Java代理中应用此转换。您可能还需要从该代理定义替代字段。您可以使用来引用此字段FieldDescription.Latent。
随时随地看视频慕课网APP

相关分类

Java
我要回答