truncate

fun ClassName.truncate(hashParams: List<Any>, separator: String = "_", innerClassLength: Int = 0): ClassName

Truncates the class name to a valid file name length by removing characters from the end of the class name. The hash of the hashParams will be appended to the class name with the given separator. If the class name is too long, it will be truncated by removing the last characters before the hash, but the hash will be unchanged.

val someScope = ClassName("com.example", "SomeScope")
val boundType = ClassName("com.example", "BoundType")
val normalName = ClassName("com.example", "Outer", "Middle", "Inner")
val truncatedName = normalName.truncate(
hashParams = listOf(someScope, boundType),
separator = "_",
innerClassLength = 0
)
println(truncatedName) // com.example.Outer_Middle_Inner_0a1b2c3d

Throws

if the resulting class name is too long to be a valid file name even after truncating.