Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump org.jetbrains.kotlin:kotlin-gradle-plugin from 1.9.24 to 2.0.0 #310

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ plugins {
}

tasks.compileKotlin {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + listOf(
compilerOptions {
freeCompilerArgs.addAll(
"-opt-in=com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.google.common.collect.SetMultimap
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.asTypeVariableName
import com.squareup.moshi.JsonAdapter
import kotlinx.metadata.ClassKind
import kotlinx.metadata.Modality
import kotlinx.metadata.Visibility
Expand All @@ -17,7 +16,6 @@ import kotlinx.metadata.kind
import kotlinx.metadata.modality
import kotlinx.metadata.visibility
import se.ansman.kotshi.Errors
import se.ansman.kotshi.Errors.abstractFactoriesAreDeprecated
import se.ansman.kotshi.ExperimentalKotshiApi
import se.ansman.kotshi.KotshiJsonAdapterFactory
import se.ansman.kotshi.RegisterJsonAdapter
Expand All @@ -33,14 +31,10 @@ import javax.annotation.processing.Filer
import javax.annotation.processing.Messager
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.element.Element
import javax.lang.model.element.ElementKind
import javax.lang.model.element.Modifier
import javax.lang.model.element.TypeElement
import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeMirror
import javax.lang.model.util.Elements
import javax.lang.model.util.Types
import kotlin.reflect.KClass

class FactoryProcessingStep(
override val processor: KotshiProcessor,
Expand Down Expand Up @@ -84,18 +78,6 @@ class FactoryProcessingStep(
val elementClassName = createClassName(metadataAccessor.getKmClass(element).name)
val factory = JsonAdapterFactory(
targetType = elementClassName,
usageType = if (
element.asType().implements(JsonAdapter.Factory::class) &&
Modifier.ABSTRACT in element.modifiers
) {
messager.logKotshiWarning(abstractFactoriesAreDeprecated, element)
JsonAdapterFactory.UsageType.Subclass(
elementClassName,
parentIsInterface = element.kind == ElementKind.INTERFACE
)
} else {
JsonAdapterFactory.UsageType.Standalone
},
generatedAdapters = generatedAdapters,
manuallyRegisteredAdapters = manuallyRegisteredAdapters,
)
Expand Down Expand Up @@ -199,7 +181,4 @@ class FactoryProcessingStep(
}
)
}

private fun TypeMirror.implements(someType: KClass<*>): Boolean =
types.isSubtype(this, elements.getTypeElement(someType.java.canonicalName).asType())
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package se.ansman.kotshi.ksp

import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.getVisibility
import com.google.devtools.ksp.isAbstract
import com.google.devtools.ksp.isAnnotationPresent
Expand All @@ -12,6 +11,7 @@ import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.Modifier
import com.google.devtools.ksp.symbol.Origin
import com.google.devtools.ksp.symbol.Visibility
Expand All @@ -21,10 +21,8 @@ import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeName
import com.squareup.kotlinpoet.ksp.toTypeVariableName
import com.squareup.kotlinpoet.ksp.writeTo
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonQualifier
import se.ansman.kotshi.Errors
import se.ansman.kotshi.Errors.abstractFactoriesAreDeprecated
import se.ansman.kotshi.Errors.javaClassNotSupported
import se.ansman.kotshi.Errors.polymorphicClassMustHaveJsonSerializable
import se.ansman.kotshi.Errors.unsupportedFactoryType
Expand Down Expand Up @@ -227,22 +225,8 @@ class KotshiSymbolProcessor(private val environment: SymbolProcessorEnvironment)

if (targetFactory != null) {
try {
val jsonAdapterFactoryType = resolver.getClassDeclarationByName<JsonAdapter.Factory>()!!
.asType(emptyList())
val factory = JsonAdapterFactory(
targetType = targetFactory.toClassName(),
usageType = if (
targetFactory.classKind == ClassKind.INTERFACE ||
targetFactory.asType(emptyList()).isAssignableFrom(jsonAdapterFactoryType)
) {
environment.logger.logKotshiWarning(abstractFactoriesAreDeprecated, targetFactory)
JsonAdapterFactory.UsageType.Subclass(
parent = targetFactory.toClassName(),
parentIsInterface = targetFactory.classKind == ClassKind.INTERFACE
)
} else {
JsonAdapterFactory.UsageType.Standalone
},
generatedAdapters = generatedAdapters,
manuallyRegisteredAdapters = manualAdapters,
)
Expand Down Expand Up @@ -275,6 +259,10 @@ class KotshiSymbolProcessor(private val environment: SymbolProcessorEnvironment)
return@mapNotNull null
}
if (annotated !is KSClassDeclaration) {
if (annotated is KSFunctionDeclaration && annotated.origin == Origin.SYNTHETIC) {
// This is a workaround for https://github.com/google/ksp/issues/1996
return@mapNotNull null
}
environment.logger.logKotshiError(Errors.unsupportedSerializableType, annotated)
return@mapNotNull null
}
Expand Down
30 changes: 21 additions & 9 deletions compiler/src/main/kotlin/se/ansman/kotshi/ksp/annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,22 @@ inline fun <reified V> KSAnnotation.getValue(name: String): V =

inline fun <reified V> KSAnnotation.getValueOrDefault(name: String, defaultValue: () -> V): V {
val arg = arguments.firstOrNull { it.name?.asString() == name } ?: return defaultValue()
return arg.value as V
if (arg.value is V) {
return arg.value as V
} else {
error("Expected ${arg.value} to be of type ${V::class.java} but was ${arg.value?.javaClass}")
}
}

inline fun <reified V : Enum<V>> KSAnnotation.getEnumValue(name: String, defaultValue: V): V =
getValue<KSType?>(name)?.let { enumValueOf<V>(it.declaration.simpleName.getShortName()) } ?: defaultValue
inline fun <reified V : Enum<V>> KSAnnotation.getEnumValue(name: String, defaultValue: V): V {
val declaration = when (val value = getValue<Any?>(name)) {
is KSType -> value.declaration
is KSClassDeclaration -> value
null -> return defaultValue
else -> error("Expected $value to be a KSType or KSClassDeclaration but was ${value.javaClass}")
}
return enumValueOf<V>(declaration.simpleName.getShortName())
}

fun KSAnnotation.isJsonQualifier(): Boolean =
annotationType.resolve().declaration.annotations.any {
Expand Down Expand Up @@ -100,18 +111,19 @@ fun KSAnnotation.toAnnotationModel(): AnnotationModel {

private fun Any.toAnnotationValue(node: KSNode, type: TypeName): Value<*> =
when (this) {
is KSType -> {
val declaration = declaration as KSClassDeclaration
if (declaration.classKind == ClassKind.ENUM_ENTRY) {
is KSClassDeclaration -> {
if (classKind == ClassKind.ENUM_ENTRY) {
Value.Enum(
(declaration.parentDeclaration as KSClassDeclaration).toClassName(),
declaration.simpleName.asString()
(parentDeclaration as KSClassDeclaration).toClassName(),
simpleName.asString()
)
} else {
Value.Class(declaration.toClassName())
Value.Class(toClassName())
}
}

is KSType -> (declaration as KSClassDeclaration).toAnnotationValue(node, type)

is KSName -> Value.Enum(
enumType = ClassName.bestGuess(getQualifier()),
value = getShortName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import se.ansman.kotshi.Types

internal data class JsonAdapterFactory(
val targetType: ClassName,
val usageType: UsageType,
val generatedAdapters: List<GeneratedAdapter<*>>,
val manuallyRegisteredAdapters: List<RegisteredAdapter<*>>,
) {
Expand Down Expand Up @@ -121,14 +120,6 @@ internal data class JsonAdapterFactory(
?.typeArguments
?.single()
}

sealed class UsageType {
/** Generates an object that directly implements JsonAdapter.Factory */
object Standalone : UsageType()

/** Generates an object that implements the given [parent] which in turn implements JsonAdapter.Factory */
data class Subclass(val parent: TypeName, val parentIsInterface: Boolean) : UsageType()
}
}

internal fun <C, P> Sequence<C>.findKotshiConstructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
package se.ansman.kotshi.renderer

import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.NOTHING
import com.squareup.kotlinpoet.NameAllocator
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.STAR
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.moshi.JsonAdapter
import se.ansman.kotshi.*
import se.ansman.kotshi.Functions
import se.ansman.kotshi.KotshiConstructor
import se.ansman.kotshi.Types
import se.ansman.kotshi.addControlFlow
import se.ansman.kotshi.applyEachIndexed
import se.ansman.kotshi.applyIf
import se.ansman.kotshi.hasParameters
import se.ansman.kotshi.mapTypeArguments
import se.ansman.kotshi.model.GeneratedAnnotation
import se.ansman.kotshi.model.JsonAdapterFactory
import se.ansman.kotshi.model.render
import se.ansman.kotshi.nullable
import se.ansman.kotshi.rawType
import se.ansman.kotshi.unwrapTypeVariables

internal class JsonAdapterFactoryRenderer(
private val factory: JsonAdapterFactory,
Expand Down Expand Up @@ -39,18 +61,7 @@ internal class JsonAdapterFactoryRenderer(
TypeSpec.objectBuilder(factory.factoryClassName)
.addModifiers(KModifier.INTERNAL)
.apply { generatedAnnotation?.toAnnotationSpec()?.let(::addAnnotation) }
.apply {
when (factory.usageType) {
JsonAdapterFactory.UsageType.Standalone -> addSuperinterface(Types.Moshi.jsonAdapterFactory)
is JsonAdapterFactory.UsageType.Subclass -> {
if (factory.usageType.parentIsInterface) {
addSuperinterface(factory.usageType.parent)
} else {
superclass(factory.usageType.parent)
}
}
}
}
.addSuperinterface(Types.Moshi.jsonAdapterFactory)
.addAnnotations(annotations)
.addProperties(properties)
.addFunction(createFunction)
Expand Down
14 changes: 8 additions & 6 deletions gradle-plugin/src/main/kotlin/library.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import org.gradle.accessors.dm.LibrariesForLibs
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
kotlin("jvm")
Expand All @@ -15,11 +16,12 @@ extensions.configure<JavaPluginExtension> {
}
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "1.8"
allWarningsAsErrors = true
freeCompilerArgs = freeCompilerArgs + listOf(
kotlin {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
allWarningsAsErrors.set(true)
freeCompilerArgs.addAll(
"-Xsuppress-version-warnings",
"-opt-in=kotlin.RequiresOptIn",
"-Xjvm-default=all",
"-Xcontext-receivers"
Expand Down
1 change: 1 addition & 0 deletions gradle-plugin/src/main/kotlin/test-library.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sourceSets {

tasks.withType<Test>().configureEach {
systemProperty("usingLegacyMoshi", providers.gradleProperty("kotshi.internal.useLegacyMoshi").orElse("false").get())
maxHeapSize = "1g"
jvmArgs(
"--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version=2.16.0-SNAPSHOT

kapt.include.compile.classpath=false
kapt.incremental.apt=true
kapt.use.k2=true
ksp.useKSP2=true

kotlin.code.style=official

Expand Down
8 changes: 5 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[versions]
kotlin = "1.9.24"
kotlin = "2.0.0"
auto-service = "1.1.1"
kotlinpoet = "1.17.0"
compileTesting = "0.4.1"
compileTesting = "0.5.0"
incap = "1.0.0"
ksp = "1.9.24-1.0.20"
ksp = "2.0.0-1.0.23"
moshi-oldestSupported = "1.8.0"
moshi-latest = "1.15.1"

Expand Down Expand Up @@ -44,6 +44,8 @@ incap-compiler = { module = "net.ltgt.gradle.incap:incap-processor", version.ref

ksp = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp" }
ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
ksp-commonDeps = { module = "com.google.devtools.ksp:symbol-processing-common-deps", version.ref = "ksp" }
ksp-aaEmbeddable = { module = "com.google.devtools.ksp:symbol-processing-aa-embeddable", version.ref = "ksp" }

asm = "org.ow2.asm:asm:9.7"

Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenLocal()
}
}

Expand All @@ -19,6 +20,7 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
mavenLocal()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class KaptGeneratorTest : BaseGeneratorTest() {
override fun KotlinCompilation.setUp(options: Map<String, String>) {
annotationProcessors = listOf(KotshiProcessor())
kaptArgs.putAll(options)
useKapt4 = true
}

override fun JvmCompilationResult.tryLoadClass(name: String): Class<*> = classLoader.loadClass(name)
Expand Down
2 changes: 2 additions & 0 deletions tests/ksp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ dependencies {
ksp(projects.compiler)
testRuntimeOnly(libs.ksp)
testImplementation(libs.ksp.api)
testImplementation(libs.ksp.commonDeps)
testImplementation(libs.ksp.aaEmbeddable)
testImplementation(libs.compileTesting.ksp)
}
Loading
Loading