public class ParameterizedTypeBuilder extends Object
Example:
ParameterizedType pt = newTypeFromClass(Optional.class)
.withTypeArg(0, String.class)
.build();
// pt = Optional<String>
You can also start with the existing values of a parameterized type using the constructor
ParameterizedTypeBuilder(ParameterizedType).| Constructor and Description |
|---|
ParameterizedTypeBuilder(ParameterizedType parameterizedType)
Creates a builder with the given parameterized type as initial values.
|
| Modifier and Type | Method and Description |
|---|---|
ParameterizedTypeImpl |
build()
Creates a parameterized type with the configured raw type and type arguments.
|
static @Nullable Type |
createOwnerType(Class<?> rawType)
Creates the appropriate Type with generic information (if needed) to be used as
owner type of a parameterized type impl with the given raw type. |
static ParameterizedTypeImpl |
newCollectionType(Class<? extends Collection> baseType,
@Nullable Type typeArgument)
Creates a new parameterized type representing the given Collection type and the provided type argument.
|
static ParameterizedTypeImpl |
newMapType(Class<? extends Map> baseType,
@Nullable Type keyType,
@Nullable Type valueType)
Creates a new parameterized type representing the given Map type and the provided key and value arguments.
|
static ParameterizedTypeBuilder |
parameterizedTypeBuilder(Class<?> clazz)
Creates a builder with the given Class as its base type.
|
ParameterizedTypeBuilder |
withTypeArg(int typeParameterIndex,
@Nullable Type type)
Sets the type parameter at the given index to the provided type.
|
ParameterizedTypeBuilder |
withTypeArg(int typeParameterIndex,
TypeInfo typeInfo)
Sets the type argument to the given index.
|
ParameterizedTypeBuilder |
withTypeArg(String typeParameterName,
@Nullable Type type)
Sets the type parameter with the given name to the provided type.
|
ParameterizedTypeBuilder |
withTypeArg(String typeParameterName,
TypeInfo typeInfo)
Sets the type argument for the parameter with the given name.
|
ParameterizedTypeBuilder |
withTypeArg(TypeVariable<?> typeVariable,
@Nullable Type type)
Sets the type parameter identified by the given type variable to the provided type.
|
ParameterizedTypeBuilder |
withTypeArg(TypeVariable<?> typeVariable,
TypeInfo typeInfo)
Sets the given type variable to the provided type.
|
ParameterizedTypeBuilder |
withTypeVariables()
Resets all type parameters to the type variables of the raw type.
|
public ParameterizedTypeBuilder(ParameterizedType parameterizedType)
parameterizedType - the parameterized type to start the builder off withpublic static ParameterizedTypeBuilder parameterizedTypeBuilder(Class<?> clazz)
clazz - the raw type of the parameterized type that will be createdIllegalArgumentException - if the class has no type parameterspublic static ParameterizedTypeImpl newCollectionType(Class<? extends Collection> baseType, @Nullable @Nullable Type typeArgument)
Example:
newCollectionType(ArrayList.class, Double.class) // ArrayList<Double>
If you know the base type and argument at compile time, consider using
TypeReference instead:
Type type = new TypeReference<ArrayList<String>>(){ }.getType().
baseType - the collection type to use as raw typetypeArgument - the type argument of the collectionIllegalArgumentException - if the base type does not have at least one type parameterpublic static ParameterizedTypeImpl newMapType(Class<? extends Map> baseType, @Nullable @Nullable Type keyType, @Nullable @Nullable Type valueType)
Example:
newMapType(HashMap.class, String.class, Double.class) // Map<String, Double>
If you know the base type and arguments at compile time, consider using
TypeReference instead:
Type type = new TypeReference<HashMap<String, Double>>(){ }.getType().
baseType - the map implementation type to use as raw typekeyType - the key type argumentvalueType - the value type argumentIllegalArgumentException - if the base type does not have at least two type parameterspublic ParameterizedTypeBuilder withTypeArg(int typeParameterIndex, TypeInfo typeInfo)
withTypeArg(int, Type).typeParameterIndex - the index of the parameter to set (0-based)typeInfo - type info whose type should be set as argumentpublic ParameterizedTypeBuilder withTypeArg(int typeParameterIndex, @Nullable @Nullable Type type)
Example:
newTypeFromClass(Optional.class).withTypeArg(0, Double.class).build();typeParameterIndex - the index of the parameter to set (0-based)type - the type to set (or null to reset to the type variable)IllegalArgumentException - if the type parameter index is out of boundspublic ParameterizedTypeBuilder withTypeArg(String typeParameterName, TypeInfo typeInfo)
withTypeArg(int, Type).typeParameterName - the name of the type parameter to settypeInfo - type info whose type should be set as argumentIllegalArgumentException - if the raw type does not have a type parameter matching the namepublic ParameterizedTypeBuilder withTypeArg(String typeParameterName, @Nullable @Nullable Type type)
Example:
newTypeFromClass(Optional.class).withTypeArg("T", Double.class).build();typeParameterName - the name of the type parameter to settype - the type to set (or null to reset to the type variable)IllegalArgumentException - if the raw type does not have a type parameter matching the namepublic ParameterizedTypeBuilder withTypeArg(TypeVariable<?> typeVariable, TypeInfo typeInfo)
withTypeArg(TypeVariable, Type).typeVariable - the type variable to matchtypeInfo - type info whose type should be set as argumentIllegalArgumentException - if the type variable does not belong to the raw typepublic ParameterizedTypeBuilder withTypeArg(TypeVariable<?> typeVariable, @Nullable @Nullable Type type)
Example:
TypeVariable<?> tv = Optional.class.getTypeParameters()[0];
newTypeFromClass(Optional.class).withTypeArg(tv, Integer.class);
typeVariable - the type variable to matchtype - the type to set (or null to reset to the type variable)IllegalArgumentException - if the type variable does not belong to the raw typepublic ParameterizedTypeBuilder withTypeVariables()
public ParameterizedTypeImpl build()
IllegalStateException - if a type parameter has not been associated with a value@Nullable public static @Nullable Type createOwnerType(Class<?> rawType)
owner type of a parameterized type impl with the given raw type.rawType - the raw type whose owner type should be created with generic informationCopyright © 2023. All rights reserved.