Skip to content

Commit

Permalink
Allow copying a ParameterizedTypeName with new type arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ansman committed Sep 20, 2021
1 parent a9755a4 commit f053ffb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public class ParameterizedTypeName internal constructor(
return ParameterizedTypeName(enclosingType, rawType, typeArguments, nullable, annotations, tags)
}

public fun copy(
nullable: Boolean = this.isNullable,
annotations: List<AnnotationSpec> = this.annotations,
tags: Map<KClass<*>, Any> = this.tags,
typeArguments: List<TypeName> = this.typeArguments
): ParameterizedTypeName {
return ParameterizedTypeName(enclosingType, rawType, typeArguments, nullable, annotations, tags)
}

public fun plusParameter(typeArgument: TypeName): ParameterizedTypeName =
ParameterizedTypeName(
enclosingType, rawType, typeArguments + typeArgument, isNullable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ class ParameterizedTypeNameTest {
assertThat(typeName.toString()).isEqualTo("java.util.Map<java.lang.String, java.lang.Integer>")
}

@Test fun copyingTypeArguments() {
val typeName = java.util.Map::class.java
.plusParameter(java.lang.String::class.java)
.plusParameter(java.lang.Integer::class.java)
.nestedClass("Entry", listOf(
java.lang.String::class.java.asClassName(),
java.lang.Integer::class.java.asClassName()
))
.copy(typeArguments = listOf(STAR, STAR))
assertThat(typeName.toString()).isEqualTo("java.util.Map<java.lang.String, java.lang.Integer>.Entry<*, *>")
}

interface Projections {
val outVariance: KClass<out Annotation>
val inVariance: KClass<in Test>
Expand Down

0 comments on commit f053ffb

Please sign in to comment.