[英]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.