diff --git a/README.md b/README.md index 512cc76..3584e4b 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ And you will have to include the required dependencies: ```groovy dependencies { - implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.4.1' - ksp 'com.github.tobrun.kotlin-data-compat:processor:0.4.1' + implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.5.0' + ksp 'com.github.tobrun.kotlin-data-compat:processor:0.5.0' } ``` @@ -57,10 +57,10 @@ annotation class SampleAnnotation * @property nickname The nickname. * @property age The age. */ -@DataCompat +@DataCompat(importsForDefaults = ["java.util.Date"]) @SampleAnnotation private data class PersonData( - @Default("\"John\" + Date(1580897313933L).toString()", imports = ["java.util.Date"]) + @Default("\"John\" + Date(1580897313933L).toString()") val name: String, val nickname: String?, @Default("42") diff --git a/annotation/src/main/kotlin/com/tobrun/datacompat/annotation/DataCompat.kt b/annotation/src/main/kotlin/com/tobrun/datacompat/annotation/DataCompat.kt index f29f223..524f113 100644 --- a/annotation/src/main/kotlin/com/tobrun/datacompat/annotation/DataCompat.kt +++ b/annotation/src/main/kotlin/com/tobrun/datacompat/annotation/DataCompat.kt @@ -3,7 +3,10 @@ package com.tobrun.datacompat.annotation /** * Annotation class of DataCompat. * Classes annotated with this annotation are required to be Kotlin data classes with private visibility. + * + * @param importsForDefaults if any default values require additional imports, they should be passed here. + * E.g. `["android.graphics.Color"]` */ @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.CLASS) -annotation class DataCompat +annotation class DataCompat(val importsForDefaults: Array = []) diff --git a/annotation/src/main/kotlin/com/tobrun/datacompat/annotation/Default.kt b/annotation/src/main/kotlin/com/tobrun/datacompat/annotation/Default.kt index 964d443..10946f3 100644 --- a/annotation/src/main/kotlin/com/tobrun/datacompat/annotation/Default.kt +++ b/annotation/src/main/kotlin/com/tobrun/datacompat/annotation/Default.kt @@ -10,13 +10,7 @@ package com.tobrun.datacompat.annotation * @param valueAsString exact representation of the default value. E.g. if default [String] is used, * it should be passed here as "\"STRING_VALUE\""; if default [Int] is used, it should be passed * as "INT_VALUE". - * - * @param imports if default parameter requires additional imports, they should be passed here. - * E.g. `listOf("android.graphics.Color")` */ @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.VALUE_PARAMETER) -annotation class Default( - val valueAsString: String, - val imports: Array = [] -) +annotation class Default(val valueAsString: String) diff --git a/example/src/main/kotlin/com/tobrun/data/compat/example/PersonData.kt b/example/src/main/kotlin/com/tobrun/data/compat/example/PersonData.kt index 5136eea..d208685 100644 --- a/example/src/main/kotlin/com/tobrun/data/compat/example/PersonData.kt +++ b/example/src/main/kotlin/com/tobrun/data/compat/example/PersonData.kt @@ -12,10 +12,10 @@ annotation class SampleAnnotation * @property nickname The nickname. * @property age The age. */ -@DataCompat +@DataCompat(importsForDefaults = ["java.util.Date"]) @SampleAnnotation private data class PersonData( - @Default("\"John\" + Date(1580897313933L).toString()", imports = ["java.util.Date"]) + @Default("\"John\" + Date(1580897313933L).toString()") val name: String, val nickname: String?, @Default("42") diff --git a/processor/src/main/kotlin/com/tobrun/datacompat/DataCompatProcessor.kt b/processor/src/main/kotlin/com/tobrun/datacompat/DataCompatProcessor.kt index 66de94f..726e081 100644 --- a/processor/src/main/kotlin/com/tobrun/datacompat/DataCompatProcessor.kt +++ b/processor/src/main/kotlin/com/tobrun/datacompat/DataCompatProcessor.kt @@ -60,7 +60,7 @@ class DataCompatProcessor( // for default values. val classToDefaultValuesMap = mutableMapOf>() - val imports = ArrayList() + val symbolsWithDefaultAnnotation = resolver.getSymbolsWithAnnotation(Default::class.qualifiedName!!, true) symbolsWithDefaultAnnotation.forEach { annotatedProperty -> @@ -76,16 +76,13 @@ class DataCompatProcessor( val defaultValue = defaultAnnotationsParams?.first() defaultValueMap[annotatedProperty.name!!.getShortName()] = defaultValue?.value as? String? - defaultAnnotationsParams?.getOrNull(1)?.value?.let { - imports.addAll(it as ArrayList) - } classToDefaultValuesMap[parentClass] = defaultValueMap } } val unableToProcess = dataCompatAnnotated.filterNot { it.validate() } dataCompatAnnotated.filter { it is KSClassDeclaration && it.validate() } - .forEach { it.accept(Visitor(classToDefaultValuesMap, imports), Unit) } + .forEach { it.accept(Visitor(classToDefaultValuesMap), Unit) } return unableToProcess.toList() } @@ -102,7 +99,6 @@ class DataCompatProcessor( private inner class Visitor( private val defaultValuesMap: Map>, - private val imports: List ) : KSVisitorVoid() { @Suppress("LongMethod", "MaxLineLength", "ComplexMethod") @@ -118,6 +114,10 @@ class DataCompatProcessor( val classKdoc = classDeclaration.docString val packageName = classDeclaration.packageName.asString() + val imports = ArrayList() + classDeclaration.annotations.firstOrNull { + it.annotationType.resolve().toString() == DataCompat::class.simpleName + }?.arguments?.firstOrNull()?.value?.let { imports.addAll(it as ArrayList) } val otherAnnotations = classDeclaration.annotations .filter { it.annotationType.resolve().toString() != DataCompat::class.simpleName } val implementedInterfaces = classDeclaration diff --git a/processor/src/test/kotlin/com/tobrun/datacompat/DataCompatProcessorTest.kt b/processor/src/test/kotlin/com/tobrun/datacompat/DataCompatProcessorTest.kt index d9f627b..4feeaa1 100644 --- a/processor/src/test/kotlin/com/tobrun/datacompat/DataCompatProcessorTest.kt +++ b/processor/src/test/kotlin/com/tobrun/datacompat/DataCompatProcessorTest.kt @@ -26,9 +26,9 @@ interface EmptyInterface2 * @property veryLongAndVeryDetailedDescription The very long and very detailed description. */ @Deprecated -@DataCompat +@DataCompat(importsForDefaults = ["java.util.Date"]) private data class PersonData( - @Default("\"John\"", ["java.util.Date"]) + @Default("\"John\"") val name: String, @Default("null") val nickname: String?, diff --git a/processor/src/test/kotlin/com/tobrun/datacompat/TestAnnotation.kt b/processor/src/test/kotlin/com/tobrun/datacompat/TestAnnotation.kt index c9141ed..a8b3409 100644 --- a/processor/src/test/kotlin/com/tobrun/datacompat/TestAnnotation.kt +++ b/processor/src/test/kotlin/com/tobrun/datacompat/TestAnnotation.kt @@ -9,10 +9,10 @@ package com.tobrun.datacompat.annotation @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.CLASS) -annotation class DataCompat +annotation class DataCompat(val importsForDefaults: Array = []) @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.VALUE_PARAMETER) -annotation class Default(val valueAsString: String, val importList: Array = []) +annotation class Default(val valueAsString: String) """.trimIndent() )