admin管理员组文章数量:1415697
I have a TypeDescription.Generic
built like this:
import static net.bytebuddy.description.type.TypeDescription.Generic.Builder.parameterizedType;
final TypeDescription.Generic supplierType =
parameterizedType(typePool.describe("java.util.function.Supplier").resolve(),
List.of(TypeDescription.Generic.Builder.of(mySuperclass.asGenericType()).asWildcardUpperBound()))
.build();
(mySuperclass
is a non-generic class, i.e. a simple TypeDescription
.)
I believe that supplierType
accurately represents java.util.function.Supplier<? extends MySuperclass>
.
Next, in a DynamicType.Builder
(using a subclassing strategy), I define a private
field using this supplierType
:
builder = builder.defineField("$supplier", supplierType, PRIVATE, SYNTHETIC, FieldManifestation.FINAL);
I believe that this field definition accurately represents private final Supplier<? extends MySuperclass> $supplier;
.
Next, I define a method returning the result of invoking get()
on that Supplier
:
builder = builder
.defineMethod("$get", mySuperclass, PUBLIC, SYNTHETIC, MethodManifestation.FINAL)
.intercept(MethodCall.invoke(named("get"))
.onField("$supplier")
.withAssigner(Assigner.DEFAULT,
Assigner.Typing.DYNAMIC)); // <-- my question
I believe that this method definition accurately represents public final MySuperclass $get() { return $supplier.get(); }
, but I have a question about its return type and casting and dynamic assignment.
Without the withAssigner
configuration, at runtime the $get()
method in my generated class fails, saying basically that the Supplier
in question cannot return a T
as a mySuperclass
.
I thought that because of the upper bound of the Supplier
's wildcard type argument, which is mySuperclass
, I wouldn't need this dynamic assignment. What have I misunderstood?
I have a TypeDescription.Generic
built like this:
import static net.bytebuddy.description.type.TypeDescription.Generic.Builder.parameterizedType;
final TypeDescription.Generic supplierType =
parameterizedType(typePool.describe("java.util.function.Supplier").resolve(),
List.of(TypeDescription.Generic.Builder.of(mySuperclass.asGenericType()).asWildcardUpperBound()))
.build();
(mySuperclass
is a non-generic class, i.e. a simple TypeDescription
.)
I believe that supplierType
accurately represents java.util.function.Supplier<? extends MySuperclass>
.
Next, in a DynamicType.Builder
(using a subclassing strategy), I define a private
field using this supplierType
:
builder = builder.defineField("$supplier", supplierType, PRIVATE, SYNTHETIC, FieldManifestation.FINAL);
I believe that this field definition accurately represents private final Supplier<? extends MySuperclass> $supplier;
.
Next, I define a method returning the result of invoking get()
on that Supplier
:
builder = builder
.defineMethod("$get", mySuperclass, PUBLIC, SYNTHETIC, MethodManifestation.FINAL)
.intercept(MethodCall.invoke(named("get"))
.onField("$supplier")
.withAssigner(Assigner.DEFAULT,
Assigner.Typing.DYNAMIC)); // <-- my question
I believe that this method definition accurately represents public final MySuperclass $get() { return $supplier.get(); }
, but I have a question about its return type and casting and dynamic assignment.
Without the withAssigner
configuration, at runtime the $get()
method in my generated class fails, saying basically that the Supplier
in question cannot return a T
as a mySuperclass
.
I thought that because of the upper bound of the Supplier
's wildcard type argument, which is mySuperclass
, I wouldn't need this dynamic assignment. What have I misunderstood?
1 Answer
Reset to default 1Byte Buddy works on byte code. You do not have to add the cast in Java, because the compiler adds it for you, but it will still be present in the byte code. get
will return an object type, and it is then casted to T
by the compiler.
本文标签: Why is dynamic assignment required in a particular case in Byte BuddyStack Overflow
版权声明:本文标题:Why is dynamic assignment required in a particular case in Byte Buddy? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745238860a2649202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论