Skip to content

Commit

Permalink
MOD: throw exception when unkown annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoJiang committed Nov 21, 2023
1 parent f869895 commit 3982940
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ object KudosAndroidJsonReader {
return fromJson(inputStream.bufferedReader(), T::class.java)
}

fun <T> fromJson(bufferReader: BufferedReader, clazz: Class<T>): T {
fun <T> fromJson(bufferedReader: BufferedReader, clazz: Class<T>): T {
val adapter = clazz.getDeclaredConstructor().newInstance()
return if (adapter is KudosJsonAdapter<*>) {
val jsonReader = JsonReader(bufferReader)
val jsonReader = JsonReader(bufferedReader)
adapter.fromJson(jsonReader) as T
} else {
throw IllegalArgumentException("class ${clazz.name} must implement KudosJsonAdapter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class KudosSyntheticResolveExtension(
if (thisDescriptor.annotations.hasAnnotation(KUDOS_NAME)) {
val kudosAnnotation = thisDescriptor.annotations.findAnnotation(KUDOS_NAME)
val annotationValues = kudosAnnotation?.allValueArguments?.values?.firstOrNull()?.value?.safeAs<List<IntValue>>()?.map {
if (it.value !in Options.validAnnotationList) {
throw IllegalArgumentException("unknown annotation argument ${it.value}")
}
it.value
}
kudosAnnotationValueMap[thisDescriptor.classId?.asString() ?: ""] = annotationValues ?: emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ class KudosFirSupertypeGenerationExtension(

private fun FirAnnotation.getIntArrayArgument(): List<Int>? {
if (this !is FirAnnotationCall) return null
return arguments.mapNotNull {
when (it.safeAs<FirPropertyAccessExpression>()?.calleeReference?.safeAs<FirSimpleNamedReference>()?.name?.asString()) {
return arguments.map {
when (val annotationValue = it.safeAs<FirPropertyAccessExpression>()?.calleeReference?.safeAs<FirSimpleNamedReference>()?.name?.asString()) {
"KUDOS_ANDROID_JSON_READER" -> {
Options.KUDOS_ANDROID_JSON_READER
}
Expand All @@ -167,7 +167,7 @@ class KudosFirSupertypeGenerationExtension(
}

else -> {
null
throw IllegalArgumentException("unknown annotation argument $annotationValue")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object Options {
const val KUDOS_GSON = 1
const val KUDOS_JACKSON = 2
const val KUDOS_ANDROID_JSON_READER = 3
val validAnnotationList = listOf(KUDOS_GSON, KUDOS_JACKSON, KUDOS_ANDROID_JSON_READER)

@JvmField
val gson = Option(
Expand Down

0 comments on commit 3982940

Please sign in to comment.