Skip to content

Commit

Permalink
fix Java to Kotlin collections mapping (#642) (#643)
Browse files Browse the repository at this point in the history
* fix Java to Kotlin collections mapping (#642)

* fix Java to Kotlin collections mapping (#642)
  • Loading branch information
kanat authored and hotchemi committed Sep 27, 2019
1 parent f9175b1 commit 9a3f45b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,19 @@ fun FileSpec.Builder.addTypes(types: List<TypeSpec>): FileSpec.Builder {
fun TypeName.correctJavaTypeToKotlinType(): TypeName {
return if (this is ParameterizedTypeName) {
val typeArguments = this.typeArguments.map { it.correctJavaTypeToKotlinType() }.toTypedArray()
return this.rawType.parameterizedBy(*typeArguments)
val rawType = ClassName.bestGuess(this.rawType.correctJavaTypeToKotlinType().toString())
return rawType.parameterizedBy(*typeArguments)
} else when (toString()) {
"java.lang.Byte" -> ClassName("kotlin", "Byte")
"java.lang.Double" -> ClassName("kotlin", "Double")
"java.lang.Object" -> ClassName("kotlin", "Any")
"java.lang.String" -> ClassName("kotlin", "String")
"java.util.Set" -> ClassName("kotlin.collections", "MutableSet")
"java.util.List" -> ClassName("kotlin.collections", "MutableList")
// https://github.com/permissions-dispatcher/PermissionsDispatcher/issues/599
// https://github.com/permissions-dispatcher/PermissionsDispatcher/issues/619
"kotlin.ByteArray", "kotlin.collections.List" -> this
"kotlin.ByteArray", "kotlin.collections.List", "kotlin.collections.Set",
"kotlin.collections.MutableList", "kotlin.collections.MutableSet" -> this
else -> this
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package permissions.dispatcher.processor.util

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeName
import org.junit.Assert.assertEquals
import org.junit.Test
Expand Down Expand Up @@ -69,4 +70,45 @@ class ExtensionsTest {
val expected = typeName.correctJavaTypeToKotlinType().toString()
assertEquals(expected, "kotlin.collections.List")
}

@Test
fun `kotlin Set retains its type`() {
val typeName = mock(TypeName::class.java)
`when`(typeName.toString()).thenReturn("kotlin.collections.Set")
val actual = typeName.correctJavaTypeToKotlinType().toString()
assertEquals("kotlin.collections.Set", actual)
}

@Test
fun `kotlin MutableList retains its type`() {
val typeName = mock(TypeName::class.java)
`when`(typeName.toString()).thenReturn("kotlin.collections.MutableList")
val actual = typeName.correctJavaTypeToKotlinType().toString()
assertEquals("kotlin.collections.MutableList", actual)
}

@Test
fun `kotlin MutableSet retains its type`() {
val typeName = mock(TypeName::class.java)
`when`(typeName.toString()).thenReturn("kotlin.collections.MutableSet")
val actual = typeName.correctJavaTypeToKotlinType().toString()
assertEquals("kotlin.collections.MutableSet", actual)
}

@Test
fun `java List being converted into kotlin MutableList`() {
val string = ClassName.bestGuess("kotlin.String")
val set = ClassName("java.util", "List")
val actual = set.parameterizedBy(string).correctJavaTypeToKotlinType().toString()
assertEquals("kotlin.collections.MutableList<kotlin.String>", actual)
}

@Test
fun `java Set being converted into kotlin MutableSet`() {
val string = ClassName.bestGuess("kotlin.String")
val set = ClassName("java.util", "Set")
val actual = set.parameterizedBy(string).correctJavaTypeToKotlinType().toString()
assertEquals("kotlin.collections.MutableSet<kotlin.String>", actual)
}

}

0 comments on commit 9a3f45b

Please sign in to comment.