好友:使用循环类型生成类
我试图用循环类依赖关系生成类,类似于这个问题:Byte Buddy - 处理生成类中的循环引用
作为一个最简单的例子,我想要生成的类有这样的依赖关系:
//class A depends on class B, and vice-versa
final class A { B theB; }
final class B { A theA; }
上面链接中接受的答案并没有为我提供足够的信息来使其发挥作用。 这是我试过的:
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.jar.asm.Opcodes;
public class ByteBuddyHello {
public static void main(String[] args) {
try {
final ByteBuddy bb = new ByteBuddy();
final TypeDescription.Latent typeDescrA = new TypeDescription.Latent("A", 0, null, null);
final TypeDescription.Latent typeDescrB = new TypeDescription.Latent("B", 0, null, null);
final DynamicType.Unloaded<Object> madeA = bb
.subclass(Object.class)
.name("A")
.defineField("theB", typeDescrB, Opcodes.ACC_PUBLIC)
.make(); // exception thrown here!
final DynamicType.Unloaded<Object> madeB = bb.subclass(Object.class)
.name("B")
.defineField("theA", typeDescrA, Opcodes.ACC_PUBLIC)
.make();
Object a = madeA
.include(madeB)
.load(ByteBuddyHello.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded().newInstance();
System.out.println(a.toString());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
当我运行这个时,我Exception in thread "main" java.lang.IllegalStateException: Cannot resolve declared type of a latent type description: class B
得到Exception in thread "main" java.lang.IllegalStateException: Cannot resolve declared type of a latent type description: class B
标记行中的Exception in thread "main" java.lang.IllegalStateException: Cannot resolve declared type of a latent type description: class B
上面提到的问题的答案说:“确保在定义潜在类型之前不加载类型”,并且我猜测这可能是我的问题。 我不知道如何定义一个潜在类型:-(
编辑:使A
和B
高于最终(因为这将是理想的解决方案)
编辑:添加堆栈跟踪
Exception in thread "main" java.lang.IllegalStateException: Cannot resolve declared type of a latent type description: class B
at net.bytebuddy.description.type.TypeDescription$Latent.getDeclaringType(TypeDescription.java:7613)
at net.bytebuddy.description.type.TypeDescription$AbstractBase.getSegmentCount(TypeDescription.java:6833)
at net.bytebuddy.implementation.attribute.AnnotationAppender$ForTypeAnnotations.onNonGenericType(AnnotationAppender.java:617)
at net.bytebuddy.implementation.attribute.AnnotationAppender$ForTypeAnnotations.onNonGenericType(AnnotationAppender.java:333)
at net.bytebuddy.description.type.TypeDescription$Generic$OfNonGenericType.accept(TypeDescription.java:3364)
at net.bytebuddy.implementation.attribute.FieldAttributeAppender$ForInstrumentedField.apply(FieldAttributeAppender.java:122)
at net.bytebuddy.dynamic.scaffold.TypeWriter$FieldPool$Record$ForExplicitField.apply(TypeWriter.java:270)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForCreation.create(TypeWriter.java:4156)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:1633)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:174)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:155)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:2559)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:2661)
at my.package.playground.ByteBuddyHello.main(ByteBuddyHello.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
这是Byte Buddy中的一个错误; 类型注释的解析器需要知道任何类型描述的深度,并因此解析任何类型描述的类型路径。 对于潜在类型,此深度始终为0,但默认实现应用更复杂的解决方案。
这将在下一个版本中解决。 同时,将潜类型描述子类并覆盖返回0的方法。
我决定不更改TypeDescription.Latent
类型,而是使InstrumentedType.Default
实现更易于访问。 使用后一种类型,可以让你定义循环类型的特征,这对任何用户都是可见的。 这样,您可以例如指定现有的字段和方法,如果您想定义针对此功能进行验证的Implementation
。
这是一个可行的解决方案,遵循公认的答案:
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.modifier.ModifierContributor;
import net.bytebuddy.description.modifier.TypeManifestation;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.jar.asm.Opcodes;
import java.io.File;
import java.io.IOException;
import java.util.List;
class TypeDescrFix extends TypeDescription.Latent {
TypeDescrFix(final String name, final int modifiers, final Generic superClass, final List<? extends Generic> interfaces) {
super(name, modifiers, superClass, interfaces);
}
@Override
public int getSegmentCount() {
return 0;
}
@Override
public AnnotationList getDeclaredAnnotations() {
return new AnnotationList.Empty();
}
}
public class ByteBuddyHello {
public static void main(String[] args) {
try {
final ByteBuddy bb = new ByteBuddy();
final TypeDescription.Latent typeDescrA = new TypeDescrFix("A", 0, null, null);
final TypeDescription.Latent typeDescrB = new TypeDescrFix("B", 0, null, null);
final DynamicType.Unloaded<Object> madeA = bb
.subclass(Object.class)
.name("A")
.modifiers(ModifierContributor.Resolver.of(Visibility.PUBLIC, TypeManifestation.FINAL).resolve())
.defineField("theB", typeDescrB, Opcodes.ACC_PUBLIC)
.make();
final DynamicType.Unloaded<Object> madeB = bb.subclass(Object.class)
.name("B")
.modifiers(ModifierContributor.Resolver.of(Visibility.PUBLIC, TypeManifestation.FINAL).resolve())
.defineField("theA", typeDescrA, Opcodes.ACC_PUBLIC)
.make();
Object a = madeA
.include(madeB)
.load(ByteBuddyHello.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded().newInstance();
System.out.println(a.toString());
final File folder = new File("/tmp/ByteBuddyHello");
madeA.saveIn(folder);
madeB.saveIn(folder);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
链接地址: http://www.djcxy.com/p/96649.html