Skip to content

Commit

Permalink
Allow copying a ParameterizedTypeName with new type arguments (#1140)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansman authored Sep 20, 2021
1 parent 1cdad8a commit 6d2f2e4
Show file tree
Hide file tree
Showing 2 changed files with 24 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,21 @@ 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 6d2f2e4

Please sign in to comment.