Skip to content

Commit

Permalink
Merge pull request #35 from tobrun/kdz-move-import-to-data-compat-ann…
Browse files Browse the repository at this point in the history
…otation

Move imports to DataCompat annotation
  • Loading branch information
kiryldz authored Mar 15, 2023
2 parents 3c42494 + a7aea85 commit 6959447
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = [])
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = []
)
annotation class Default(val valueAsString: String)
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DataCompatProcessor(
// for default values.
val classToDefaultValuesMap =
mutableMapOf<KSClassDeclaration, MutableMap<String, String?>>()
val imports = ArrayList<String>()

val symbolsWithDefaultAnnotation =
resolver.getSymbolsWithAnnotation(Default::class.qualifiedName!!, true)
symbolsWithDefaultAnnotation.forEach { annotatedProperty ->
Expand All @@ -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<String>)
}
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()
}

Expand All @@ -102,7 +99,6 @@ class DataCompatProcessor(

private inner class Visitor(
private val defaultValuesMap: Map<KSClassDeclaration, MutableMap<String, String?>>,
private val imports: List<String>
) : KSVisitorVoid() {

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

val imports = ArrayList<String>()
classDeclaration.annotations.firstOrNull {
it.annotationType.resolve().toString() == DataCompat::class.simpleName
}?.arguments?.firstOrNull()?.value?.let { imports.addAll(it as ArrayList<String>) }
val otherAnnotations = classDeclaration.annotations
.filter { it.annotationType.resolve().toString() != DataCompat::class.simpleName }
val implementedInterfaces = classDeclaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package com.tobrun.datacompat.annotation
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
annotation class DataCompat
annotation class DataCompat(val importsForDefaults: Array<String> = [])
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class Default(val valueAsString: String, val importList: Array<String> = [])
annotation class Default(val valueAsString: String)
""".trimIndent()
)

0 comments on commit 6959447

Please sign in to comment.