简体   繁体   English

ByteBuddy:如何在另一个类中使用自定义方法声明一个类

[英]ByteBuddy: How to declare a class with a custom method inside another class

I'm trying to dynamically create a class which extends a class ServerPing, inside this class there is a static class called Serializer, I want to override its method "a" and returns my own JsonElement. 我正在尝试动态创建一个扩展类ServerPing的类,在这个类中有一个名为Serializer的静态类,我想覆盖它的方法“a”并返回我自己的JsonElement。 The problem is that I don't know how to edit a static class inside another class using bytebuddy. 问题是我不知道如何使用bytebuddy编辑另一个类中的静态类。

Here is what it could look like (but defineClassInside doesn't exist): 这是它的样子(但是defineClassInside不存在):

        Class<?> serverPingSerializerClone = new ByteBuddy()
                .subclass(serverPingClass)
                .defineClassInside("Serializer",
                        new ByteBuddy().subclass(ServerPing.Serializer.class)
                                .method(ElementMatchers.named("a")
                                        .and(ElementMatchers.returns(JsonElement.class)
                                                .and(ElementMatchers.takesArguments(3))))
                                .intercept(FixedValue.value(exampleResponse))
                                .make())
                .make()
                .load(Core.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();```

At the byte code level, an inner class Bar defined inside Foo is nothing but a class named Foo$Bar with some additional meta data. 在字节代码级别,Foo中定义的内部类Bar只是一个名为Foo $ Bar的类,带有一些额外的元数据。

You can just treat the inner/nested class like any other class and subclass it. 您可以像处理任何其他类一样处理内部/嵌套类,并将其子类化。 If you need to add inner class meta data, Byte Buddy has DSL steps to edit/add such information, eg innerTypeOf. 如果您需要添加内部类元数据,Byte Buddy有DSL步骤来编辑/添加此类信息,例如innerTypeOf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在现有类中定义新方法并使用 bytebuddy 在同一类中的现有方法中添加对它的调用? - How to define a new method inside an existing class and add a call to it in an existing method inside the same class using bytebuddy? ByteBuddy 如何在 Android 中创建另一个 ByteBuddy 创建的类的子类? - ByteBuddy how to create a subclass of another ByteBuddy created Class in Android? 如何使用 ByteBuddy 读取 Java 类方法注释 - How to read a Java class method annotation with ByteBuddy ByteBuddy拦截对方法字节码内特定类实例的调用 - ByteBuddy intercept the calls toward specific class instances inside the bytecode of a method 如何调用另一个类内部的类的方法? - How to call a method of a class that is inside another class? 如何在ByteBuddy中向类添加字段并在方法拦截器中设置/获取该值 - How to add a field to a class in ByteBuddy and set / get that value in a method interceptor 如何使用ByteBuddy委托拦截bootstrap类方法 - How to use ByteBuddy delegation for intercepting bootstrap class method 如何在来自 json 的另一个 object 内部的数据 class 和 object 中声明? - how to declare in data class an object inside of another object from json? 使用 Bytebuddy 动态创建类和方法 - Dynamically create class and method using Bytebuddy Bytebuddy 方法拦截器不适用于自定义类 - Bytebuddy method interceptor not working with self defined class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM