Skip to content

Commit 6959447

Browse files
authored
Merge pull request #35 from tobrun/kdz-move-import-to-data-compat-annotation
Move imports to DataCompat annotation
2 parents 3c42494 + a7aea85 commit 6959447

File tree

7 files changed

+21
-24
lines changed

7 files changed

+21
-24
lines changed

README.md

+4-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.4.1'
32-
ksp 'com.github.tobrun.kotlin-data-compat:processor:0.4.1'
31+
implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.5.0'
32+
ksp 'com.github.tobrun.kotlin-data-compat:processor:0.5.0'
3333
}
3434
```
3535

@@ -57,10 +57,10 @@ annotation class SampleAnnotation
5757
* @property nickname The nickname.
5858
* @property age The age.
5959
*/
60-
@DataCompat
60+
@DataCompat(importsForDefaults = ["java.util.Date"])
6161
@SampleAnnotation
6262
private data class PersonData(
63-
@Default("\"John\" + Date(1580897313933L).toString()", imports = ["java.util.Date"])
63+
@Default("\"John\" + Date(1580897313933L).toString()")
6464
val name: String,
6565
val nickname: String?,
6666
@Default("42")

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package com.tobrun.datacompat.annotation
33
/**
44
* Annotation class of DataCompat.
55
* Classes annotated with this annotation are required to be Kotlin data classes with private visibility.
6+
*
7+
* @param importsForDefaults if any default values require additional imports, they should be passed here.
8+
* E.g. `["android.graphics.Color"]`
69
*/
710
@Retention(AnnotationRetention.RUNTIME)
811
@Target(AnnotationTarget.CLASS)
9-
annotation class DataCompat
12+
annotation class DataCompat(val importsForDefaults: Array<String> = [])

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ 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")`
1613
*/
1714
@Retention(AnnotationRetention.RUNTIME)
1815
@Target(AnnotationTarget.VALUE_PARAMETER)
19-
annotation class Default(
20-
val valueAsString: String,
21-
val imports: Array<String> = []
22-
)
16+
annotation class Default(val valueAsString: String)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ annotation class SampleAnnotation
1212
* @property nickname The nickname.
1313
* @property age The age.
1414
*/
15-
@DataCompat
15+
@DataCompat(importsForDefaults = ["java.util.Date"])
1616
@SampleAnnotation
1717
private data class PersonData(
18-
@Default("\"John\" + Date(1580897313933L).toString()", imports = ["java.util.Date"])
18+
@Default("\"John\" + Date(1580897313933L).toString()")
1919
val name: String,
2020
val nickname: String?,
2121
@Default("42")

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class DataCompatProcessor(
6060
// for default values.
6161
val classToDefaultValuesMap =
6262
mutableMapOf<KSClassDeclaration, MutableMap<String, String?>>()
63-
val imports = ArrayList<String>()
63+
6464
val symbolsWithDefaultAnnotation =
6565
resolver.getSymbolsWithAnnotation(Default::class.qualifiedName!!, true)
6666
symbolsWithDefaultAnnotation.forEach { annotatedProperty ->
@@ -76,16 +76,13 @@ class DataCompatProcessor(
7676
val defaultValue = defaultAnnotationsParams?.first()
7777
defaultValueMap[annotatedProperty.name!!.getShortName()] =
7878
defaultValue?.value as? String?
79-
defaultAnnotationsParams?.getOrNull(1)?.value?.let {
80-
imports.addAll(it as ArrayList<String>)
81-
}
8279
classToDefaultValuesMap[parentClass] = defaultValueMap
8380
}
8481
}
8582

8683
val unableToProcess = dataCompatAnnotated.filterNot { it.validate() }
8784
dataCompatAnnotated.filter { it is KSClassDeclaration && it.validate() }
88-
.forEach { it.accept(Visitor(classToDefaultValuesMap, imports), Unit) }
85+
.forEach { it.accept(Visitor(classToDefaultValuesMap), Unit) }
8986
return unableToProcess.toList()
9087
}
9188

@@ -102,7 +99,6 @@ class DataCompatProcessor(
10299

103100
private inner class Visitor(
104101
private val defaultValuesMap: Map<KSClassDeclaration, MutableMap<String, String?>>,
105-
private val imports: List<String>
106102
) : KSVisitorVoid() {
107103

108104
@Suppress("LongMethod", "MaxLineLength", "ComplexMethod")
@@ -118,6 +114,10 @@ class DataCompatProcessor(
118114
val classKdoc = classDeclaration.docString
119115
val packageName = classDeclaration.packageName.asString()
120116

117+
val imports = ArrayList<String>()
118+
classDeclaration.annotations.firstOrNull {
119+
it.annotationType.resolve().toString() == DataCompat::class.simpleName
120+
}?.arguments?.firstOrNull()?.value?.let { imports.addAll(it as ArrayList<String>) }
121121
val otherAnnotations = classDeclaration.annotations
122122
.filter { it.annotationType.resolve().toString() != DataCompat::class.simpleName }
123123
val implementedInterfaces = classDeclaration

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ interface EmptyInterface2
2626
* @property veryLongAndVeryDetailedDescription The very long and very detailed description.
2727
*/
2828
@Deprecated
29-
@DataCompat
29+
@DataCompat(importsForDefaults = ["java.util.Date"])
3030
private data class PersonData(
31-
@Default("\"John\"", ["java.util.Date"])
31+
@Default("\"John\"")
3232
val name: String,
3333
@Default("null")
3434
val nickname: String?,

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

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

0 commit comments

Comments
 (0)