diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterizedTypeName.kt b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterizedTypeName.kt index e65b78832c..af726bd4b4 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterizedTypeName.kt +++ b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterizedTypeName.kt @@ -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 = this.annotations, + tags: Map, Any> = this.tags, + typeArguments: List = this.typeArguments + ): ParameterizedTypeName { + return ParameterizedTypeName(enclosingType, rawType, typeArguments, nullable, annotations, tags) + } + public fun plusParameter(typeArgument: TypeName): ParameterizedTypeName = ParameterizedTypeName( enclosingType, rawType, typeArguments + typeArgument, isNullable, diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt b/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt index 0424ed7cad..00c83195b1 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt +++ b/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt @@ -112,6 +112,21 @@ class ParameterizedTypeNameTest { assertThat(typeName.toString()).isEqualTo("java.util.Map") } + @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.Entry<*, *>") + } + interface Projections { val outVariance: KClass val inVariance: KClass