Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte Buddy - how can make a field self type?

It is necessary to describe the structure of this class

class A{
    private List<A> listA;   
}

tried the solution: Byte-buddy: generate classes with cyclic types

but it will lead to an error

java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalStateException: Cannot resolve declared type of a latent type description:...

like image 403
ua80669782699 Avatar asked May 13 '19 16:05

ua80669782699


Video Answer


1 Answers

You can use TargetType as a reference for the currently generated type:

new ByteBuddy()
  .subclass(Object.class)
  .name("A")
  .defineField("listA", 
      TypeDescription.Generic.Builder.parameterizedType(
          List.class, TargetType.class).build(),
      Visibility.PRIVATE)
  .make()
like image 188
Rafael Winterhalter Avatar answered Nov 10 '22 17:11

Rafael Winterhalter