Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ByteBuddy';成员替换类_Java_Byte Buddy - Fatal编程技术网

Java 使用ByteBuddy';成员替换类

Java 使用ByteBuddy';成员替换类,java,byte-buddy,Java,Byte Buddy,我使用ByteBuddy将一个字段引用替换为另一个字段引用到类的方法中。在另一个问题中,有人建议我使用类net.bytebuddy.asm.MemberSubstitution。我一直在寻找关于如何在MiJava代理中使用它的示例,但a找不到任何示例 我想使用以下代码: MemberSubstitution.relaxed() .field(ElementMatchers.named("oneField")) .onRead() .replaceWith(otherField);

我使用ByteBuddy将一个字段引用替换为另一个字段引用到类的方法中。在另一个问题中,有人建议我使用类net.bytebuddy.asm.MemberSubstitution。我一直在寻找关于如何在MiJava代理中使用它的示例,但a找不到任何示例

我想使用以下代码:

MemberSubstitution.relaxed()
  .field(ElementMatchers.named("oneField"))
  .onRead()
  .replaceWith(otherField);
我的代理人是:

public class MyAgent {

    public static void premain(String arg, Instrumentation inst)
            throws Exception {
        File temp = Files.createTempDirectory("tmp").toFile();
        ClassInjector.UsingInstrumentation.of(temp,
                ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, inst)
                .inject(Collections.singletonMap(
                        new TypeDescription.ForLoadedType(MyInterceptor.class),
                        ClassFileLocator.ForClassLoader.read(MyInterceptor.class).resolve()));

        new AgentBuilder.Default()
                .enableBootstrapInjection(inst, temp)
                .type(isAnnotatedWith(CriptoObject.class))
                .transform(new AgentBuilder.Transformer() {
                    @Override
                    public DynamicType.Builder<?> transform(
                            DynamicType.Builder<?> builder,
                            TypeDescription typeDescription,
                            ClassLoader classLoader,
                            JavaModule module) {
                        return builder.method(any())
                                .intercept(MethodDelegation.to(MyInterceptor.class)
                                .andThen(SuperMethodCall.INSTANCE));
                    }
                }).installOn(inst);
    }
}
我不知道在哪里放置MemberSubstitution代码来转换特定方法

拜托,有人能帮我吗


提前感谢

这是一位访客,他可以在您通过以下方式注册的地方转换现有代码,而无需添加任何内容:

builder = builder.visit(MemberSubstitution.relaxed()
  .field(ElementMatchers.named("oneField"))
  .onRead()
  .replaceWith(otherField)
  .on(method(any()));

你好,拉斐尔。对不起,我不明白。如果我试图在代码中实现它,builder.method(any())将返回ImplementationDefinition的一个实例,并且没有方法visit(MethodVisitorWrapper visitor)。你能告诉我我有没有遗漏什么吗?对不起,遗漏了。我终于更新了代码。我的错。
builder = builder.visit(MemberSubstitution.relaxed()
  .field(ElementMatchers.named("oneField"))
  .onRead()
  .replaceWith(otherField)
  .on(method(any()));