3

I know some of this, but not all of it. Most notably, I am aware of TypeDescription.Generic.Builder but I have a very specific question about it.

Suppose I want to build Supplier<? extends Frob<X>>.

Suppose further that all I know I have is a TypeDefinition for the parameter, but I don't know what it represents (in the example above it would represent Frob<X>). That is, I don't know whether the TypeDefinition I have is a class, a parameterized type, a generic array type, a type variable, a wildcard, or anything else; I just know it's a TypeDefinition.

Obviously if I wanted to make Supplier<Frob<X>>, I could just do:

TypeDescription.Generic.Builder.parameterizedType(TypeDescription.ForLoadedType.of(Supplier.class),
                                                  myTypeDefinition)
  .build();

…assuming I haven't made any typos in the snippet above.

How can I make an upper-bounded wildcard TypeDefinition out of an existing TypeDefinition suitable for supplying as the "parameterized" part of a parameterized type build? Is there an obvious recipe I'm overlooking, or is this a gap in the builder's DSL?

(I'm aware of the asWildcardUpperBound() method on TypeDescription.Generic.Builder, but that presumes I have a builder to work with, and in order to "bootstrap" such a builder I would need to give it a TypeDescription at the very least. But I don't have a TypeDescription; I have a TypeDefinition which might be parameterized, and I don't want to use asErasure().)

(I'm sort of looking for a way to do TypeDescription.Generic.Builder.parameterizedType(myTypeDefinition).asWildcardUpperBound().build(), but I can't obviously do that.)

There does seem to be TypeDescription.Generic.OfWildcardType.Latent::boundedAbove but I can't tell if that's supposed to be an "internal use only" class/method or not.

Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
  • From the top of my head, in Byte Buddy, there is `TypeDescriptionGenericBuilderTest` where any generic Java `Type` can be resolved to a `TypeDescription`. Wildcards can never be a top-level type which is why they are treated differently. I might need to move this test utility to the actual builder and to accept a `TypeDefinition`, that way it would become possible. – Rafael Winterhalter Jun 18 '21 at 07:56

1 Answers1

1

Such an API was indeed missing. I added an API in today's release (1.11.5) to translate an existing generic type description to a builder what allows transformations to arrays or wildcards. The API is TypeDescription.Generic.Builder.of which accepts a loaded or unloaded generic type description.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192