diff --git a/android-json-reader/build.gradle.kts b/android-json-reader/build.gradle.kts new file mode 100644 index 0000000..2bbcd34 --- /dev/null +++ b/android-json-reader/build.gradle.kts @@ -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 { +} diff --git a/android-json-reader/gradle.properties b/android-json-reader/gradle.properties new file mode 100644 index 0000000..11bd880 --- /dev/null +++ b/android-json-reader/gradle.properties @@ -0,0 +1 @@ +POM_NAME=android-json-reader diff --git a/kudos-json-reader/src/main/java/android/util/JsonReader.java b/android-json-reader/src/main/java/android/util/JsonReader.java similarity index 100% rename from kudos-json-reader/src/main/java/android/util/JsonReader.java rename to android-json-reader/src/main/java/android/util/JsonReader.java diff --git a/kudos-json-reader/src/main/java/android/util/JsonScope.java b/android-json-reader/src/main/java/android/util/JsonScope.java similarity index 100% rename from kudos-json-reader/src/main/java/android/util/JsonScope.java rename to android-json-reader/src/main/java/android/util/JsonScope.java diff --git a/kudos-json-reader/src/main/java/android/util/JsonToken.java b/android-json-reader/src/main/java/android/util/JsonToken.java similarity index 100% rename from kudos-json-reader/src/main/java/android/util/JsonToken.java rename to android-json-reader/src/main/java/android/util/JsonToken.java diff --git a/kudos-json-reader/src/main/java/android/util/MalformedJsonException.java b/android-json-reader/src/main/java/android/util/MalformedJsonException.java similarity index 100% rename from kudos-json-reader/src/main/java/android/util/MalformedJsonException.java rename to android-json-reader/src/main/java/android/util/MalformedJsonException.java diff --git a/kudos-json-reader/src/main/java/com/android/internal/util/StringPool.java b/android-json-reader/src/main/java/com/android/internal/util/StringPool.java similarity index 100% rename from kudos-json-reader/src/main/java/com/android/internal/util/StringPool.java rename to android-json-reader/src/main/java/com/android/internal/util/StringPool.java diff --git a/kudos-compiler/build.gradle.kts b/kudos-compiler/build.gradle.kts index 1e77696..767f72b 100644 --- a/kudos-compiler/build.gradle.kts +++ b/kudos-compiler/build.gradle.kts @@ -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") diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosConsts.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosConsts.kt deleted file mode 100644 index 97be19d..0000000 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosConsts.kt +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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 - -/** - * Created by Benny Huo on 2023/8/21 - */ -const val KUDOS = "com.kanyun.kudos.annotations.Kudos" -const val KUDOS_IGNORE = "com.kanyun.kudos.annotations.KudosIgnore" -const val KUDOS_META = "com.kanyun.kudos.annotations.KudosMeta" -const val KUDOS_VALIDATOR = "com.kanyun.kudos.validator.KudosValidator" -const val VALIDATE_FIELD = "com.kanyun.kudos.validator.validateField" -const val VALIDATE_COLLECTION = "com.kanyun.kudos.validator.validateCollection" -const val VALIDATE_ARRAY = "com.kanyun.kudos.validator.validateArray" -const val KUDOS_JSON_ADAPTER = "com.kanyun.kudos.adapter.KudosJsonAdapter" -const val KUDOS_FROM_JSON_FUNCTION_NAME = "fromJson" - -const val TRANSIENT = "kotlin.jvm.Transient" - -const val ADAPTER_FACTORY = "com.kanyun.kudos.gson.adapter.KudosReflectiveTypeAdapterFactory" - -// Avoid package relocating -val JSON_ADAPTER = "#com.google.gson.annotations.JsonAdapter".removePrefix("#") - -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", -) - -fun String.toNonNullContainerType(): String? { - return if (this in CONTAINER_FQ_NAMES) { - "com.kanyun.kudos.runtime.collections.NonNull${removePrefix("kotlin.collections.")}" - } else { - null - } -} diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosFromJsonFunctionBuilder.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosFromJsonFunctionBuilder.kt index 080d851..3440851 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosFromJsonFunctionBuilder.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosFromJsonFunctionBuilder.kt @@ -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 @@ -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)) @@ -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(), diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosIrClassTransformer.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosIrClassTransformer.kt index efef1b3..f2b3b04 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosIrClassTransformer.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosIrClassTransformer.kt @@ -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 @@ -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, @@ -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, @@ -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") diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosNames.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosNames.kt new file mode 100644 index 0000000..9301ba2 --- /dev/null +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/KudosNames.kt @@ -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", + ) +} diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosDeclarationChecker.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosDeclarationChecker.kt index 9243dc5..a7ee0b9 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosDeclarationChecker.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosDeclarationChecker.kt @@ -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 @@ -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)) @@ -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)) } } @@ -177,7 +177,7 @@ class KudosDeclarationChecker( return emptyList() } - if (type.hasAnnotation(KUDOS)) { + if (type.hasAnnotation(KUDOS_NAME)) { return emptyList() } @@ -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 } } diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosSyntheticResolveExtension.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosSyntheticResolveExtension.kt index 75f3602..18573e1 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosSyntheticResolveExtension.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/KudosSyntheticResolveExtension.kt @@ -16,10 +16,14 @@ package com.kanyun.kudos.compiler.k1 -import com.kanyun.kudos.compiler.KUDOS -import com.kanyun.kudos.compiler.KUDOS_FROM_JSON_FUNCTION_NAME -import com.kanyun.kudos.compiler.KUDOS_JSON_ADAPTER -import com.kanyun.kudos.compiler.KUDOS_VALIDATOR +import com.kanyun.kudos.compiler.KudosNames.JSON_READER_CLASS_ID +import com.kanyun.kudos.compiler.KudosNames.JSON_READER_IDENTIFIER +import com.kanyun.kudos.compiler.KudosNames.KUDOS_FROM_JSON_IDENTIFIER +import com.kanyun.kudos.compiler.KudosNames.KUDOS_JSON_ADAPTER +import com.kanyun.kudos.compiler.KudosNames.KUDOS_JSON_ADAPTER_CLASS_ID +import com.kanyun.kudos.compiler.KudosNames.KUDOS_NAME +import com.kanyun.kudos.compiler.KudosNames.KUDOS_VALIDATOR +import com.kanyun.kudos.compiler.KudosNames.KUDOS_VALIDATOR_CLASS_ID import com.kanyun.kudos.compiler.k1.symbol.FromJsonFunctionDescriptorImpl import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassKind @@ -29,8 +33,6 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl import org.jetbrains.kotlin.js.descriptorUtils.getKotlinTypeFqName -import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.descriptorUtil.module @@ -49,7 +51,7 @@ class KudosSyntheticResolveExtension : SyntheticResolveExtension { supertypes: MutableList, ) { if (thisDescriptor.kind != ClassKind.CLASS) return - if (thisDescriptor.annotations.hasAnnotation(FqName(KUDOS))) { + if (thisDescriptor.annotations.hasAnnotation(KUDOS_NAME)) { val superTypeNames = supertypes.asSequence().flatMap { listOf(it) + it.supertypes() }.map { @@ -57,13 +59,7 @@ class KudosSyntheticResolveExtension : SyntheticResolveExtension { }.toSet() if (KUDOS_VALIDATOR !in superTypeNames) { - val kudosValidator = thisDescriptor.module.findClassAcrossModuleDependencies( - ClassId( - FqName("com.kanyun.kudos.validator"), - Name.identifier("KudosValidator"), - ), - )!! - + val kudosValidator = thisDescriptor.module.findClassAcrossModuleDependencies(KUDOS_VALIDATOR_CLASS_ID)!! supertypes.add( KotlinTypeFactory.simpleNotNullType( TypeAttributes.Empty, @@ -73,13 +69,7 @@ class KudosSyntheticResolveExtension : SyntheticResolveExtension { ) } if (KUDOS_JSON_ADAPTER !in superTypeNames) { - val kudosValidator = thisDescriptor.module.findClassAcrossModuleDependencies( - ClassId( - FqName("com.kanyun.kudos.adapter"), - Name.identifier("KudosJsonAdapter"), - ), - )!! - + val kudosValidator = thisDescriptor.module.findClassAcrossModuleDependencies(KUDOS_JSON_ADAPTER_CLASS_ID)!! supertypes.add( KotlinTypeFactory.simpleNotNullType( TypeAttributes.Empty, @@ -92,8 +82,8 @@ class KudosSyntheticResolveExtension : SyntheticResolveExtension { } override fun getSyntheticFunctionNames(thisDescriptor: ClassDescriptor): List { - if (thisDescriptor.annotations.hasAnnotation(FqName(KUDOS))) { - return listOf(Name.identifier(KUDOS_FROM_JSON_FUNCTION_NAME)) + if (thisDescriptor.annotations.hasAnnotation(KUDOS_NAME)) { + return listOf(KUDOS_FROM_JSON_IDENTIFIER) } return super.getSyntheticFunctionNames(thisDescriptor) } @@ -105,10 +95,9 @@ class KudosSyntheticResolveExtension : SyntheticResolveExtension { fromSupertypes: List, result: MutableCollection, ) { - if (name.identifier == KUDOS_FROM_JSON_FUNCTION_NAME) { - if (thisDescriptor.annotations.hasAnnotation(FqName(KUDOS))) { - val jsonReaderClassId = ClassId.fromString("android/util/JsonReader") - val jsonReaderType: SimpleType = thisDescriptor.module.findClassAcrossModuleDependencies(jsonReaderClassId)?.defaultType ?: return + if (name.identifier == KUDOS_FROM_JSON_IDENTIFIER.identifier) { + if (thisDescriptor.annotations.hasAnnotation(KUDOS_NAME)) { + val jsonReaderType: SimpleType = thisDescriptor.module.findClassAcrossModuleDependencies(JSON_READER_CLASS_ID)?.defaultType ?: return result += FromJsonFunctionDescriptorImpl(thisDescriptor).apply { val valueParameterDescriptor = ValueParameterDescriptorImpl( @@ -116,7 +105,7 @@ class KudosSyntheticResolveExtension : SyntheticResolveExtension { original = null, index = 0, annotations = Annotations.EMPTY, - name = Name.identifier("jsonReader"), + name = JSON_READER_IDENTIFIER, outType = jsonReaderType, declaresDefaultValue = false, isCrossinline = false, diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/symbol/FromJsonFunctionDescriptorImpl.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/symbol/FromJsonFunctionDescriptorImpl.kt index ef10103..8a1fae3 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/symbol/FromJsonFunctionDescriptorImpl.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k1/symbol/FromJsonFunctionDescriptorImpl.kt @@ -16,7 +16,7 @@ package com.kanyun.kudos.compiler.k1.symbol -import com.kanyun.kudos.compiler.KUDOS_FROM_JSON_FUNCTION_NAME +import com.kanyun.kudos.compiler.KudosNames.KUDOS_FROM_JSON_IDENTIFIER import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DescriptorVisibilities @@ -24,7 +24,6 @@ import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl -import org.jetbrains.kotlin.name.Name class FromJsonFunctionDescriptorImpl( private val classDescriptor: ClassDescriptor, @@ -32,7 +31,7 @@ class FromJsonFunctionDescriptorImpl( classDescriptor, null, Annotations.EMPTY, - Name.identifier(KUDOS_FROM_JSON_FUNCTION_NAME), + KUDOS_FROM_JSON_IDENTIFIER, CallableMemberDescriptor.Kind.SYNTHESIZED, classDescriptor.source, ) { diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirClassChecker.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirClassChecker.kt index 1a45da7..5033fff 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirClassChecker.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirClassChecker.kt @@ -16,11 +16,11 @@ package com.kanyun.kudos.compiler.k2 -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_NAME +import com.kanyun.kudos.compiler.KudosNames.KUDOS_NAME +import com.kanyun.kudos.compiler.KudosNames.TRANSIENT_NAME import com.kanyun.kudos.compiler.k2.diagnostic.KudosKtErrors import com.kanyun.kudos.compiler.k2.utils.initializedWithThisReference import com.kanyun.kudos.compiler.k2.utils.isLazyCall @@ -67,7 +67,7 @@ class KudosFirClassChecker( private val noArgAnnotations: List, ) : FirClassChecker() { override fun check(declaration: FirClass, context: CheckerContext, reporter: DiagnosticReporter) { - if (declaration.hasAnnotation(ClassId.topLevel(FqName(KUDOS)), context.session)) { + if (declaration.hasAnnotation(ClassId.topLevel(KUDOS_NAME), context.session)) { if (declaration.typeParameters.isNotEmpty()) { reporter.reportOn(declaration.source, KudosKtErrors.GENERIC_TYPE, context) } else { @@ -110,7 +110,7 @@ class KudosFirClassChecker( context: CheckerContext, reporter: DiagnosticReporter, ) { - if (declaration.hasAnnotation(ClassId.topLevel(FqName(JSON_ADAPTER)), context.session)) { + if (declaration.hasAnnotation(ClassId.topLevel(JSON_ADAPTER_NAME), context.session)) { reporter.reportOn(declaration.source, KudosKtErrors.CONFLICTS_WITH_JSON_ADAPTER, context) } } @@ -173,8 +173,8 @@ class KudosFirClassChecker( reporter: DiagnosticReporter, ) { if (symbol.hasBackingField && !symbol.hasDelegate && - !symbol.hasAnnotation(ClassId.topLevel(FqName(KUDOS_IGNORE)), context.session) && - !symbol.hasAnnotation(ClassId.topLevel(FqName(TRANSIENT)), context.session) + !symbol.hasAnnotation(ClassId.topLevel(KUDOS_IGNORE_NAME), context.session) && + !symbol.hasAnnotation(ClassId.topLevel(TRANSIENT_NAME), context.session) ) { for (type in findNonKudosType(symbol.resolvedReturnType, context)) { reporter.reportOn( @@ -211,7 +211,7 @@ class KudosFirClassChecker( if (annotations != null) { for (annotation in annotations) { val fqName = annotation.fqName(context.session) ?: continue - if (fqName.asString() == KUDOS) { + if (fqName.asString() == KUDOS_NAME.asString()) { return emptyList() } } diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirDeclarationGenerator.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirDeclarationGenerator.kt index ad2f271..82a5c9f 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirDeclarationGenerator.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirDeclarationGenerator.kt @@ -16,8 +16,10 @@ package com.kanyun.kudos.compiler.k2 -import com.kanyun.kudos.compiler.KUDOS -import com.kanyun.kudos.compiler.KUDOS_FROM_JSON_FUNCTION_NAME +import com.kanyun.kudos.compiler.KudosNames.JSON_READER_CLASS_ID +import com.kanyun.kudos.compiler.KudosNames.JSON_READER_IDENTIFIER +import com.kanyun.kudos.compiler.KudosNames.KUDOS_FROM_JSON_IDENTIFIER +import com.kanyun.kudos.compiler.KudosNames.KUDOS_NAME import org.jetbrains.kotlin.GeneratedDeclarationKey import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.fir.FirSession @@ -42,10 +44,7 @@ import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.classId import org.jetbrains.kotlin.fir.types.coneType import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl -import org.jetbrains.kotlin.javac.resolve.classId import org.jetbrains.kotlin.name.CallableId -import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.utils.addToStdlib.runIf @@ -54,9 +53,9 @@ class KudosFirDeclarationGenerator(session: FirSession) : companion object { private val PREDICATE = LookupPredicate.create { - annotated(FqName(KUDOS)) + annotated(KUDOS_NAME) } - private val kudosMethodsNames = setOf(Name.identifier(KUDOS_FROM_JSON_FUNCTION_NAME)) + private val kudosMethodsNames = setOf(KUDOS_FROM_JSON_IDENTIFIER) } private val matchedClasses by lazy { @@ -85,7 +84,7 @@ class KudosFirDeclarationGenerator(session: FirSession) : ): List { val owner = context?.owner ?: return emptyList() require(owner is FirRegularClassSymbol) - if (callableId.callableName == Name.identifier(KUDOS_FROM_JSON_FUNCTION_NAME)) { + if (callableId.callableName == KUDOS_FROM_JSON_IDENTIFIER) { val declaredFunctions = owner.declarationSymbols.filterIsInstance() val function = runIf(declaredFunctions.none { it.isFromJson() }) { @@ -103,8 +102,8 @@ class KudosFirDeclarationGenerator(session: FirSession) : returnType, ) { valueParameter( - Name.identifier("jsonReader"), - ClassId.fromString("android/util/JsonReader").createConeType(session), + JSON_READER_IDENTIFIER, + JSON_READER_CLASS_ID.createConeType(session), ) } } @@ -127,11 +126,11 @@ class KudosFirDeclarationGenerator(session: FirSession) : } private fun FirNamedFunctionSymbol.isFromJson(): Boolean { - if (name != Name.identifier(KUDOS_FROM_JSON_FUNCTION_NAME)) return false + if (name != KUDOS_FROM_JSON_IDENTIFIER) return false val parameterSymbols = valueParameterSymbols if (parameterSymbols.size != 1) return false val jsonReaderSymbol = parameterSymbols[0] - if (jsonReaderSymbol.resolvedReturnTypeRef.coneType.classId != ClassId.fromString("android/util/JsonReader")) return false + if (jsonReaderSymbol.resolvedReturnTypeRef.coneType.classId != JSON_READER_CLASS_ID) return false return true } } diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirSupertypeGenerationExtension.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirSupertypeGenerationExtension.kt index 0323ba4..37fa4b8 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirSupertypeGenerationExtension.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/k2/KudosFirSupertypeGenerationExtension.kt @@ -16,7 +16,9 @@ package com.kanyun.kudos.compiler.k2 -import com.kanyun.kudos.compiler.KUDOS +import com.kanyun.kudos.compiler.KudosNames.KUDOS_JSON_ADAPTER_CLASS_ID +import com.kanyun.kudos.compiler.KudosNames.KUDOS_NAME +import com.kanyun.kudos.compiler.KudosNames.KUDOS_VALIDATOR_CLASS_ID import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.FirClass import org.jetbrains.kotlin.fir.declarations.FirClassLikeDeclaration @@ -40,9 +42,7 @@ import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.constructClassLikeType import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl import org.jetbrains.kotlin.fir.types.toSymbol -import org.jetbrains.kotlin.javac.resolve.classId import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName /** * Created by benny at 2023/5/29 14:47. @@ -52,7 +52,7 @@ class KudosFirSupertypeGenerationExtension( ) : FirSupertypeGenerationExtension(session) { private val hasKudos = DeclarationPredicate.create { - annotated(FqName(KUDOS)) + annotated(KUDOS_NAME) } override fun FirDeclarationPredicateRegistrar.registerPredicates() { @@ -64,17 +64,15 @@ class KudosFirSupertypeGenerationExtension( classLikeDeclaration: FirClassLikeDeclaration, resolvedSupertypes: List, ): List { - val kudosValidatorClassId = classId("com.kanyun.kudos.validator", "KudosValidator") - val kudosJsonAdapterClassId = classId("com.kanyun.kudos.adapter", "KudosJsonAdapter") var hasValidator = false var hasJsonAdapter = false for (superTypeRef in resolvedSupertypes) { val superType = superTypeRef.type val superTypeClassIds = superType.allSuperTypeClassIds() - if (kudosValidatorClassId in superTypeClassIds) { + if (KUDOS_VALIDATOR_CLASS_ID in superTypeClassIds) { hasValidator = true } - if (kudosJsonAdapterClassId in superTypeClassIds) { + if (KUDOS_JSON_ADAPTER_CLASS_ID in superTypeClassIds) { hasJsonAdapter = true } } @@ -82,7 +80,7 @@ class KudosFirSupertypeGenerationExtension( val firTypeRefList = mutableListOf() if (!hasValidator) { firTypeRefList += buildResolvedTypeRef { - type = kudosValidatorClassId.constructClassLikeType( + type = KUDOS_VALIDATOR_CLASS_ID.constructClassLikeType( emptyArray(), isNullable = false, ) @@ -97,7 +95,7 @@ class KudosFirSupertypeGenerationExtension( false, ) firTypeRefList += buildResolvedTypeRef { - type = kudosJsonAdapterClassId.constructClassLikeType( + type = KUDOS_JSON_ADAPTER_CLASS_ID.constructClassLikeType( arrayOf(genericType), isNullable = false, ) diff --git a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/utils/IrClass.kt b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/utils/IrClass.kt index bfacc56..0ae008a 100644 --- a/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/utils/IrClass.kt +++ b/kudos-compiler/src/main/java/com/kanyun/kudos/compiler/utils/IrClass.kt @@ -16,7 +16,7 @@ package com.kanyun.kudos.compiler.utils -import com.kanyun.kudos.compiler.KUDOS +import com.kanyun.kudos.compiler.KudosNames.KUDOS_NAME import org.jetbrains.kotlin.backend.jvm.ir.erasedUpperBound import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.ir.builders.declarations.addFunction @@ -33,7 +33,7 @@ import org.jetbrains.kotlin.name.FqName * Created by Benny Huo on 2023/9/8 */ fun IrClass.hasKudosAnnotation(): Boolean { - return hasAnnotation(FqName(KUDOS)) + return hasAnnotation(KUDOS_NAME) } fun IrClass.isSubclassOfFqName(fqName: String): Boolean = diff --git a/kudos-compiler/src/test/java/com/kanyun/kudos/compiler/base/TestBase.kt b/kudos-compiler/src/test/java/com/kanyun/kudos/compiler/base/TestBase.kt index b8f432b..5996c3b 100644 --- a/kudos-compiler/src/test/java/com/kanyun/kudos/compiler/base/TestBase.kt +++ b/kudos-compiler/src/test/java/com/kanyun/kudos/compiler/base/TestBase.kt @@ -109,11 +109,11 @@ open class TestBase { private fun jsonReaderDeserialize(): String { return """ // FILE: JsonReader.kt - import com.kanyun.kudos.Kudos + import com.kanyun.kudos.json.reader.KudosJsonReader inline fun deserialize(string: String): T? { return try { - val t: T = Kudos.fromJson(string) + val t: T = KudosJsonReader.fromJson(string) println(t) t } catch (e: Exception) { diff --git a/kudos-gradle-plugin/src/main/java/com/kanyun/kudos/gradle/KudosGradlePlugin.kt b/kudos-gradle-plugin/src/main/java/com/kanyun/kudos/gradle/KudosGradlePlugin.kt index 9e63e8f..3a68d65 100644 --- a/kudos-gradle-plugin/src/main/java/com/kanyun/kudos/gradle/KudosGradlePlugin.kt +++ b/kudos-gradle-plugin/src/main/java/com/kanyun/kudos/gradle/KudosGradlePlugin.kt @@ -46,7 +46,8 @@ open class KudosGradlePlugin : KotlinCompilerPluginSupportPlugin { if (kudosExtension.jackson) { config += "${BuildConfig.KOTLIN_PLUGIN_GROUP}:kudos-jackson:${BuildConfig.KOTLIN_PLUGIN_VERSION}" } - + // TODO: Add json reader config + config += "${BuildConfig.KOTLIN_PLUGIN_GROUP}:kudos-json-reader:${BuildConfig.KOTLIN_PLUGIN_VERSION}" config += "${BuildConfig.KOTLIN_PLUGIN_GROUP}:kudos-annotations:${BuildConfig.KOTLIN_PLUGIN_VERSION}" config += "${BuildConfig.KOTLIN_PLUGIN_GROUP}:kudos-runtime:${BuildConfig.KOTLIN_PLUGIN_VERSION}" } diff --git a/kudos-json-reader/build.gradle.kts b/kudos-json-reader/build.gradle.kts index 2bbcd34..7f12503 100644 --- a/kudos-json-reader/build.gradle.kts +++ b/kudos-json-reader/build.gradle.kts @@ -19,4 +19,7 @@ plugins { } dependencies { + compileOnly(project(":android-json-reader")) + api(project(":kudos-annotations")) + api(project(":kudos-runtime")) } diff --git a/kudos-json-reader/gradle.properties b/kudos-json-reader/gradle.properties new file mode 100644 index 0000000..c1bbea5 --- /dev/null +++ b/kudos-json-reader/gradle.properties @@ -0,0 +1 @@ +POM_NAME=kudos-json-reader diff --git a/kudos-runtime/src/main/java/com/kanyun/kudos/Kudos.kt b/kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/KudosJsonReader.kt similarity index 90% rename from kudos-runtime/src/main/java/com/kanyun/kudos/Kudos.kt rename to kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/KudosJsonReader.kt index 2b95e2c..2f90445 100644 --- a/kudos-runtime/src/main/java/com/kanyun/kudos/Kudos.kt +++ b/kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/KudosJsonReader.kt @@ -14,12 +14,12 @@ * limitations under the License. */ -package com.kanyun.kudos +package com.kanyun.kudos.json.reader import android.util.JsonReader -import com.kanyun.kudos.adapter.KudosJsonAdapter +import com.kanyun.kudos.json.reader.adapter.KudosJsonAdapter -object Kudos { +object KudosJsonReader { inline fun fromJson(json: String): T { return fromJson(json, T::class.java) } diff --git a/kudos-runtime/src/main/java/com/kanyun/kudos/adapter/KudosJsonAdapter.kt b/kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/adapter/KudosJsonAdapter.kt similarity index 98% rename from kudos-runtime/src/main/java/com/kanyun/kudos/adapter/KudosJsonAdapter.kt rename to kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/adapter/KudosJsonAdapter.kt index 5204fb8..38aa567 100644 --- a/kudos-runtime/src/main/java/com/kanyun/kudos/adapter/KudosJsonAdapter.kt +++ b/kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/adapter/KudosJsonAdapter.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.kanyun.kudos.adapter +package com.kanyun.kudos.json.reader.adapter import android.util.JsonReader import java.lang.reflect.Type diff --git a/kudos-runtime/src/main/java/com/kanyun/kudos/adapter/ParameterizedTypeImpl.kt b/kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/adapter/ParameterizedTypeImpl.kt similarity index 95% rename from kudos-runtime/src/main/java/com/kanyun/kudos/adapter/ParameterizedTypeImpl.kt rename to kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/adapter/ParameterizedTypeImpl.kt index 2b3fe15..086129a 100644 --- a/kudos-runtime/src/main/java/com/kanyun/kudos/adapter/ParameterizedTypeImpl.kt +++ b/kudos-json-reader/src/main/java/com/kanyun/kudos/json/reader/adapter/ParameterizedTypeImpl.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.kanyun.kudos.adapter +package com.kanyun.kudos.json.reader.adapter import java.lang.reflect.ParameterizedType import java.lang.reflect.Type diff --git a/kudos-runtime/build.gradle.kts b/kudos-runtime/build.gradle.kts index 799d67e..b1e8042 100644 --- a/kudos-runtime/build.gradle.kts +++ b/kudos-runtime/build.gradle.kts @@ -17,7 +17,6 @@ plugins { java kotlin("jvm") } - dependencies { - compileOnly(project(":kudos-json-reader")) } + diff --git a/kudos-sample/kudos-gradle-sample/build.gradle.kts b/kudos-sample/kudos-gradle-sample/build.gradle.kts index 4892468..8f5cd9d 100644 --- a/kudos-sample/kudos-gradle-sample/build.gradle.kts +++ b/kudos-sample/kudos-gradle-sample/build.gradle.kts @@ -25,6 +25,8 @@ repositories { maven("https://s01.oss.sonatype.org/content/repositories/snapshots") } +val KUDOS_VERSION: String by extra + kudos { gson = true jackson = true @@ -33,7 +35,7 @@ kudos { dependencies { implementation("com.google.code.gson:gson:2.10") implementation("com.fasterxml.jackson.core:jackson-databind:2.15.0") - implementation("com.kanyun.kudos:kudos-json-reader:0.0.0-SNAPSHOT") + implementation("com.kanyun.kudos:android-json-reader:$KUDOS_VERSION") testImplementation("org.jetbrains.kotlin:kotlin-test-junit") testImplementation("junit:junit:4.13.1") } diff --git a/kudos-sample/kudos-maven-sample/pom.xml b/kudos-sample/kudos-maven-sample/pom.xml index b768552..ff46efa 100644 --- a/kudos-sample/kudos-maven-sample/pom.xml +++ b/kudos-sample/kudos-maven-sample/pom.xml @@ -34,6 +34,11 @@ kudos-jackson ${kudos.version} + + com.kanyun.kudos + kudos-json-reader + ${kudos.version} + com.google.code.gson gson @@ -46,8 +51,8 @@ com.kanyun.kudos - kudos-json-reader - 0.0.0-SNAPSHOT + android-json-reader + ${kudos.version} org.jetbrains.kotlin diff --git a/settings.gradle.kts b/settings.gradle.kts index 00023ea..c42a2af 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -14,3 +14,4 @@ include(":kudos-runtime") include(":kudos-gson") include(":kudos-jackson") include(":kudos-json-reader") +include(":android-json-reader")