https://github.com/jffiorillo/jvmbuilder
A source code generator for Kotlin data classes to automatically create a Builder class.
https://github.com/jffiorillo/jvmbuilder
annotation-processor java kapt kotlin kotlin-android kotlin-library
Last synced: 15 days ago
JSON representation
A source code generator for Kotlin data classes to automatically create a Builder class.
- Host: GitHub
- URL: https://github.com/jffiorillo/jvmbuilder
- Owner: jffiorillo
- License: other
- Created: 2018-08-07T12:17:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-15T14:36:21.000Z (over 7 years ago)
- Last Synced: 2023-07-04T03:46:32.504Z (over 2 years ago)
- Topics: annotation-processor, java, kapt, kotlin, kotlin-android, kotlin-library
- Language: Kotlin
- Homepage:
- Size: 79.1 KB
- Stars: 16
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# JvmBuilder
[  ](https://bintray.com/jffiorillo/jvmbuilder/jvmbuilder/_latestVersion)
A source code generator for [Kotlin](https://kotlinlang.org/) 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`](https://en.wikipedia.org/wiki/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:"
kapt "io.github.jffiorillo:jvmbuilder:"
}
```
## Maven
Maven users should add the dependencies in their `pom.xml` file:
```
io.github.jffiorillo
jvmbuilder-annotations
{latest_version}
io.github.jffiorillo
jvmbuilder
{latest_version}
provided
```
## Benefit
* In order to have a good Kotlin interoperability with [Java](https://en.wikipedia.org/wiki/Java_(programming_language)), 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](https://github.com/Takhion/kotlin-metadata) instead of reflection.