Skip to content

Commit 37349dd

Browse files
authored
Merge pull request #32 from tobrun/kdz-retain-imports
Add import support for default parameters
2 parents 169045b + a66f2ee commit 37349dd

File tree

7 files changed

+33
-11
lines changed

7 files changed

+33
-11
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ And you will have to include the required dependencies:
2828

2929
```groovy
3030
dependencies {
31-
implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.3.0'
32-
ksp 'com.github.tobrun.kotlin-data-compat:processor:0.3.0'
31+
implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.4.0'
32+
ksp 'com.github.tobrun.kotlin-data-compat:processor:0.4.0'
3333
}
3434
```
3535

@@ -41,6 +41,7 @@ Given an exisiting data class:
4141
- mark class private
4242
- append `Data` to the class name
4343
- support default parameters by using `@Default` annotation
44+
- support imports for default parameters
4445
- retain existing class annotations (but not parameters for them)
4546
- retain existing interfaces
4647

@@ -59,7 +60,7 @@ annotation class SampleAnnotation
5960
@DataCompat
6061
@SampleAnnotation
6162
private data class PersonData(
62-
@Default("\"John\"")
63+
@Default("\"John\" + Date(1580897313933L).toString()", imports = ["java.util.Date"])
6364
val name: String,
6465
val nickname: String?,
6566
@Default("42")
@@ -72,6 +73,7 @@ After compilation, the following class will be generated:
7273
```kotlin
7374
package com.tobrun.`data`.compat.example
7475

76+
import java.util.Date
7577
import java.util.Objects
7678
import kotlin.Any
7779
import kotlin.Boolean
@@ -120,7 +122,7 @@ public class Person private constructor(
120122
*/
121123
public class Builder {
122124
@set:JvmSynthetic
123-
public var name: String? = "John"
125+
public var name: String? = "John" + Date(1580897313933L).toString()
124126

125127
@set:JvmSynthetic
126128
public var nickname: String? = null

annotation/src/main/kotlin/com/tobrun/datacompat/annotation/Default.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ package com.tobrun.datacompat.annotation
1010
* @param valueAsString exact representation of the default value. E.g. if default [String] is used,
1111
* it should be passed here as "\"STRING_VALUE\""; if default [Int] is used, it should be passed
1212
* as "INT_VALUE".
13+
*
14+
* @param imports if default parameter requires additional imports, they should be passed here.
15+
* E.g. `listOf("android.graphics.Color")`
1316
*/
1417
@Retention(AnnotationRetention.RUNTIME)
1518
@Target(AnnotationTarget.VALUE_PARAMETER)
16-
annotation class Default(val valueAsString: String)
19+
annotation class Default(
20+
val valueAsString: String,
21+
val imports: Array<String> = []
22+
)

example/src/main/kotlin/com/tobrun/data/compat/example/PersonData.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ annotation class SampleAnnotation
1515
@DataCompat
1616
@SampleAnnotation
1717
private data class PersonData(
18-
@Default("\"John\"")
18+
@Default("\"John\" + Date(1580897313933L).toString()", imports = ["java.util.Date"])
1919
val name: String,
2020
val nickname: String?,
2121
@Default("42")

processor/src/main/kotlin/com/tobrun/datacompat/DataCompatProcessor.kt

+15-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class DataCompatProcessor(
7777
val implementedInterfaces = classDeclaration
7878
.superTypes
7979
.filter { (it.resolve().declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }
80+
val imports = ArrayList<String>()
8081

8182
// Map KSP properties with KoltinPoet TypeNames
8283
val propertyMap = mutableMapOf<KSPropertyDeclaration, TypeName>()
@@ -219,9 +220,13 @@ class DataCompatProcessor(
219220
val builderBuilder = TypeSpec.classBuilder("Builder")
220221
for (property in propertyMap) {
221222
val propertyName = property.key.toString()
222-
val defaultValue = property.key.annotations
223+
val defaultAnnotationsParams = property.key.annotations
223224
.firstOrNull { it.annotationType.resolve().toString() == Default::class.simpleName }
224-
?.arguments?.first()
225+
?.arguments
226+
val defaultValue = defaultAnnotationsParams?.first()
227+
defaultAnnotationsParams?.getOrNull(1)?.value?.let {
228+
imports.addAll(it as ArrayList<String>)
229+
}
225230
val nullableType = property.value.copy(nullable = true)
226231
builderBuilder.addProperty(
227232
PropertySpec.builder(propertyName, nullableType)
@@ -350,6 +355,14 @@ class DataCompatProcessor(
350355
.addType(classBuilder.build())
351356
.addFunction(initializerFunctionBuilder.build())
352357

358+
imports.forEach {
359+
fileBuilder
360+
.addImport(
361+
it.split(".").dropLast(1).joinToString("."),
362+
it.split(".").last()
363+
)
364+
}
365+
353366
fileBuilder.build().writeTo(codeGenerator = codeGenerator, aggregating = false)
354367
}
355368

processor/src/test/kotlin/com/tobrun/datacompat/DataCompatProcessorTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface EmptyInterface2
2828
@Deprecated
2929
@DataCompat
3030
private data class PersonData(
31-
@Default("\"John\"")
31+
@Default("\"John\"", ["java.util.Date"])
3232
val name: String,
3333
@Default("null")
3434
val nickname: String?,

processor/src/test/kotlin/com/tobrun/datacompat/SimpleTestContent.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.tobrun.datacompat
22

33
internal val expectedSimpleTestContent = """
4+
import java.util.Date
45
import java.util.Objects
56
import kotlin.Any
67
import kotlin.Boolean

processor/src/test/kotlin/com/tobrun/datacompat/TestAnnotation.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package com.tobrun.datacompat.annotation
1212
annotation class DataCompat
1313
1414
@Retention(AnnotationRetention.RUNTIME)
15-
@Target(AnnotationTarget.CLASS)
16-
annotation class Default(val valueAsString: String)
15+
@Target(AnnotationTarget.VALUE_PARAMETER)
16+
annotation class Default(val valueAsString: String, val importList: Array<String> = [])
1717
""".trimIndent()
1818
)

0 commit comments

Comments
 (0)