Skip to content

Commit

Permalink
Address #545.
Browse files Browse the repository at this point in the history
  • Loading branch information
hotchemi committed Dec 12, 2018
1 parent f3447b3 commit a9b2c02
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ fun VariableElement.isNullable(): Boolean =
fun VariableElement.asPreparedType(): TypeName =
this.asType()
.asTypeName()
.checkStringType()
.checkParameterStringType()
.correctStringType()
.correctParameterStringType()
.correctAnyType()
.mapToNullableTypeIf(this.isNullable())

/**
Expand Down Expand Up @@ -117,21 +118,27 @@ fun FileSpec.Builder.addTypes(types: List<TypeSpec>): FileSpec.Builder {
* To avoid KotlinPoet bug that returns java.lang.String when type name is kotlin.String.
* This method should be removed after addressing on KotlinPoet side.
*/
fun TypeName.checkStringType() =
fun TypeName.correctStringType() =
if (this.toString() == "java.lang.String") ClassName("kotlin", "String") else this

/**
* Convert [java.lang.String] to [kotlin.String] in a parameter.
* ref: https://github.com/permissions-dispatcher/PermissionsDispatcher/issues/427
*/
fun TypeName.checkParameterStringType(): TypeName {
fun TypeName.correctParameterStringType(): TypeName {
if (this is ParameterizedTypeName) {
val typeArguments = this.typeArguments.map { it.checkStringType() }.toTypedArray()
val typeArguments = this.typeArguments.map { it.correctStringType() }.toTypedArray()
return this.rawType.parameterizedBy(*typeArguments)
}
return this
}

/**
* https://github.com/permissions-dispatcher/PermissionsDispatcher/issues/545
*/
fun TypeName.correctAnyType() =
if (this.toString() == "java.lang.Object") ClassName("kotlin", "Any") else this

/**
* Returns this TypeName as nullable or non-nullable based on the given condition.
*/
Expand Down

0 comments on commit a9b2c02

Please sign in to comment.