Skip to content

Commit

Permalink
Kotlin 1.6 (#163)
Browse files Browse the repository at this point in the history
* Update to Kotlin 1.6.0

* Update Gradle to 7.3

* Fixup version handling

* Fix up gradle properties

* Switch to ktfmt

* Switch to ktfmt

* Update Moshi, remove ksp/records!

* Bump spotless

* Misc bumps

* Update gjf
  • Loading branch information
ZacSweers authored Dec 10, 2021
1 parent 0612cc5 commit e33a9cd
Show file tree
Hide file tree
Showing 86 changed files with 1,643 additions and 8,230 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
java_version: [ 17 ]
kotlin_version: [ 1.5.30 ]
kotlin_version: [ 1.6.0 ]
ksp_enabled: [ true, false ]
ksp_incremental_enabled: [ true, false ]
exclude:
Expand Down
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
Extensions for [Moshi](https://github.com/square/moshi)

* [moshi-adapters](https://github.com/ZacSweers/MoshiX/tree/main/moshi-adapters) - A collection of custom adapters for Moshi.
* [moshi-ksp](https://github.com/ZacSweers/MoshiX/tree/main/moshi-ksp) - A [KSP](https://github.com/google/ksp) implementation of Moshi Kotlin Codegen.
* [moshi-records-reflect](https://github.com/ZacSweers/MoshiX/tree/main/moshi-records-reflect) - Reflective support for Java `record` classes.
* [moshi-metadata-reflect](https://github.com/ZacSweers/MoshiX/tree/main/moshi-metadata-reflect) - A [kotlinx-metadata](https://github.com/JetBrains/kotlin/tree/master/libraries/kotlinx-metadata/jvm) based implementation of `KotlinJsonAdapterFactory`. This allows for reflective Moshi serialization on Kotlin classes without the cost of including kotlin-reflect.
* [moshi-sealed](https://github.com/ZacSweers/MoshiX/tree/main/moshi-sealed) - Reflective and code gen implementations for serializing Kotlin sealed classes via Moshi polymorphic adapters.

Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap].

### Local development

This project requires JDK 11 as a minimum due to tooling compatibility requirements to build against newer JDK
This project requires JDK 17 as a minimum due to tooling compatibility requirements to build against newer JDK
APIs.

If you want to develop anything targeting Java sealed classes or records, use JDK 16. By default, these projects will
not be enabled unless the current JDK supports 16+.

License
--------

Expand Down
87 changes: 39 additions & 48 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,37 @@
* limitations under the License.
*/

import java.net.URL
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.net.URL

buildscript {
dependencies {
classpath(kotlin("gradle-plugin", version = (System.getenv()["MOSHIX_KOTLIN"] ?: "1.5.30")))
classpath(
kotlin(
"gradle-plugin",
version = (System.getenv()["MOSHIX_KOTLIN"] ?: libs.versions.kotlin.get())))
}
}

plugins {
kotlin("jvm") version (System.getenv()["MOSHIX_KOTLIN"] ?: "1.5.30") apply false
kotlin("jvm") version (System.getenv()["MOSHIX_KOTLIN"] ?: libs.versions.kotlin.get()) apply false
alias(libs.plugins.dokka) apply false
alias(libs.plugins.mavenPublish) apply false
alias(libs.plugins.spotless)
alias(libs.plugins.kotlinBinaryCompatibilityValidator)
}

apiValidation {
ignoredProjects.addAll(
listOf(
/* :moshi-ksp: */ "extra-moshi-test-module",
/* :moshi-ksp: */ "tests",
/* :moshi-sealed: */ "sample"
)
)
ignoredProjects +=
listOf(
/* :moshi-sealed: */
"sample")
}

repositories { mavenCentral() }

spotless {
format("misc") {
target("*.md", ".gitignore")
Expand All @@ -53,43 +55,42 @@ spotless {
java {
googleJavaFormat(libs.versions.gjf.get())
target("**/*.java")
targetExclude(
"**/spotless.java",
"**/build/**"
)
targetExclude("**/spotless.java", "**/build/**")
licenseHeaderFile("spotless/spotless.java")
}
kotlin {
ktlint(libs.versions.ktlint.get()).userData(mapOf("indent_size" to "2"))
ktfmt("0.30")
target("**/*.kt")
trimTrailingWhitespace()
endWithNewline()
licenseHeaderFile("spotless/spotless.kt")
.updateYearWithLatest(false)
licenseHeaderFile("spotless/spotless.kt").updateYearWithLatest(false)
targetExclude(
"**/Dependencies.kt", "**/spotless.kt", "**/build/**", "**/moshi-ksp/tests/**",
"**/moshi-ksp/moshi-ksp/src/main/kotlin/dev/zacsweers/moshix/ksp/shade/**"
"**/Dependencies.kt",
"**/spotless.kt",
"**/build/**",
)
}
// format("externalKotlin", KotlinExtension::class.java) {
// // These don't use our spotless config for header files since we don't want to overwrite the
// // existing copyright headers.
// configureCommonKotlinFormat()
// }
// format("externalKotlin", KotlinExtension::class.java) {
// // These don't use our spotless config for header files since we don't want to overwrite the
// // existing copyright headers.
// configureCommonKotlinFormat()
// }
kotlinGradle {
ktlint(libs.versions.ktlint.get()).userData(mapOf("indent_size" to "2"))
ktfmt("0.30")
target("**/*.gradle.kts")
trimTrailingWhitespace()
endWithNewline()
licenseHeaderFile("spotless/spotless.kts", "(import|plugins|buildscript|dependencies|pluginManagement)")
licenseHeaderFile(
"spotless/spotless.kts", "(import|plugins|buildscript|dependencies|pluginManagement)")
}
}

subprojects {
repositories {
mavenCentral()
google()
// Kotlin bootstrap repository, useful for testing against Kotlin dev builds. Usually only tested on CI shadow jobs
// Kotlin bootstrap repository, useful for testing against Kotlin dev builds. Usually only
// tested on CI shadow jobs
// https://kotlinlang.slack.com/archives/C0KLZSCHF/p1616514468003200?thread_ts=1616509748.001400&cid=C0KLZSCHF
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap") {
name = "Kotlin-Bootstrap"
Expand All @@ -99,35 +100,29 @@ subprojects {
}
}
}
val toolChainVersion = project.findProperty("moshix.javaLanguageVersion")?.toString() ?: "8"
val releaseVersion = project.findProperty("moshix.javaReleaseVersion")?.toString() ?: "8"
val release = releaseVersion.toInt()
pluginManager.withPlugin("java") {
configure<JavaPluginExtension> {
toolchain {
languageVersion.set(JavaLanguageVersion.of(toolChainVersion))
}
}
configure<JavaPluginExtension> { toolchain { languageVersion.set(JavaLanguageVersion.of(17)) } }

project.tasks.withType<JavaCompile>().configureEach {
options.release.set(release)
}
project.tasks.withType<JavaCompile>().configureEach { options.release.set(release) }
}
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = libs.versions.jvmTarget.get()
@Suppress("SuspiciousCollectionReassignment")
freeCompilerArgs += listOf("-Xjsr305=strict", "-progressive", "-Xopt-in=kotlin.RequiresOptIn")
freeCompilerArgs +=
listOf("-Xjsr305=strict", "-progressive", "-Xopt-in=kotlin.RequiresOptIn")
// TODO disabled because Gradle's Kotlin handling is silly
// https://github.com/gradle/gradle/issues/16779
// allWarningsAsErrors = true
// allWarningsAsErrors = true
}
}
if (project.name != "sample" && !project.path.contains("sample") && !project.path.contains("test")) {
configure<KotlinProjectExtension> {
explicitApi()
}
if (project.name != "sample" &&
!project.path.contains("sample") &&
!project.path.contains("test")) {
configure<KotlinProjectExtension> { explicitApi() }
}
}
pluginManager.withPlugin("com.vanniktech.maven.publish") {
Expand All @@ -136,12 +131,8 @@ subprojects {
outputDirectory.set(rootProject.rootDir.resolve("docs/0.x"))
dokkaSourceSets.configureEach {
skipDeprecated.set(true)
externalDocumentationLink {
url.set(URL("https://square.github.io/okio/2.x/okio/"))
}
externalDocumentationLink {
url.set(URL("https://square.github.io/moshi/1.x/moshi/"))
}
externalDocumentationLink { url.set(URL("https://square.github.io/okio/2.x/okio/")) }
externalDocumentationLink { url.set(URL("https://square.github.io/moshi/1.x/moshi/")) }
}
}
}
Expand Down
23 changes: 2 additions & 21 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,12 @@
# limitations under the License.
#

# Add opens for Kapt
# https://youtrack.jetbrains.com/issue/KT-45545#focus=Comments-27-4862682.0-0
# Adds exports for GJF in spotless
# https://github.com/diffplug/spotless/issues/834
# Memory for Dokka https://github.com/Kotlin/dokka/issues/1405
# --add-opens for GJF https://github.com/google/google-java-format#jdk-16
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 \
--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

kotlin.daemon.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 \
--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

Expand Down
20 changes: 9 additions & 11 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,26 @@
[versions]
autoService = "1.0"
incap = "0.3"
gjf = "1.11.0"
kotlinCompileTesting = "1.4.4"
kotlin = "1.5.31"
gjf = "1.13.0"
kotlinCompileTesting = "1.4.6"
kotlin = "1.6.0"
javaRelease = "8"
jvmTarget = "1.8"
kotlinpoet = "1.10.1"
ksp = "1.5.30-1.0.0"
kotlinpoet = "1.10.2"
ksp = "1.6.0-1.0.1"
ktlint = "0.41.0"
moshi = "1.12.0"
okhttp = "4.9.1"
moshi = "1.13.0"
okhttp = "4.9.3"
retrofit = "2.9.0"

[plugins]
dokka = { id = "org.jetbrains.dokka", version = "1.5.30" }
dokka = { id = "org.jetbrains.dokka", version = "1.6.0" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.18.0" }
spotless = { id = "com.diffplug.spotless", version = "5.15.0" }
spotless = { id = "com.diffplug.spotless", version = "6.0.4" }
kotlinBinaryCompatibilityValidator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version = "0.7.1" }

[libraries]
asm = { module = "org.ow2.asm:asm", version = "9.2" }

autoCommon = { module = "com.google.auto:auto-common", version = "1.1" }

autoService = { module = "com.google.auto.service:auto-service-annotations", version.ref = "autoService" }
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion moshi-adapters/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tasks.named<KotlinCompile>("compileTestKotlin") {

dependencies {
implementation(libs.moshi)
kspTest(project(":moshi-ksp:moshi-ksp"))
kspTest(libs.moshi.codegen)
testImplementation(libs.moshi.kotlin)
testImplementation(libs.okhttp)
testImplementation(libs.okhttp.mockwebserver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.reflect.KClass

/**
* An annotation that indicates the Moshi [JsonAdapter] or [JsonAdapter.Factory] to use
* with a class or property. The adapter class must have a public default constructor.
* An annotation that indicates the Moshi [JsonAdapter] or [JsonAdapter.Factory] to use with a class
* or property. The adapter class must have a public default constructor.
*
* Here is an example of how this annotation is used:
* ```
Expand All @@ -55,8 +55,8 @@ import kotlin.reflect.KClass
* }
* ```
*
* Since `User` class specified `UserJsonAdapter` in `@AdaptedBy` annotation, it
* will be invoked to encode/decode `User` instances.
* Since `User` class specified `UserJsonAdapter` in `@AdaptedBy` annotation, it will be invoked to
* encode/decode `User` instances.
*
* Here is an example of how to apply this annotation to a property as a [JsonQualifier].
*
Expand All @@ -66,21 +66,17 @@ import kotlin.reflect.KClass
* )
* ```
*
* The class referenced by this annotation must be either a [JsonAdapter]
* or a [JsonAdapter.Factory].
* Using [JsonAdapter.Factory] makes it possible to delegate
* to the enclosing [Moshi] instance.
* The class referenced by this annotation must be either a [JsonAdapter] or a [JsonAdapter.Factory]
* . Using [JsonAdapter.Factory] makes it possible to delegate to the enclosing [Moshi] instance.
*
* @property adapter Either a [JsonAdapter] or [JsonAdapter.Factory].
* @property nullSafe Set to false to be able to handle null values within the adapter, default value is true.
* @property nullSafe Set to false to be able to handle null values within the adapter, default
* value is true.
*/
@JsonQualifier
@Retention(RUNTIME)
@Target(CLASS, PROPERTY, FIELD)
public annotation class AdaptedBy(
val adapter: KClass<*>,
val nullSafe: Boolean = true
) {
public annotation class AdaptedBy(val adapter: KClass<*>, val nullSafe: Boolean = true) {
public class Factory : JsonAdapter.Factory {
override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*>? {
var adaptedBy: AdaptedBy?
Expand All @@ -103,23 +99,22 @@ public annotation class AdaptedBy(
if (adaptedBy == null) return null
val adapterClass = adaptedBy.adapter
val javaClass = adapterClass.java
val adapter = when {
JsonAdapter.Factory::class.java.isAssignableFrom(javaClass) -> {
val factory = javaClass.getDeclaredConstructor()
.newInstance() as JsonAdapter.Factory
factory.create(type, nextAnnotations.orEmpty(), moshi)
}
JsonAdapter::class.java.isAssignableFrom(javaClass) -> {
javaClass.getDeclaredConstructor()
.newInstance() as JsonAdapter<*>
}
else -> {
error(
"Invalid attempt to bind an instance of ${javaClass.name} as a @AdaptedBy for $type. @AdaptedBy " +
"value must be a JsonAdapter or JsonAdapter.Factory."
)
}
} ?: return null
val adapter =
when {
JsonAdapter.Factory::class.java.isAssignableFrom(javaClass) -> {
val factory = javaClass.getDeclaredConstructor().newInstance() as JsonAdapter.Factory
factory.create(type, nextAnnotations.orEmpty(), moshi)
}
JsonAdapter::class.java.isAssignableFrom(javaClass) -> {
javaClass.getDeclaredConstructor().newInstance() as JsonAdapter<*>
}
else -> {
error(
"Invalid attempt to bind an instance of ${javaClass.name} as a @AdaptedBy for $type. @AdaptedBy " +
"value must be a JsonAdapter or JsonAdapter.Factory.")
}
}
?: return null

return if (adaptedBy.nullSafe) {
adapter.nullSafe()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.nextAnnotations
import okio.BufferedSource
import java.lang.reflect.Type
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FIELD
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY
import okio.BufferedSource

/**
* A [JsonQualifier] for use with [String] properties to indicate that their value should be
Expand Down Expand Up @@ -58,7 +58,7 @@ public annotation class JsonString {

private class JsonStringJsonAdapter : JsonAdapter<String>() {
override fun fromJson(reader: JsonReader): String =
reader.nextSource().use(BufferedSource::readUtf8)
reader.nextSource().use(BufferedSource::readUtf8)

override fun toJson(writer: JsonWriter, value: String?) {
writer.valueSink().use { sink -> sink.writeUtf8(checkNotNull(value)) }
Expand Down
Loading

0 comments on commit e33a9cd

Please sign in to comment.