An AutoValue extension that generates binary and source compatible equivalent Kotlin data classes of AutoValue models.

Overview

AutoValue Kotlin

auto-value-kotlin (AVK) is an AutoValue extension that generates binary-and-source-compatible, equivalent Kotlin data classes. This is intended to help migrations by doing 95% of the work and just letting the developer come through and clean up the generated file as-needed.

The intended use of this project is to ease migration from AutoValue classes to Kotlin data classes and should be used ad-hoc rather than continuously. The idea is that it does 95% of the work for you while leaving obvious placeholders or TODOs in places that require manual adjustment.

Installation

Add the dependency to kapt

dependencies {
  kapt("com.google.auto.value:auto-value:<version>")
  kapt("com.slack.auto.value:auto-value-kotlin:<version>")
}

Snapshots of the development version are available in Sonatype's snapshots repository.

Configure the following arguments in your build file

kapt {
  arguments {
    // Source dir to output files to. This should usually be the src/main/java or src/main/kotlin
    // path of the project you’re running this in.
    // REQUIRED
    arg("avkSrc", file("src/main/java"))

    // Colon-delimited string of simple class names to convert
    // OPTIONAL. By default, AVK will convert all AutoValue models on the compilation.
    arg("avkTargets", "ClassOne:ClassTwo")

    // Boolean option to ignore nested classes. By default, AVK will error out when it encounters
    // a nested AutoValue class as it has no means of safely converting the class since its
    // references are always qualified. This option can be set to true to make AVK just skip them
    // and emit a warning.
    // OPTIONAL. False by default.
    arg("avkIgnoreNested", "true")
  }
}

Workflow

Pre-requisites

  • Move any nested AutoValue classes to top-level first (even if just temporarily for the migration).
    • You can optionally choose to ignore nested classes or only specific targets per the configuration options detailed in the Installation section above.
  • Ensure no classes outside of the original AutoValue class accesses its generated AutoValue_ class.
  • Clean once to clear up any generated file references.

Add the AVK configuration to your project's build file

Run the kapt task, such as ./gradlew :path:to:library:kaptReleaseKotlin or /gradlew :path:to:library:kaptKotlin.

Now you'll see generated equivalent Kotlin files in the source set defined by avkSrc.

Repeat the following for each generated file.

  • Open the original AV file and change the class name to something else, so it doesn’t link in the IDE to existing places. For example: rename class AuthModel to class AuthModelOld. image
  • Open the generated Kotlin file (i.e. AuthModel.kt). The file will look a little noisy, but this is by design to intelligently best-effort preserve Java and Kotlin interop.
    1. You shouldn’t need to edit any Java usages 🤞 , only Kotlin usages to switch to the property accessors. The generated deprecated functions will have @JvmName annotations with funny values that hide them from Java callers.
  • For any @Deprecated functions, these are functions that may have existing Kotlin usages that need to be updated. These come with ReplaceWith information to allow the IDE to auto-migrate these where possible. For each of these, you should…
    1. Put your cursor on the first line of the function (it looks like a recursive call)
    2. option+enter on it to bring up inspections, then click “Replace all usages in project”.
    3. Delete the function image
  • For any “placeholders”, these are cases where AVK couldn’t migrate them automatically. This includes final non-property methods, static factories/helpers, etc. For these, it’s best to copy them in the original AV file (i.e. from AuthModel.java) and paste them into the new Kotlin file. The IDE will automatically ask if you want to convert it to Kotlin and do most of the work for you. image
  • When done, delete the old Java file.
    • If you want to go the extra mile for review and have git recognize the file migration, you can rename it first with no changes, commit the rename, then commit the content changes in a separate commit.

When done with all the migrations, revert the added AVK configuration to your build file and be done with it!

Supported features/interop

License

Copyright 2021 Slack Technologies, LLC

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.
Comments
  • Add JsonClass annotations to enums where appropriate

    Add JsonClass annotations to enums where appropriate

    We were accidentally omitting these before. This only applies it if they have @Json-annotated members or the enclosing AV class is annotated with @JsonClass

    opened by ZacSweers 0
  • Rework implementation to support nested classes

    Rework implementation to support nested classes

    This reworks the implementation in an interesting new way to support nested classes. While the grunt work is still done via AutoValueKotlinExtension, we now actually don't export a service file for it and instead run our own AutoValue annotation processor (:O). This processor acts a pass-through processor that internally calls AutoValueProcessor with service-loaded extensions + our AVK extension. It's given a no-op Filer that effectively ignores all outputs.

    After processing is run, it then reads the processed AVK classes back from the extension and then aggregates them all until the end of processing. Finally, at the end of processing it stitches together all the classes and writes them out in one go.

    This allows us to support nested classes and even basic support for nested enum classes, easing use!

    This also cleans up some visibility handling and jvm modifiers on parameters

    opened by ZacSweers 0
  • More API tweaking

    More API tweaking

    3 changes!

    • Switch to accepting supertypes map for KotlinClass, which allows for class delegation
    • Allow overriding property default values
    • Omit implicit kotlin.* imports
    opened by ZacSweers 0
  • Split internals into separate files with `@ExperimentalAvkApi`

    Split internals into separate files with `@ExperimentalAvkApi`

    This allows for (unstable) reuse of AVK's inner components, which we've found useful for converting other types to Kotlin too. Totally unsupported API and requires opting in to the @ExperimentalAvkApi annotation.

    opened by ZacSweers 0
  • Finish supporting autoBuild vs build

    Finish supporting autoBuild vs build

    Currently this snippet will generate the autoBuild() method but not the build() method

    @AutoValue.Builder
    public abstract static class Builder {
      public abstract Builder image48(String image48);
    
      public abstract Builder image64(String image64);
    
      public abstract Builder image72(String image72);
    
      abstract AppIcons autoBuild();
    
      public AppIcons build() {
        return autoBuild();
      }
    }
    
    enhancement 
    opened by ZacSweers 0
Releases(1.1.3)
  • 1.1.3(Nov 15, 2021)

    • Fix: Don't leak nested enums across multiple AVK extension passes.
    • Fix: Add missing @JsonClass annotation to nested enums.
    • Fix: Generate constructor parameter defaults for @JsonClass-annotated classes.
    Source code(tar.gz)
    Source code(zip)
  • 1.1.2(Nov 5, 2021)

    • Enhancement: Skip processing if there are no @AutoValue-annotated classes in the current round.
    • Enhancement: Report an error if there are no @AutoValue-annotated classes across all rounds.
    • Fix: Use correct classloader when loading AutoValueExtension services
    Source code(tar.gz)
    Source code(zip)
  • 1.1.1(Nov 3, 2021)

  • 1.1.0(Nov 3, 2021)

    The implementation has been reworked to run as a standalone processor that then runs AutoValue within its own process. This allows us to share some data across all processed types and expand the supported feature set.

    As a result...

    • New: Support for converting nested AutoValue classes!
    • New: (Basic) support for converting nested enum classes!
    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(Oct 25, 2021)

  • 1.0.1(Oct 23, 2021)

  • 1.0.0(Oct 21, 2021)

Owner
Slack
On a mission to make your working life simpler, more pleasant and more productive.
Slack
Spring & Kotlin equivalent to original CraftMania's TypeScript CraftAPI.

KotlinCraftAPI This project is Spring & Kotlin equivalent to original CraftMania's TypeScript CraftAPI. This project was created solely to study Sprin

Jakub Bordáš 0 Nov 6, 2021
Template for building CLI tool in Kotlin and producing native binary

Kotlin command-line native tool template This template allows you to quickly build command-line tool using Kotlin , Clikt and build a native binary fo

Ryszard Grodzicki 10 Dec 31, 2022
A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server

dadb Blog Post: Our First Open-Source Project A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server de

mobile.dev 791 Dec 20, 2022
The tool allows to dump binary API of a Kotlin library

Binary compatibility validator The tool allows to dump binary API of a Kotlin library that is public in sense of Kotlin visibilities and ensures that

Kotlin 490 Dec 31, 2022
Small kotlin library for persisting _single instances_ of kotlin data classes

PerSista Small library for persisting single instances of kotlin data classes. NB: PerSista uses typeOf() internally which is marked as @ExperimentalS

Eric Donovan 5 Nov 13, 2022
This is a sample app to demonstrate the power of using EventSourced models and the ease with which these can be modelled using Kotlin.

Lego 4 Rent This is a sample app to demonstrate the power of using EventSourced models and the ease with which these can be modelled using Kotlin. To

Nico Krijnen 4 Jul 28, 2022
A backend service that generates a random 32-chars length message and its sha256 hashcode and put them to DB

This is an Android application in which a user can ask the server to generate a random message and its sha256, and ask to obtain this info from the server.

Iskander 1 Nov 20, 2022
🎲 Kotlin Symbol Processor to auto-generate extensive sealed classes and interfaces for Android and Kotlin.

SealedX ?? Kotlin Symbol Processor to auto-generate extensive sealed classes and interfaces for Android and Kotlin. Why SealedX? SealedX generates ext

Jaewoong Eum 236 Nov 30, 2022
A webapp which generates a simple Discord profile banner image in real-time which shows user's status and activity.

DiscordProfileBanner This tool generates a Discord profile banner image in realtime. I wrote it for use in my AniList profile. An example in action: H

Quanta 11 Oct 17, 2022
KSP-based library that generates lists from your annotation usages

ListGen, Generate Lists From Functions That Have @Listed Annotations! Welcome to ListGen! ListGen is a KSP-based library that can generate lists (and

Adib Faramarzi 24 Dec 6, 2022
Automatically generates UI demos which allow users to call any function with any parameters

Automatically generates UI demos which allow users to call any function (including composable ones) with any parameters. Useful for building demo screens in playground apps of various design systems.

Anton Popov 3 Jul 28, 2022
Native-Blur: a C++/Kotlin library for blur bitmaps and activity, mobile-ready, android compatible

Native-Blur The Native-Blur is a C++/Kotlin libraray for blur bitmaps and activity, mobile-ready, android compatible, powered by Java Native Interface

Abolfazl Abbasi 26 Dec 13, 2022
🧶 Library to handling files for persistent storage with Google Cloud Storage and Amazon S3-compatible server, made in Kotlin

?? Remi Library to handling files for persistent storage with Google Cloud Storage and Amazon S3-compatible server, made in Kotlin! Why is this built?

Noelware 8 Dec 17, 2022
Kotlin compiler plugin for converting suspend functions to platform-compatible functions

Kotlin suspend transform compiler plugin Summary Kotlin compiler plugin for generating platform-compatible functions for suspend functions. JVM class

ForteScarlet 5 Oct 12, 2022
Event State Processor Generator plugin is compatible with IntelliJ and Android Studio.

Event State Processor Generator plugin is compatible with IntelliJ and Android Studio. It provides source code generation for the EventStateProcessor Library to increase code productivity in Flutter apps development.

Extreme Vietnam Public 2 Dec 7, 2021
Android application compatible with ZX2C4's Pass command line application

Password Store Download Documentation We're in the process of rewriting our documentation from scratch, and the work-in-progress state can be seen her

Android Password Store 2.2k Dec 29, 2022
A convenient BMI (Body Mass Index) calculator that is compatible with Android devices.

?? BMI Calculator A convenient BMI (Body Mass Index) calculator that is compatible with Android devices. ?? Please feel free to compute your BMI using

Md. Zahidul Islam 12 Dec 23, 2022
XliteKt is an open-source, and forever open-source Kotlin based OSRS Emulator for educational purposes.

xlitekt Introduction XliteKt is an open-source, and forever open-source Kotlin based OSRS Emulator for educational purposes. Currently built around th

Runetopic 6 Dec 16, 2022