Giter VIP home page Giter VIP logo

jvmbuilder's Introduction

JvmBuilder

Download

A source code generator for Kotlin data classes to automatically create a Builder class.

How to use JvmBuilder

When annotating a Kotlin data class with @JvmBuilder

@JvmBuilder
data class Test(val foo: Int = 1, val bar: String)

The following class is generated

// Code auto-generated by JvmBuilder. Do not edit.
package com.example

import kotlin.Int
import kotlin.String

class JvmBuilder_Test {
    private var foo: Int? = null

    private var bar: String? = null

    fun foo(foo: Int): JvmBuilder_Test {
        this.foo = foo
        return this
    }

    fun bar(bar: String): JvmBuilder_Test {
        this.bar = bar
        return this
    }

    fun build(): Test {
        var result = com.example.Test(bar = this.bar!!)
        result = result.copy(foo = this.foo ?: result.foo)
        return result
    }
}

This provides a Builder class that can be used in Java to create your Kotlin Test data class using the default values and following a Builder Pattern.

The following Java code generates a Test instance with foo = 1 (taking the 1 from the default Kotlin constructor) and bar = "bar".

new Jvm_Builder().bar("bar").build()

Gradle

Gradle users should add the dependencies in their build.gradle file:

dependencies {
  implementation "io.github.jffiorillo:jvmbuilder-annotations:<latest_version>"
  kapt "io.github.jffiorillo:jvmbuilder:<latest_version>"
}

Maven

Maven users should add the dependencies in their pom.xml file:

<dependency>
  <groupId>io.github.jffiorillo</groupId>
  <artifactId>jvmbuilder-annotations</artifactId>
  <version>{latest_version}</version>
</dependency>
<dependency>
  <groupId>io.github.jffiorillo</groupId>
  <artifactId>jvmbuilder</artifactId>
  <version>{latest_version}</version>
  <scope>provided</scope>
</dependency>

Benefit

  • In order to have a good Kotlin interoperability with Java, you can use this tool to generate automatically generate Builders for your data classes that are accessible from Java consumers.
  • The time of executing the annotation processor is really slow because uses the kotlin-metadata instead of reflection.

jvmbuilder's People

Contributors

gortiz avatar jffiorillo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.