mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Merge pull request #687 from hzsweers/z/removeExtensions
Completely remove companion object jsonAdapter extension function gen
This commit is contained in:
@@ -17,14 +17,12 @@ package com.squareup.moshi.kotlin.codegen
|
|||||||
|
|
||||||
import com.squareup.kotlinpoet.ARRAY
|
import com.squareup.kotlinpoet.ARRAY
|
||||||
import com.squareup.kotlinpoet.AnnotationSpec
|
import com.squareup.kotlinpoet.AnnotationSpec
|
||||||
import com.squareup.kotlinpoet.ClassName
|
|
||||||
import com.squareup.kotlinpoet.CodeBlock
|
import com.squareup.kotlinpoet.CodeBlock
|
||||||
import com.squareup.kotlinpoet.FileSpec
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
import com.squareup.kotlinpoet.FunSpec
|
import com.squareup.kotlinpoet.FunSpec
|
||||||
import com.squareup.kotlinpoet.KModifier
|
import com.squareup.kotlinpoet.KModifier
|
||||||
import com.squareup.kotlinpoet.NameAllocator
|
import com.squareup.kotlinpoet.NameAllocator
|
||||||
import com.squareup.kotlinpoet.ParameterSpec
|
import com.squareup.kotlinpoet.ParameterSpec
|
||||||
import com.squareup.kotlinpoet.ParameterizedTypeName
|
|
||||||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
|
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
|
||||||
import com.squareup.kotlinpoet.PropertySpec
|
import com.squareup.kotlinpoet.PropertySpec
|
||||||
import com.squareup.kotlinpoet.TypeSpec
|
import com.squareup.kotlinpoet.TypeSpec
|
||||||
@@ -40,7 +38,6 @@ import me.eugeniomarletti.kotlin.metadata.isDataClass
|
|||||||
import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.Visibility
|
import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.Visibility
|
||||||
import me.eugeniomarletti.kotlin.metadata.visibility
|
import me.eugeniomarletti.kotlin.metadata.visibility
|
||||||
import java.lang.reflect.Type
|
import java.lang.reflect.Type
|
||||||
import javax.annotation.processing.Messager
|
|
||||||
import javax.lang.model.element.TypeElement
|
import javax.lang.model.element.TypeElement
|
||||||
|
|
||||||
/** Generates a JSON adapter for a target type. */
|
/** Generates a JSON adapter for a target type. */
|
||||||
@@ -50,7 +47,6 @@ internal class AdapterGenerator(
|
|||||||
) {
|
) {
|
||||||
private val className = target.name
|
private val className = target.name
|
||||||
private val isDataClass = target.proto.isDataClass
|
private val isDataClass = target.proto.isDataClass
|
||||||
private val companionObjectName = target.companionObjectName
|
|
||||||
private val visibility = target.proto.visibility!!
|
private val visibility = target.proto.visibility!!
|
||||||
private val typeVariables = target.typeVariables
|
private val typeVariables = target.typeVariables
|
||||||
|
|
||||||
@@ -87,21 +83,18 @@ internal class AdapterGenerator(
|
|||||||
JsonReader.Options::class.asTypeName())
|
JsonReader.Options::class.asTypeName())
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
fun generateFile(messager: Messager, generatedOption: TypeElement?): FileSpec {
|
fun generateFile(generatedOption: TypeElement?): FileSpec {
|
||||||
for (property in propertyList) {
|
for (property in propertyList) {
|
||||||
property.allocateNames(nameAllocator)
|
property.allocateNames(nameAllocator)
|
||||||
}
|
}
|
||||||
|
|
||||||
val result = FileSpec.builder(className.packageName, adapterName)
|
val result = FileSpec.builder(className.packageName, adapterName)
|
||||||
result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.")
|
result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.")
|
||||||
companionObjectName?.let {
|
result.addType(generateType(generatedOption))
|
||||||
result.addFunction(generateJsonAdapterFun(it))
|
|
||||||
}
|
|
||||||
result.addType(generateType(messager, generatedOption))
|
|
||||||
return result.build()
|
return result.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateType(messager: Messager, generatedOption: TypeElement?): TypeSpec {
|
private fun generateType(generatedOption: TypeElement?): TypeSpec {
|
||||||
val result = TypeSpec.classBuilder(adapterName)
|
val result = TypeSpec.classBuilder(adapterName)
|
||||||
|
|
||||||
generatedOption?.let {
|
generatedOption?.let {
|
||||||
@@ -311,31 +304,4 @@ internal class AdapterGenerator(
|
|||||||
|
|
||||||
return result.build()
|
return result.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateJsonAdapterFun(name: String): FunSpec {
|
|
||||||
val rawType = when (originalTypeName) {
|
|
||||||
is TypeVariableName -> throw IllegalArgumentException("Cannot get raw type of TypeVariable!")
|
|
||||||
is ParameterizedTypeName -> originalTypeName.rawType
|
|
||||||
else -> originalTypeName as ClassName
|
|
||||||
}
|
|
||||||
|
|
||||||
val result = FunSpec.builder("jsonAdapter")
|
|
||||||
.receiver(rawType.nestedClass(name))
|
|
||||||
.returns(jsonAdapterTypeName)
|
|
||||||
.addParameter(moshiParam)
|
|
||||||
|
|
||||||
if (visibility == Visibility.INTERNAL) {
|
|
||||||
result.addModifiers(KModifier.INTERNAL)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeVariables.isNotEmpty()) {
|
|
||||||
result.addParameter(typesParam)
|
|
||||||
result.addTypeVariables(typeVariables)
|
|
||||||
result.addStatement("return %N(%N, %N)", adapterName, moshiParam, typesParam)
|
|
||||||
} else {
|
|
||||||
result.addStatement("return %N(%N)", adapterName, moshiParam)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.build()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -125,7 +125,7 @@ class JsonClassCodegenProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun AdapterGenerator.generateAndWrite(generatedOption: TypeElement?) {
|
private fun AdapterGenerator.generateAndWrite(generatedOption: TypeElement?) {
|
||||||
val fileSpec = generateFile(messager, generatedOption)
|
val fileSpec = generateFile(generatedOption)
|
||||||
val adapterName = fileSpec.members.filterIsInstance<TypeSpec>().first().name!!
|
val adapterName = fileSpec.members.filterIsInstance<TypeSpec>().first().name!!
|
||||||
val outputDir = generatedDir ?: mavenGeneratedDir(adapterName)
|
val outputDir = generatedDir ?: mavenGeneratedDir(adapterName)
|
||||||
fileSpec.writeTo(outputDir)
|
fileSpec.writeTo(outputDir)
|
||||||
|
@@ -53,8 +53,7 @@ internal data class TargetType(
|
|||||||
val element: TypeElement,
|
val element: TypeElement,
|
||||||
val constructor: TargetConstructor,
|
val constructor: TargetConstructor,
|
||||||
val properties: Map<String, TargetProperty>,
|
val properties: Map<String, TargetProperty>,
|
||||||
val typeVariables: List<TypeVariableName>,
|
val typeVariables: List<TypeVariableName>
|
||||||
val companionObjectName: String?
|
|
||||||
) {
|
) {
|
||||||
val name = element.className
|
val name = element.className
|
||||||
|
|
||||||
@@ -129,12 +128,7 @@ internal data class TargetType(
|
|||||||
properties.putIfAbsent(name, property)
|
properties.putIfAbsent(name, property)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val companionObjectName = if (proto.hasCompanionObjectName()) {
|
return TargetType(proto, element, constructor, properties, typeVariables)
|
||||||
typeMetadata.data.nameResolver.getQualifiedClassName(proto.companionObjectName)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
return TargetType(proto, element, constructor, properties, typeVariables, companionObjectName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the properties declared by `typeElement`. */
|
/** Returns the properties declared by `typeElement`. */
|
||||||
|
@@ -824,22 +824,6 @@ class GeneratedAdaptersTest {
|
|||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
class DuplicateValue(var a: Int = -1, var b: Int = -2)
|
class DuplicateValue(var a: Int = -1, var b: Int = -2)
|
||||||
|
|
||||||
@Test fun companionObjectsTests() {
|
|
||||||
val moshi = Moshi.Builder().build()
|
|
||||||
val standardAdapter = CompanionObjectClass.jsonAdapter(moshi)
|
|
||||||
val customNameAdapter = NamedCompanionObjectClass.jsonAdapter(moshi)
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class CompanionObjectClass(val foo: String) {
|
|
||||||
companion object
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class NamedCompanionObjectClass(val foo: String) {
|
|
||||||
companion object CustomCompanionObject
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test fun extensionProperty() {
|
@Test fun extensionProperty() {
|
||||||
val moshi = Moshi.Builder().build()
|
val moshi = Moshi.Builder().build()
|
||||||
val jsonAdapter = moshi.adapter(ExtensionProperty::class.java)
|
val jsonAdapter = moshi.adapter(ExtensionProperty::class.java)
|
||||||
|
Reference in New Issue
Block a user