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 Aug 31, 2021
1 parent a9755a4 commit 095dcbd
Show file tree
Hide file tree
Showing 2 changed files with 18 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,15 @@ class ParameterizedTypeNameTest {
assertThat(typeName.toString()).isEqualTo("java.util.Map<java.lang.String, java.lang.Integer>")
}

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

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

0 comments on commit 095dcbd

Please sign in to comment.