简体   繁体   English

Byte Buddy - 处理生成的类中的循环引用

[英]Byte Buddy - Handling cyclic references in generated classes

I'm trying to generate runtime wrappers around classes in some class graph, but I don't know how to handle the situation when there is a cycle in the graph. 我试图在一些类图中围绕类生成运行时包装器,但我不知道如何在图中有一个循环时处理这种情况。 Imagine there's a class A that has a field of type B, but type B has a field of type A. I want to generate classes A' and B' so that class A' has a field of type B' and B' has a field of type A'. 想象一下,有一个类A有一个B类型的字段,但是类型B有一个类型为A的字段。我想生成类A'和B',以便类A'有一个类型B'的字段,B'有一个A'字段。 In Byte Buddy the method "defineField" can receive a parameter of type TypeDefinition. 在Byte Buddy中,方法“defineField”可以接收TypeDefinition类型的参数。 I think there must be a way to define TypeDefinition for a type that isn't defined yet, but I cannot find it yet. 我认为必须有一种方法来为尚未定义的类型定义TypeDefinition,但我还没有找到它。

You can define such a field with TypeDescription.Latent but this is at your own risk. 您可以使用TypeDescription.Latent定义此类字段,但这需要您自担风险。 Byte Buddy treats this as a specific, unvalidated use case as type redefinition is quite complex considering the interdepencdency. Byte Buddy将此视为一个特定的,未经验证的用例,因为考虑到相互依赖性,类型重定义非常复杂。

Make sure that you do not load a type before defining the latent type properly. 在正确定义潜在类型之前,请确保不加载类型。 Also, you probably need to combine the two types into a single DynamicType.Unloaded via the include method. 此外,您可能需要通过include方法DynamicType.Unloaded两种类型组合成一个DynamicType.Unloaded Furthermore, you should note that the ClassLoadingStrategy.Default.INJECT strategy might not work if the VM in question validates the injected type eagerly. 此外,您应该注意,如果有问题的VM急切地验证注入的类型,则ClassLoadingStrategy.Default.INJECT策略可能不起作用。 As you can only inject one type at a time, in circular type definitions, at least one type will always be missing at the time of injection the first type. 由于您一次只能注入一种类型,因此在循环类型定义中,注入第一种类型时至少会丢失一种类型。 Consider using the ClassLoadingStrategy.Default.WRAPPER strategy which does not suffer this limitation. 考虑使用不受此限制的ClassLoadingStrategy.Default.WRAPPER策略。

This said, you can do this without any problems but you can experience VerifierError s what is something that you are normally promised to not experience. 这就是说,你可以毫无问题地做到这一点,但是你可以体验到VerifierError这是你通常承诺不会遇到的事情。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM