ByteBuddy - 创建 getter/setter 的接口



>我正在尝试从List生成一个Intgerface,其中元素表示"get"方法。 接口是无异常生成的,但是当我反思性地尝试获取动态类型接口的方法时,没有列出任何方法。 这似乎是一件简单的事情

ByteBuddy bb = new ByteBuddy();
Builder<?> bbb = bb.makeInterface().merge(Visibility.PUBLIC);
for (K name : propertyNames) {
    String get = "get" + name.toString();
    String set = "set" + name.toString();
    bbb.defineMethod(get, String.class, Visibility.PUBLIC); //** .withoutCode()
    bbb.defineMethod(set, String.class, Visibility.PUBLIC).withParameter(String.class); //** .withoutCode()
}
DynamicType.Unloaded unloadedType = bbb.name(NAME).make();
DYNAMIC_TYPE = unloadedType.load(POJOFactory.class.getClassLoader()).getLoaded();

在过去的一周里,我在谷歌上搜索了使用ByteBuddy创建接口的参考资料,但根本没有任何参考资料。

谢谢

Byte Buddy的构建器API是完全不可变的。您必须始终对返回值进行操作。所有呼叫都没有副作用,因此:

builder.<something>

没有效果。正确的方法是:

builder = builder.<something>

最新更新