Skip to content

Commit

Permalink
MOD: Modify naming and version
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoJiang committed Nov 3, 2023
1 parent 9d237f4 commit cb7bc97
Show file tree
Hide file tree
Showing 30 changed files with 189 additions and 162 deletions.
22 changes: 22 additions & 0 deletions android-json-reader/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2023 Kanyun, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
java
kotlin("jvm")
}

dependencies {
}
1 change: 1 addition & 0 deletions android-json-reader/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POM_NAME=android-json-reader
1 change: 1 addition & 0 deletions kudos-compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
testImplementation(project(":kudos-gson"))
testImplementation(project(":kudos-jackson"))
testImplementation(project(":kudos-json-reader"))
testImplementation(project(":android-json-reader"))
testImplementation("org.jetbrains.kotlin:kotlin-noarg:1.8.20")
testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
testImplementation("com.bennyhuo.kotlin:kotlin-compile-testing-extensions:1.8.20-1.2.2")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.kanyun.kudos.compiler

import com.kanyun.kudos.compiler.KudosNames.KUDOS_JSON_ADAPTER_CLASS_ID
import com.kanyun.kudos.compiler.utils.irThis
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.jvm.ir.kClassReference
Expand Down Expand Up @@ -171,12 +172,12 @@ internal class KudosFromJsonFunctionBuilder(
field.type.isSubtypeOfClass(context.irBuiltIns.listClass) ||
field.type.isSubtypeOfClass(context.irBuiltIns.arrayClass) ||
field.type.isSubtypeOfClass(
pluginContext.referenceClass(ClassId(FqName("com.kanyun.kudos.adapter"), Name.identifier("KudosJsonAdapter")))!!,
pluginContext.referenceClass(KUDOS_JSON_ADAPTER_CLASS_ID)!!,
)
) {
irCall(
pluginContext.referenceFunctions(
CallableId(FqName("com.kanyun.kudos.adapter"), Name.identifier("parseKudosObject")),
CallableId(FqName("com.kanyun.kudos.json.reader.adapter"), Name.identifier("parseKudosObject")),
).first(),
).apply {
putValueArgument(0, irGet(jsonReader))
Expand Down Expand Up @@ -215,7 +216,7 @@ internal class KudosFromJsonFunctionBuilder(
return irCall(
pluginContext.referenceClass(
ClassId(
FqName("com.kanyun.kudos.adapter"),
FqName("com.kanyun.kudos.json.reader.adapter"),
Name.identifier("ParameterizedTypeImpl"),
),
)!!.constructors.single(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.kanyun.kudos.compiler

import com.kanyun.kudos.compiler.KudosNames.ADAPTER_FACTORY_NAME
import com.kanyun.kudos.compiler.KudosNames.JSON_ADAPTER_NAME
import com.kanyun.kudos.compiler.KudosNames.KUDOS_VALIDATOR_NAME
import com.kanyun.kudos.compiler.options.Options
import com.kanyun.kudos.compiler.utils.addOverride
import com.kanyun.kudos.compiler.utils.hasKudosAnnotation
Expand Down Expand Up @@ -92,16 +95,16 @@ class KudosIrClassTransformer(
}

private fun generateJsonAdapter() {
val jsonAdapter = context.referenceConstructors(ClassId.topLevel(FqName(JSON_ADAPTER))).firstOrNull()
val jsonAdapter = context.referenceConstructors(ClassId.topLevel(JSON_ADAPTER_NAME)).firstOrNull()
?: throw IllegalStateException(
"Constructors of class $JSON_ADAPTER not found while isGsonEnabled is set to true. " +
"Constructors of class ${JSON_ADAPTER_NAME.shortName()} not found while isGsonEnabled is set to true. " +
"Please check your dependencies to ensure the existing of the Gson library.",
)
irClass.annotations += IrConstructorCallImpl.fromSymbolOwner(
jsonAdapter.owner.returnType,
jsonAdapter.owner.symbol,
).apply {
val adapterFactory = context.referenceClass(ClassId.topLevel(FqName(ADAPTER_FACTORY)))!!
val adapterFactory = context.referenceClass(ClassId.topLevel(ADAPTER_FACTORY_NAME))!!

putValueArgument(
0,
Expand Down Expand Up @@ -230,7 +233,6 @@ class KudosIrClassTransformer(

if (nonDefaults.isEmpty() && collections.isEmpty() && arrays.isEmpty()) return

val kudosValidator = FqName(KUDOS_VALIDATOR)
val statusType = context.irBuiltIns.mapClass.typeWith(
context.irBuiltIns.stringType,
context.irBuiltIns.booleanType,
Expand All @@ -248,7 +250,7 @@ class KudosIrClassTransformer(
irClass.declarations.remove(validateFunction)
}

irClass.addOverride(kudosValidator, "validate", context.irBuiltIns.unitType, Modality.OPEN).apply {
irClass.addOverride(KUDOS_VALIDATOR_NAME, "validate", context.irBuiltIns.unitType, Modality.OPEN).apply {
dispatchReceiverParameter = irClass.thisReceiver!!.copyTo(this)
val statusParameter = addValueParameter {
name = Name.identifier("status")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2023 Kanyun, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.kanyun.kudos.compiler

import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name

object KudosNames {
// const
const val KUDOS_VALIDATOR = "com.kanyun.kudos.validator.KudosValidator"
const val KUDOS_JSON_ADAPTER = "com.kanyun.kudos.json.reader.adapter.KudosJsonAdapter"
const val KUDOS_IGNORE = "com.kanyun.kudos.annotations.KudosIgnore"
const val TRANSIENT = "kotlin.jvm.Transient"

// FqName
val KUDOS_NAME = FqName("com.kanyun.kudos.annotations.Kudos")
val KUDOS_VALIDATOR_NAME = FqName(KUDOS_VALIDATOR)
val KUDOS_IGNORE_NAME = FqName(KUDOS_IGNORE)
val TRANSIENT_NAME = FqName(TRANSIENT)

// Avoid package relocating
val JSON_ADAPTER_NAME = FqName("#com.google.gson.annotations.JsonAdapter".removePrefix("#"))
val ADAPTER_FACTORY_NAME = FqName("com.kanyun.kudos.gson.adapter.KudosReflectiveTypeAdapterFactory")

// ClassId
val KUDOS_VALIDATOR_CLASS_ID = ClassId(FqName("com.kanyun.kudos.validator"), Name.identifier("KudosValidator"))
val KUDOS_JSON_ADAPTER_CLASS_ID = ClassId(FqName("com.kanyun.kudos.json.reader.adapter"), Name.identifier("KudosJsonAdapter"))
val JSON_READER_CLASS_ID = ClassId.fromString("android/util/JsonReader")

// Name.identifier
val KUDOS_FROM_JSON_IDENTIFIER = Name.identifier("fromJson")
val JSON_READER_IDENTIFIER = Name.identifier("jsonReader")

val CONTAINER_FQ_NAMES = setOf(
"kotlin.Array",

"kotlin.collections.Collection",
"kotlin.collections.MutableCollection",

"kotlin.collections.List",
"kotlin.collections.MutableList",
"kotlin.collections.ArrayList",

"kotlin.collections.Set",
"kotlin.collections.MutableSet",
"kotlin.collections.HashSet",
"kotlin.collections.LinkedHashSet",

"kotlin.collections.Map",
"kotlin.collections.MutableMap",
"kotlin.collections.HashMap",
"kotlin.collections.LinkedHashMap",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.kanyun.kudos.compiler.k1

import com.kanyun.kudos.compiler.CONTAINER_FQ_NAMES
import com.kanyun.kudos.compiler.JSON_ADAPTER
import com.kanyun.kudos.compiler.KUDOS
import com.kanyun.kudos.compiler.KUDOS_IGNORE
import com.kanyun.kudos.compiler.TRANSIENT
import com.kanyun.kudos.compiler.KudosNames.CONTAINER_FQ_NAMES
import com.kanyun.kudos.compiler.KudosNames.JSON_ADAPTER_NAME
import com.kanyun.kudos.compiler.KudosNames.KUDOS_IGNORE
import com.kanyun.kudos.compiler.KudosNames.KUDOS_NAME
import com.kanyun.kudos.compiler.KudosNames.TRANSIENT
import com.kanyun.kudos.compiler.k1.diagnostic.KudosErrors
import com.kanyun.kudos.compiler.k1.utils.initializedWithThisReference
import com.kanyun.kudos.compiler.k1.utils.isLazyCall
Expand Down Expand Up @@ -53,7 +53,7 @@ class KudosDeclarationChecker(
if (
declaration is KtClass &&
descriptor is ClassDescriptor &&
descriptor.annotations.hasAnnotation(FqName(KUDOS))
descriptor.annotations.hasAnnotation(KUDOS_NAME)
) {
if (declaration.typeParameters.isNotEmpty()) {
context.trace.report(KudosErrors.GENERIC_TYPE.on(declaration))
Expand Down Expand Up @@ -94,7 +94,7 @@ class KudosDeclarationChecker(
context: DeclarationCheckerContext,
declaration: KtDeclaration,
) {
if (descriptor.annotations.hasAnnotation(FqName(JSON_ADAPTER))) {
if (descriptor.annotations.hasAnnotation(JSON_ADAPTER_NAME)) {
context.trace.report(KudosErrors.CONFLICTS_WITH_JSON_ADAPTER.on(declaration))
}
}
Expand Down Expand Up @@ -177,7 +177,7 @@ class KudosDeclarationChecker(
return emptyList()
}

if (type.hasAnnotation(KUDOS)) {
if (type.hasAnnotation(KUDOS_NAME)) {
return emptyList()
}

Expand All @@ -192,7 +192,7 @@ class KudosDeclarationChecker(
(constructor.declarationDescriptor as? TypeParameterDescriptor)?.representativeUpperBound?.getErasedUpperBound()
?: this

private fun KotlinType.hasAnnotation(fqName: String): Boolean {
return constructor.declarationDescriptor?.annotations?.hasAnnotation(FqName(fqName)) ?: false
private fun KotlinType.hasAnnotation(fqName: FqName): Boolean {
return constructor.declarationDescriptor?.annotations?.hasAnnotation(fqName) ?: false
}
}
Loading

0 comments on commit cb7bc97

Please sign in to comment.