在 ByteBuddy 中做循环类型

问题描述

我正在尝试使用字节伙伴动态创建这样的循环类:

class A{
 B b;
}
class B{
  A a;
}

我看过这个 example 并编写了这段代码

public static void main(String[] args) throws Exception {

    final ByteBuddy bb = new ByteBuddy();

    TypeDescription.Generic g1 = TypeDescription.Generic.Builder.rawType(Object.class).build();
    final InstrumentedType typeDescrB = InstrumentedType.Default.of("B",g1,Modifier.PUBLIC);

    final DynamicType.Unloaded<Object> madeA = bb
            .subclass(Object.class)
            .name("A")
            .defineField("theB",typeDescrB,Modifier.PUBLIC).make();

    final DynamicType.Unloaded<Object> madeB = bb.subclass(Object.class)
            .name("B")
            .defineField("theA",madeA.getTypeDescription()).make();

    Class a = madeA.include(madeB)
            .load(Test.class.getClassLoader(),ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded();

    for (Field f : a.getFields()) {
        System.out.println(f + "|" + f.getType().getName());
    }
    System.out.println("====");
    for (Field f : a.getField("theB").getType().getFields()) {
        System.out.println(f + "|" + f.getType().getName());
    }
}

运行代码后,我得到了这个结果

public B A.theB|B
====
Process finished with exit code 0

这意味着 B 类不包含字段 a。有人知道是什么问题吗?

解决方法

我猜您所看到的只是代码中的一个错误。

使用此代码:

final DynamicType.Unloaded<Object> madeB = bb.subclass(Object.class)
    .name("B")
    .defineField("theA",madeA.getTypeDescription()).make();

...您已将该字段设为非公开(请注意,您为其他字段指定了 PUBLIC,但未在此处指定)。然后用这个代码:

for (Field f : a.getField("theB").getType().getFields()) {
    System.out.println(f + "|" + f.getType().getName());
}

……您只要求公共字段。也许你的意思是getDeclaredFields()