Skip to content

Commit e5a52e3

Browse files
authored
[603][620]Fix java-kotlin conversion issue (#622)
* Fix java-kotlin conversion issue which derives from KotlinPoet. * Update mockito dependency. * Fix to use old mockito for test module.
1 parent 5cba044 commit e5a52e3

File tree

7 files changed

+79
-30
lines changed

7 files changed

+79
-30
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ ANDROIDX_LIBRARY_VERSION= 1.0.0
2424
JAVAPOET_VERSION = 1.9.0
2525
KOTLINPOET_VERSION = 1.0.1
2626
JUNIT_VERSION = 4.12
27-
MOCKITO_VERSION = 1.10.19
28-
POWERMOCK_VERSION = 1.6.4
27+
MOCKITO_VERSION = 2.28.2
28+
POWERMOCK_VERSION = 2.0.2
2929
COMPILE_TESTING_VERSION = 0.12
3030
LINT_VERSION = 26.2.0-alpha06
3131
ROBOLECTRIC_VERSION = 3.3.2

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies {
3838

3939
testImplementation "junit:junit:$JUNIT_VERSION"
4040
testImplementation "org.mockito:mockito-core:$MOCKITO_VERSION"
41-
testImplementation "org.powermock:powermock-api-mockito:$POWERMOCK_VERSION"
41+
testImplementation "org.powermock:powermock-api-mockito2:$POWERMOCK_VERSION"
4242
testImplementation "org.powermock:powermock-module-junit4:$POWERMOCK_VERSION"
4343

4444
api project(path: ':annotation')

processor/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dependencies {
4141
testCompile androidJar()
4242
testCompile files(Jvm.current().getToolsJar())
4343
testCompile "junit:junit:$JUNIT_VERSION"
44+
testCompile "org.mockito:mockito-core:$MOCKITO_VERSION"
4445
testCompile "com.google.testing.compile:compile-testing:$COMPILE_TESTING_VERSION"
4546
testCompile "commons-io:commons-io:$COMMONS_IO_VERSION"
4647
testCompile "com.squareup.okio:okio:2.1.0"

processor/src/main/kotlin/permissions/dispatcher/processor/util/Extensions.kt

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ fun VariableElement.isNullable(): Boolean =
6767
fun VariableElement.asPreparedType(): TypeName =
6868
this.asType()
6969
.asTypeName()
70-
.correctStringType()
71-
.correctParameterStringType()
72-
.correctAnyType()
70+
.correctJavaTypeToKotlinType()
7371
.mapToNullableTypeIf(this.isNullable())
7472

7573
/**
@@ -118,30 +116,22 @@ fun FileSpec.Builder.addTypes(types: List<TypeSpec>): FileSpec.Builder {
118116
}
119117

120118
/**
121-
* To avoid KotlinPoet bug that returns java.lang.String when type name is kotlin.String.
122-
* This method should be removed after addressing on KotlinPoet side.
119+
* To avoid KotlinPoet bugs return java.lang.class when type name is in kotlin package.
120+
* TODO: Remove this method after being addressed on KotlinPoet side.
123121
*/
124-
fun TypeName.correctStringType() =
125-
if (this.toString() == "java.lang.String") ClassName("kotlin", "String") else this
126-
127-
/**
128-
* Convert [java.lang.String] to [kotlin.String] in a parameter.
129-
* ref: https://github.com/permissions-dispatcher/PermissionsDispatcher/issues/427
130-
*/
131-
fun TypeName.correctParameterStringType(): TypeName {
132-
if (this is ParameterizedTypeName) {
133-
val typeArguments = this.typeArguments.map { it.correctStringType() }.toTypedArray()
122+
fun TypeName.correctJavaTypeToKotlinType(): TypeName {
123+
return if (this is ParameterizedTypeName) {
124+
val typeArguments = this.typeArguments.map { it.correctJavaTypeToKotlinType() }.toTypedArray()
134125
return this.rawType.parameterizedBy(*typeArguments)
126+
} else when (toString()) {
127+
"java.lang.Byte" -> ClassName("kotlin", "Byte")
128+
"java.lang.Double" -> ClassName("kotlin", "Double")
129+
"java.lang.Object" -> ClassName("kotlin", "Any")
130+
"java.lang.String" -> ClassName("kotlin", "String")
131+
else -> this
135132
}
136-
return this
137133
}
138134

139-
/**
140-
* https://github.com/permissions-dispatcher/PermissionsDispatcher/issues/545
141-
*/
142-
fun TypeName.correctAnyType() =
143-
if (this.toString() == "java.lang.Object") ClassName("kotlin", "Any") else this
144-
145135
/**
146136
* Returns this TypeName as nullable or non-nullable based on the given condition.
147137
*/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package permissions.dispatcher.processor.util
2+
3+
import com.squareup.kotlinpoet.ClassName
4+
import com.squareup.kotlinpoet.ParameterizedTypeName
5+
import com.squareup.kotlinpoet.TypeName
6+
import org.junit.Assert.assertEquals
7+
import org.junit.Test
8+
import org.mockito.Mockito.`when`
9+
import org.mockito.Mockito.mock
10+
11+
class ExtensionsTest {
12+
@Test
13+
fun `java Byte is converted into kotlin Byte`() {
14+
val typeName = mock(TypeName::class.java)
15+
`when`(typeName.toString()).thenReturn("java.lang.Byte")
16+
val expected = typeName.correctJavaTypeToKotlinType().toString()
17+
assertEquals(expected, "kotlin.Byte")
18+
}
19+
20+
@Test
21+
fun `java Double is converted into kotlin Double`() {
22+
val typeName = mock(TypeName::class.java)
23+
`when`(typeName.toString()).thenReturn("java.lang.Double")
24+
val expected = typeName.correctJavaTypeToKotlinType().toString()
25+
assertEquals(expected, "kotlin.Double")
26+
}
27+
28+
@Test
29+
fun `java Object is converted into kotlin Any`() {
30+
val typeName = mock(TypeName::class.java)
31+
`when`(typeName.toString()).thenReturn("java.lang.Object")
32+
val expected = typeName.correctJavaTypeToKotlinType().toString()
33+
assertEquals(expected, "kotlin.Any")
34+
}
35+
36+
@Test
37+
fun `java String is converted into kotlin String`() {
38+
val typeName = mock(TypeName::class.java)
39+
`when`(typeName.toString()).thenReturn("java.lang.String")
40+
val expected = typeName.correctJavaTypeToKotlinType().toString()
41+
assertEquals(expected, "kotlin.String")
42+
}
43+
44+
@Test
45+
fun `java String in List is converted into kotlin String`() {
46+
val parameterizedTypeName = mock(ParameterizedTypeName::class.java)
47+
val typeName = mock(TypeName::class.java)
48+
`when`(typeName.toString()).thenReturn("java.lang.String")
49+
`when`(parameterizedTypeName.rawType).thenReturn(ClassName.bestGuess("kotlin.collections.List"))
50+
`when`(parameterizedTypeName.typeArguments).thenReturn(listOf(typeName))
51+
52+
val expected = parameterizedTypeName.correctJavaTypeToKotlinType() as ParameterizedTypeName
53+
expected.typeArguments.first().toString()
54+
assertEquals( expected.typeArguments.first().toString(), "kotlin.String")
55+
}
56+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

test/build.gradle

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ dependencies {
4646

4747
testImplementation "junit:junit:$JUNIT_VERSION"
4848
testImplementation "org.jetbrains.kotlin:kotlin-reflect:1.3.11"
49-
testImplementation "org.mockito:mockito-core:$MOCKITO_VERSION"
50-
testImplementation "org.powermock:powermock-api-mockito:$POWERMOCK_VERSION"
51-
testImplementation "org.powermock:powermock-module-junit4:$POWERMOCK_VERSION"
52-
testImplementation "org.powermock:powermock-module-junit4-rule:$POWERMOCK_VERSION"
53-
testImplementation "org.powermock:powermock-classloading-xstream:$POWERMOCK_VERSION"
49+
// TODO: update mockito and powermock
50+
testImplementation "org.mockito:mockito-core:1.10.19"
51+
testImplementation "org.powermock:powermock-api-mockito:1.6.4"
52+
testImplementation "org.powermock:powermock-module-junit4:1.6.4"
53+
testImplementation "org.powermock:powermock-module-junit4-rule:1.6.4"
54+
testImplementation "org.powermock:powermock-classloading-xstream:1.6.4"
5455
testImplementation "org.robolectric:robolectric:$ROBOLECTRIC_VERSION"
5556
}

0 commit comments

Comments
 (0)